aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/common/cli
diff options
context:
space:
mode:
authorJ08nY2018-07-29 18:34:58 +0200
committerJ08nY2018-07-29 18:34:58 +0200
commitd24630d759bb16f715564ab80a5d4447f57d03f2 (patch)
treec30699e723a8ed31ea354be7e76eb16c1b681f04 /src/cz/crcs/ectester/common/cli
parentcb6c6b8b1274fe5a340c4317a4b015ea0ef15396 (diff)
parent07d0c8947ef0d0f4c0ae01c1d8699d24a892752d (diff)
downloadECTester-d24630d759bb16f715564ab80a5d4447f57d03f2.tar.gz
ECTester-d24630d759bb16f715564ab80a5d4447f57d03f2.tar.zst
ECTester-d24630d759bb16f715564ab80a5d4447f57d03f2.zip
Merge branch 'devel'
Diffstat (limited to 'src/cz/crcs/ectester/common/cli')
-rw-r--r--src/cz/crcs/ectester/common/cli/CLITools.java7
-rw-r--r--src/cz/crcs/ectester/common/cli/Colors.java97
-rw-r--r--src/cz/crcs/ectester/common/cli/ParserOptions.java13
-rw-r--r--src/cz/crcs/ectester/common/cli/TreeParser.java2
4 files changed, 107 insertions, 12 deletions
diff --git a/src/cz/crcs/ectester/common/cli/CLITools.java b/src/cz/crcs/ectester/common/cli/CLITools.java
index 91f121f..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) {
@@ -37,7 +37,8 @@ public class CLITools {
}
tp.getParsers().forEach((key, value) -> {
pw.println();
- help.printWrapped(pw, HelpFormatter.DEFAULT_WIDTH, String.format("%" + depth + "s" + key + ":", " "));
+ String description = value.getDescription() == null ? "" : " | " + value.getDescription() + " |";
+ help.printWrapped(pw, HelpFormatter.DEFAULT_WIDTH, String.format("%" + depth + "s" + key + ":" + description, " "));
CLITools.help(help, pw, value.getParser(), value.getOptions(), depth + 1);
});
}
@@ -96,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..7601088
--- /dev/null
+++ b/src/cz/crcs/ectester/common/cli/Colors.java
@@ -0,0 +1,97 @@
+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);
+ }
+
+ public static String underline(String text) {
+ return colored(text, Attribute.UNDERLINE);
+ }
+} \ No newline at end of file
diff --git a/src/cz/crcs/ectester/common/cli/ParserOptions.java b/src/cz/crcs/ectester/common/cli/ParserOptions.java
index ee2097e..7300cbb 100644
--- a/src/cz/crcs/ectester/common/cli/ParserOptions.java
+++ b/src/cz/crcs/ectester/common/cli/ParserOptions.java
@@ -3,25 +3,22 @@ package cz.crcs.ectester.common.cli;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
-import java.util.Collections;
-import java.util.List;
-
/**
* @author Jan Jancar johny@neuromancer.sk
*/
public class ParserOptions {
private CommandLineParser parser;
private Options options;
- private List<Argument> arguments;
+ private String description;
public ParserOptions(CommandLineParser parser, Options options) {
this.parser = parser;
this.options = options;
}
- public ParserOptions(CommandLineParser parser, Options options, List<Argument> arguments) {
+ public ParserOptions(CommandLineParser parser, Options options, String description) {
this(parser, options);
- this.arguments = arguments;
+ this.description = description;
}
public CommandLineParser getParser() {
@@ -32,7 +29,7 @@ public class ParserOptions {
return options;
}
- public List<Argument> getArguments() {
- return Collections.unmodifiableList(arguments);
+ public String getDescription() {
+ return description;
}
}
diff --git a/src/cz/crcs/ectester/common/cli/TreeParser.java b/src/cz/crcs/ectester/common/cli/TreeParser.java
index f1a1980..23f59b1 100644
--- a/src/cz/crcs/ectester/common/cli/TreeParser.java
+++ b/src/cz/crcs/ectester/common/cli/TreeParser.java
@@ -82,7 +82,7 @@ public class TreeParser implements CommandLineParser {
}
} else {
if (required) {
- throw new MissingOptionException(new ArrayList(parsers.keySet()));
+ throw new MissingOptionException(new ArrayList<>(parsers.keySet()));
}
}