Skip to content

Commit 65f1e64

Browse files
author
Robin Duda
committed
improve styling/formatting of launcher help text.
1 parent a698c02 commit 65f1e64

File tree

3 files changed

+64
-50
lines changed

3 files changed

+64
-50
lines changed

core/main/java/com/codingchili/core/configuration/CoreStrings.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public abstract class CoreStrings {
2727
// Author.
2828
public static String VERSION;
2929
public static final String GITHUB = "https://github.com/codingchili";
30-
public static final String AUTHOR = "Robin Duda \u00a9 2019";
30+
public static final String AUTHOR = "Robin Duda \u00a9 2020";
3131

3232
static {
3333
VERSION = CoreStrings.class.getPackage().getImplementationVersion();
@@ -196,7 +196,7 @@ public abstract class CoreStrings {
196196
public static final String ERROR_CONFIGURATION_MISMATCH = "configuration mismatches with currently loaded.";
197197
public static final String ERROR_ALREADY_INITIALIZED = "Error already initialized.";
198198
public static final String ERROR_STORAGE_EXCEPTION = "Failed to perform a storage operation.";
199-
public static final String CONFIGURED_BLOCKS = "Configured deployment blocks";
199+
public static final String CONFIGURED_BLOCKS = "Configured deployment blocks and remotes available";
200200
public static final String ERROR_PATCH_RELOADED = "The patch version changed during patch session.";
201201
public static final String[] BENCHMARK_CONSOLE_REPORT_COLUMNS =
202202
{"[GROUP]", "[IMPLEMENTATION]", "[BENCHMARK]", "[OP/s]", "[TIME]"};
@@ -491,10 +491,6 @@ public static String getCommandExecutorHelpDescription() {
491491
return "prints this help text.";
492492
}
493493

494-
public static String getRemotesAvailable() {
495-
return "remotes available";
496-
}
497-
498494
public static String getBenchmarkDescription() {
499495
return "Executes benchmarks. [--iterations ?, --html]";
500496
}
@@ -541,14 +537,6 @@ public static String formatAsPercent(Double value) {
541537
return new DecimalFormat("#.00").format(value);
542538
}
543539

544-
public static List<String> getCommandExecutorText() {
545-
List<String> list = new ArrayList<>();
546-
list.add("================================ HELP ================================");
547-
list.add("\t\t<block-name>\t\t\tdeploys the services configured in the given block.");
548-
list.add("\t\t<remote-name>\t\t\tdeploys configured blocks on a remote host.");
549-
return list;
550-
}
551-
552540
public static String getMissingEntity(String key) {
553541
return "Error: could not get '" + key + "' in storage.";
554542
}
@@ -649,5 +637,18 @@ public static String getKeystoreTooManyEntries(String path) {
649637
return String.format("Keystore at '%s' contains too many entries, only one entry supported.", path);
650638
}
651639

640+
public static String pad(String text, int spaces) {
641+
int padding = spaces - text.length();
642+
if (padding > 0) {
643+
return text + String.join("", Collections.nCopies(padding, " "));
644+
} else {
645+
return text;
646+
}
647+
}
648+
649+
public static String leftPad(String text, int spaces) {
650+
return pad("", spaces) + text;
651+
}
652+
652653
private enum IPVersion {IP4, IP6;}
653654
}

core/main/java/com/codingchili/core/context/LauncherCommandExecutor.java

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.codingchili.core.context;
22

33
import io.vertx.core.Future;
4+
import org.fusesource.jansi.Ansi;
45

56
import java.util.*;
67
import java.util.function.Function;
78

89
import com.codingchili.core.benchmarking.CoreBenchmarkSuite;
9-
import com.codingchili.core.configuration.CoreStrings;
1010
import com.codingchili.core.files.Configurations;
11-
import com.codingchili.core.logging.*;
11+
import com.codingchili.core.logging.Logger;
1212
import com.codingchili.core.security.AuthenticationGenerator;
1313

1414
import static com.codingchili.core.configuration.CoreStrings.*;
@@ -17,6 +17,7 @@
1717
* Command executor that registers launch commands to a CommandExecutor.
1818
*/
1919
public class LauncherCommandExecutor extends DefaultCommandExecutor {
20+
private Ansi ansi = Ansi.ansi();
2021

2122
/**
2223
* constructs an instance with the default logger
@@ -74,25 +75,45 @@ public Optional<String> getCommand() {
7475
}
7576
}
7677

78+
private void printCommand(String text, String description, int align) {
79+
ansi.fgBright(Ansi.Color.CYAN)
80+
.a(String.format("\t%s", pad(text, align)))
81+
.reset()
82+
.a(description)
83+
.newline();
84+
}
85+
86+
private void printExecutorHelpText(int align) {
87+
ansi.fgBright(Ansi.Color.GREEN)
88+
.a(" HELP ")
89+
.newline()
90+
.reset();
91+
92+
printCommand("<block-name>", "deploys the services configured in the given block.", align);
93+
printCommand("<remote-name>", "deploys configured blocks on a remote host.", align);
94+
}
95+
7796
private void help() {
78-
int align = 24;
97+
final int DEFAULT_SPACING = 18;
98+
final int COMMAND_PADDING = 4;
7999

80-
for (String line : getCommandExecutorText()) {
81-
logger.log(line, Level.STARTUP);
82-
}
100+
// find the longest command and add some padding to it.
101+
int align = commands.values().stream()
102+
.map(command -> command.getName().length() + COMMAND_PADDING)
103+
.reduce(Math::max)
104+
.orElse(DEFAULT_SPACING);
83105

84-
for (Command command : commands.values()) {
85-
int space = align - command.getName().length();
106+
printExecutorHelpText(align);
86107

108+
for (Command command : commands.values()) {
87109
if (command.isVisible()) {
88-
logger.log("\t\t" + command.getName() +
89-
String.join("", Collections.nCopies(space, " ")) +
90-
command.getDescription(), Level.STARTUP);
110+
printCommand(command.getName(), command.getDescription(), align);
91111
}
92112
}
93113

94114
List<BlockRow> blocks = new ArrayList<>();
95-
logger.log("\n\t\t" + CoreStrings.CONFIGURED_BLOCKS + "\t\t" + getRemotesAvailable(), Level.WARNING);
115+
ansi.a(String.format("\n\t%s\n\n", CONFIGURED_BLOCKS));
116+
96117
settings.getBlocks().keySet()
97118
.forEach(block -> {
98119
BlockRow row = new BlockRow(block);
@@ -102,7 +123,8 @@ private void help() {
102123
.forEach(row.remotes::add);
103124
blocks.add(row);
104125
});
105-
blocks.forEach(block -> logger.log(block.toString(), Level.SPECIAL));
126+
blocks.forEach(block -> block.toAnsi(ansi, align));
127+
logger.log(ansi.toString());
106128
}
107129

108130
private class BlockRow {
@@ -113,25 +135,25 @@ private class BlockRow {
113135
this.block = block;
114136
}
115137

116-
@Override
117-
public String toString() {
118-
int align = 36;
119-
String string = "\t\t" + block + String.join("",
120-
Collections.nCopies(align - block.length(), " "));
138+
public void toAnsi(Ansi ansi, int align) {
139+
ansi.fgBright(Ansi.Color.CYAN)
140+
.a(pad("\t" + block, align))
141+
.reset();
121142

122143
for (int i = 0; i < remotes.size(); i++) {
123144
if (i == 0)
124-
string += "[";
145+
ansi.a("[");
125146

126-
string += remotes.get(i);
147+
ansi.fgBright(Ansi.Color.CYAN)
148+
.a(remotes.get(i))
149+
.reset();
127150

128151
if (i < remotes.size() - 1)
129-
string += ", ";
152+
ansi.a(", ");
130153
if (i == remotes.size() - 1)
131-
string += "]";
154+
ansi.a("]");
132155
}
133-
134-
return string;
156+
ansi.newline();
135157
}
136158
}
137159
}

core/main/java/com/codingchili/core/logging/ConsoleLogger.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.util.concurrent.*;
1010
import java.util.concurrent.atomic.AtomicBoolean;
1111
import java.util.function.Consumer;
12-
import java.util.stream.Collectors;
1312

1413
import com.codingchili.core.context.CoreContext;
1514

@@ -111,6 +110,7 @@ private void write(String line) {
111110
//AnsiConsole.out.flush();
112111
}
113112

113+
private static final int SPACES = 15;
114114
protected String parseJsonLog(JsonObject data, String event) {
115115
LogLevel level = consumeLevel(data);
116116
String message = consume(data, LOG_MESSAGE);
@@ -125,11 +125,11 @@ protected String parseJsonLog(JsonObject data, String event) {
125125
.a(consumeTimestamp(data))
126126
.reset()
127127
.a("] ")
128-
.a((hasValue(event)) ? pad(event, 15) : "")
128+
.a((hasValue(event)) ? pad(event, SPACES) : "")
129129
.a(" [");
130130

131131
level.apply(ansi)
132-
.a(pad(consume(data, LOG_SOURCE), 15))
132+
.a(pad(consume(data, LOG_SOURCE), SPACES))
133133
.reset()
134134
.a("]");
135135

@@ -199,15 +199,6 @@ private String consumeTimestamp(JsonObject data) {
199199
}
200200
}
201201

202-
private String pad(String text, int spaces) {
203-
int padding = spaces - text.length();
204-
if (padding > 0) {
205-
return text + Collections.nCopies(padding, " ").stream().collect(Collectors.joining());
206-
} else {
207-
return text;
208-
}
209-
}
210-
211202
private String compactPath(String path) {
212203
StringBuilder text = new StringBuilder();
213204

0 commit comments

Comments
 (0)