diff options
| author | J08nY | 2018-07-02 16:55:37 +0200 |
|---|---|---|
| committer | J08nY | 2018-07-02 16:55:37 +0200 |
| commit | c8b36d3491bb2c5a3eb0632a2e1ead7d5ea9f7c8 (patch) | |
| tree | 3b833a3ac76100226724b9ff640c5d720ed45b1c /src/cz/crcs/ectester/common/cli | |
| parent | 957afe308fd9f648894076fe8e348c4a35669499 (diff) | |
| download | ECTester-c8b36d3491bb2c5a3eb0632a2e1ead7d5ea9f7c8.tar.gz ECTester-c8b36d3491bb2c5a3eb0632a2e1ead7d5ea9f7c8.tar.zst ECTester-c8b36d3491bb2c5a3eb0632a2e1ead7d5ea9f7c8.zip | |
Add support for colored output.
Diffstat (limited to 'src/cz/crcs/ectester/common/cli')
| -rw-r--r-- | src/cz/crcs/ectester/common/cli/CLITools.java | 4 | ||||
| -rw-r--r-- | src/cz/crcs/ectester/common/cli/Colors.java | 93 |
2 files changed, 95 insertions, 2 deletions
diff --git a/src/cz/crcs/ectester/common/cli/CLITools.java b/src/cz/crcs/ectester/common/cli/CLITools.java index 4aa58b0..a9d036e 100644 --- a/src/cz/crcs/ectester/common/cli/CLITools.java +++ b/src/cz/crcs/ectester/common/cli/CLITools.java @@ -22,7 +22,7 @@ public class CLITools { public static void help(String prog, String header, Options options, String footer, boolean usage) { HelpFormatter help = new HelpFormatter(); help.setOptionComparator(null); - help.printHelp(prog, header, options, footer, usage); + help.printHelp(Colors.bold(prog), header, options, footer, usage); } private static void help(HelpFormatter help, PrintWriter pw, CommandLineParser cli, Options opts, int depth) { @@ -97,7 +97,7 @@ public class CLITools { StringWriter uw = new StringWriter(); PrintWriter upw = new PrintWriter(uw); usage(help, upw, baseParser, baseOpts); - pw.print("usage: " + prog); + pw.print("usage: " + Colors.bold(prog)); help.printWrapped(pw, HelpFormatter.DEFAULT_WIDTH, uw.toString()); upw.close(); pw.println(); diff --git a/src/cz/crcs/ectester/common/cli/Colors.java b/src/cz/crcs/ectester/common/cli/Colors.java new file mode 100644 index 0000000..cdc42e8 --- /dev/null +++ b/src/cz/crcs/ectester/common/cli/Colors.java @@ -0,0 +1,93 @@ +package cz.crcs.ectester.common.cli; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +/** + * @author Diogo Nunes + * @author Jan Jancar johny@neuromancer.sk + * Adapted from https://github.com/dialex/JCDP/ + */ +public class Colors { + public static boolean enabled = false; + + public interface ANSIParam { + } + + public enum Foreground implements ANSIParam { + BLACK("30"), RED("31"), GREEN("32"), YELLOW("33"), BLUE("34"), MAGENTA("35"), CYAN("36"), WHITE("37"), NONE(""); + private final String code; + + Foreground(String code) { + this.code = code; + } + + @Override + public String toString() { + return code; + } + } + + public enum Background implements ANSIParam { + BLACK("40"), RED("41"), GREEN("42"), YELLOW("43"), BLUE("44"), MAGENTA("45"), CYAN("46"), WHITE("47"), NONE(""); + private final String code; + + Background(String code) { + this.code = code; + } + + @Override + public String toString() { + return code; + } + } + + public enum Attribute implements ANSIParam { + CLEAR("0"), BOLD("1"), LIGHT("1"), DARK("2"), UNDERLINE("4"), REVERSE("7"), HIDDEN("8"), NONE(""); + private final String code; + + Attribute(String code) { + this.code = code; + } + + @Override + public String toString() { + return code; + } + } + + private static final String PREFIX = "\033["; + private static final String SEPARATOR = ";"; + private static final String POSTFIX = "m"; + + public static String colored(String text, ANSIParam... params) { + if (!enabled) { + return text; + } + Optional<Foreground> fg = Arrays.stream(params).filter(Foreground.class::isInstance).map(Foreground.class::cast).findFirst(); + Optional<Background> bg = Arrays.stream(params).filter(Background.class::isInstance).map(Background.class::cast).findFirst(); + List<Attribute> attr = Arrays.stream(params).filter(Attribute.class::isInstance).distinct().map(Attribute.class::cast).collect(Collectors.toList()); + + List<ANSIParam> apply = new LinkedList<>(); + apply.addAll(attr); + fg.ifPresent(apply::add); + bg.ifPresent(apply::add); + List<String> codes = apply.stream().map(Object::toString).collect(Collectors.toList()); + return PREFIX + String.join(SEPARATOR, codes) + POSTFIX + text + PREFIX + Attribute.CLEAR + POSTFIX; + } + + public static String error(String text) { + return colored(text, Foreground.RED, Attribute.BOLD); + } + + public static String ok(String text) { + return colored(text, Foreground.GREEN, Attribute.BOLD); + } + + public static String bold(String text) { + return colored(text, Attribute.BOLD); + } +}
\ No newline at end of file |
