diff options
author | Mattias Andrée <maandree@operamail.com> | 2013-08-18 14:42:36 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2013-08-18 14:42:36 +0200 |
commit | 58c69a3ae04adc5be632123af3e6476b704f73fb (patch) | |
tree | 6bf71d7275702bb32e04e26a80da49127f87123a /src | |
parent | use colour if not pipe or use settings, on help in python version (diff) | |
download | argparser-58c69a3ae04adc5be632123af3e6476b704f73fb.tar.gz argparser-58c69a3ae04adc5be632123af3e6476b704f73fb.tar.bz2 argparser-58c69a3ae04adc5be632123af3e6476b704f73fb.tar.xz |
implement colour settings in java version
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/Test.java | 2 | ||||
-rw-r--r-- | src/argparser/ArgParser.java | 73 |
2 files changed, 62 insertions, 13 deletions
diff --git a/src/Test.java b/src/Test.java index bab6092..27473ab 100644 --- a/src/Test.java +++ b/src/Test.java @@ -39,7 +39,7 @@ public class Test "GNU Affero General Public License for more details.\n" + "\n" + "You should have received a copy of the GNU Affero General Public License\n" + - "along with this library. If not, see <http://www.gnu.org/licenses/>.", null, true); + "along with this library. If not, see <http://www.gnu.org/licenses/>.", null, false); parser.add(new ArgParser.Argumentless(0, "-h", "-?", "--help"), "Prints this help message\n(and exits)"); parser.add(new ArgParser.Argumentless(0, "--hello"), "Prints the text: hello world"); diff --git a/src/argparser/ArgParser.java b/src/argparser/ArgParser.java index a786f23..a9276dc 100644 --- a/src/argparser/ArgParser.java +++ b/src/argparser/ArgParser.java @@ -121,6 +121,7 @@ public class ArgParser this.usage = usage; this.longDescription = longDescription; this.out = useStderr ? System.err : System.out; + this.outFile = useStderr ? "/dev/stderr" : "/dev/stdout"; } @@ -156,6 +157,11 @@ public class ArgParser private final OutputStream out; /** + * The error output file + */ + private final String outFile; + + /** * The passed arguments */ public String[] arguments = null; @@ -857,20 +863,63 @@ public class ArgParser /** - * Prints a colourful help message + * Prints a help message */ public void help() { + boolean useColours = false; + try + { final String file = (new File(this.outFile)).getCanonicalPath(); + useColours = file.startsWith("/dev/tty") || file.startsWith("/dev/pts/"); + } + catch (final Throwable ignore) + { /* ignore */ + } + help(useColours); + } + + + /** + * Prints a help message + * + * @param useColours Whether to print colours, {@code null} is not piped + */ + public void help(final Boolean useColours) + { + if (useColours == null) + help(); + else + help(useColours.booleanValue()); + } + + + /** + * Prints a help message + * + * @param useColours Whether to print colours + */ + public void help(final boolean useColours) + { final String dash = this.linuxvt ? "-" : "—"; - this.println("\033[01m" + program + "\033[21m " + dash + " " + this.description + "\n", false); + this.println((useColours ? ("\033[01m" + this.program + "\033[21m ") : (this.program + " ")) + dash + " " + this.description + "\n", false); if (this.longDescription != null) - this.println(longDescription, false); + { + String desc = this.longDescription; + if (useColours == false) + for (int esc; (esc = desc.indexOf('\033')) >= 0;) + desc = desc.substring(0, esc) + desc.substring(desc.indexOf('m', esc) + 1); + this.println(desc, false); + } this.println(false); if (this.usage != null) - { this.print("\033[01mUSAGE:\033[21m"); + { this.print(useColours ? "\033[01mUSAGE:\033[21m" : "USAGE:"); boolean first = true; - for (final String line : this.usage.split("\n")) + String desc = this.usage; + if (useColours == false) + for (int esc; (esc = desc.indexOf('\033')) >= 0;) + desc = desc.substring(0, esc) + desc.substring(desc.indexOf('m', esc) + 1); + for (final String line : desc.split("\n")) { if (first) first = false; else @@ -893,7 +942,7 @@ public class ArgParser empty += empty; empty = empty.substring(0, maxfirstlen); - this.println("\033[01mSYNOPSIS:\033[21m", false); + this.println(useColours ? "\033[01mSYNOPSIS:\033[21m" : "SYNOPSIS:", false); final ArrayList<String> lines = new ArrayList<String>(); final ArrayList<int[]> lens = new ArrayList<int[]>(); for (final Option opt : this.options) @@ -906,14 +955,14 @@ public class ArgParser first = empty; else first += empty.substring(first.length()); - String line = " \033[02m" + first + "\033[22m \0" + last; + String line = (useColours ? (" \033[02m" + first + "\033[22m \0") : (" " + first + " \0")) + last; l += first.length() + 6 + last.length(); if (opt.getClass() == Variadic.class) - { line += " [\033[04m" + opt.argument + "\033[24m...]"; + { line += useColours ? (" [\033[04m" + opt.argument + "\033[24m...]") : (" [" + opt.argument + "...]"); l += opt.argument.length() + 6; } else if (opt.getClass() == Argumented.class) - { line += " \033[04m" + opt.argument + "\033[24m"; + { line += useColours ? (" \033[04m" + opt.argument + "\033[24m") : (" " + opt.argument); l += opt.argument.length() + 1; } lines.add(line); @@ -936,17 +985,17 @@ public class ArgParser continue; boolean first = true; final String colour = (index & 1) == 0 ? "36" : "34"; - { String line = lines.get(index).replace("\0", "\033[" + colour + ";01m"); + { String line = lines.get(index).replace("\0", useColours ? ("\033[" + colour + ";01m") : ""); line += empty.substring(lens.get(index)[0]); this.print(line, false); } for (final String line : opt.help.split("\n")) if (first) { first = false; - this.print(line + "\033[00m\n"); + this.print(line + (useColours ? "\033[00m\n" : "\n")); } else - this.print(empty + "\033[" + colour + "m" + line + "\033[00m\n"); + this.print(empty + (useColours ? ("\033[" + colour + "m" + line + "\033[00m\n") : (line + "\n"))); index++; } |