diff --git a/builtins/pom.xml b/builtins/pom.xml index 69c4b90e0..c0e60e6af 100644 --- a/builtins/pom.xml +++ b/builtins/pom.xml @@ -27,6 +27,10 @@ org.jline jline-reader + + org.jline + jline-style + com.googlecode.juniversalchardet diff --git a/builtins/src/main/java/org/jline/builtins/Commands.java b/builtins/src/main/java/org/jline/builtins/Commands.java index 9fb63d7c5..8fd3fad03 100644 --- a/builtins/src/main/java/org/jline/builtins/Commands.java +++ b/builtins/src/main/java/org/jline/builtins/Commands.java @@ -35,6 +35,7 @@ import org.jline.builtins.Completers.CompletionData; import org.jline.builtins.Options; +import org.jline.builtins.Options.HelpException; import org.jline.builtins.Source.StdInSource; import org.jline.builtins.Source.URLSource; import org.jline.keymap.KeyMap; @@ -64,8 +65,7 @@ public static void tmux(Terminal terminal, PrintStream out, PrintStream err, }; Options opt = Options.compile(usage).parse(argv); if (opt.isSet("help")) { - opt.usage(err); - return; + throw new HelpException(opt.usage()); } // Tmux with no args if (argv.length == 0) { @@ -101,8 +101,7 @@ public static void nano(Terminal terminal, PrintStream out, PrintStream err, }; Options opt = Options.compile(usage).parse(argv); if (opt.isSet("help")) { - opt.usage(err); - return; + throw new HelpException(opt.usage()); } Nano edit = new Nano(terminal, currentDir); edit.open(opt.args()); @@ -111,7 +110,7 @@ public static void nano(Terminal terminal, PrintStream out, PrintStream err, public static void less(Terminal terminal, InputStream in, PrintStream out, PrintStream err, Path currentDir, - String[] argv) throws IOException, InterruptedException { + String[] argv) throws Exception { final String[] usage = { "less - file pager", "Usage: less [OPTIONS] [FILES]", @@ -130,8 +129,7 @@ public static void less(Terminal terminal, InputStream in, PrintStream out, Prin Options opt = Options.compile(usage).parse(argv); if (opt.isSet("help")) { - opt.usage(err); - return; + throw new HelpException(opt.usage()); } Less less = new Less(terminal); @@ -161,7 +159,7 @@ public static void less(Terminal terminal, InputStream in, PrintStream out, Prin } public static void history(LineReader reader, PrintStream out, PrintStream err, - String[] argv) throws IOException, IllegalArgumentException { + String[] argv) throws Exception { final String[] usage = { "history - list history of commands", "Usage: history [-dnrfEi] [-m match] [first] [last]", @@ -174,24 +172,23 @@ public static void history(LineReader reader, PrintStream out, PrintStream err, " -m match If option -m is present the first argument is taken as a pattern", " and only the history events matching the pattern will be shown", " -d Print timestamps for each event", - " -f Print full time-date stamps in the US format", - " -E Print full time-date stamps in the European format", - " -i Print full time-date stamps in ISO8601 format", + " -f Print full time date stamps in the US format", + " -E Print full time date stamps in the European format", + " -i Print full time date stamps in ISO8601 format", " -n Suppresses command numbers", " -r Reverses the order of the commands", " -A Appends the history out to the given file", " -R Reads the history from the given file", " -W Writes the history out to the given file", " -I If added to -R, only the events that are not contained within the internal list are added", - " If added to -W/A, only the events that are new since the last incremental operation to", - " the file are added", + " If added to -W or -A, only the events that are new since the last incremental operation", + " to the file are added", " [first] [last] These optional arguments are numbers. A negative number is", " used as an offset to the current history event number"}; Options opt = Options.compile(usage).parse(argv); if (opt.isSet("help")) { - opt.usage(err); - return; + throw new HelpException(opt.usage()); } History history = reader.getHistory(); boolean done = true; @@ -298,7 +295,7 @@ private static int parseInteger(String s) throws IllegalArgumentException { public static void complete(LineReader reader, PrintStream out, PrintStream err, Map> completions, - String[] argv) { + String[] argv) throws HelpException { final String[] usage = { "complete - edit command specific tab-completions", "Usage: complete", @@ -315,8 +312,7 @@ public static void complete(LineReader reader, PrintStream out, PrintStream err, Options opt = Options.compile(usage).parse(argv); if (opt.isSet("help")) { - opt.usage(err); - return; + throw new HelpException(opt.usage()); } String command = opt.get("command"); @@ -370,8 +366,7 @@ public static void widget(LineReader reader, PrintStream out, PrintStream err, }; Options opt = Options.compile(usage).parse(argv); if (opt.isSet("help")) { - opt.usage(err); - return; + throw new HelpException(opt.usage()); } int actions = (opt.isSet("N") ? 1 : 0) @@ -435,7 +430,7 @@ else if (opt.args().size() == 1) { public static void keymap(LineReader reader, PrintStream out, PrintStream err, - String[] argv) { + String[] argv) throws HelpException { final String[] usage = { "keymap - manipulate keymaps", "Usage: keymap [options] -l [-L] [keymap ...]", @@ -466,8 +461,7 @@ public static void keymap(LineReader reader, }; Options opt = Options.compile(usage).parse(argv); if (opt.isSet("help")) { - opt.usage(err); - return; + throw new HelpException(opt.usage()); } Map> keyMaps = reader.getKeyMaps(); @@ -782,7 +776,7 @@ else if (opt.isSet("s") || opt.args().size() > 1) { public static void setopt(LineReader reader, PrintStream out, PrintStream err, - String[] argv) { + String[] argv) throws HelpException { final String[] usage = { "setopt - set options", "Usage: setopt [-m] option ...", @@ -792,8 +786,7 @@ public static void setopt(LineReader reader, }; Options opt = Options.compile(usage).parse(argv); if (opt.isSet("help")) { - opt.usage(err); - return; + throw new HelpException(opt.usage()); } if (opt.args().isEmpty()) { for (Option option : Option.values()) { @@ -811,7 +804,7 @@ public static void setopt(LineReader reader, public static void unsetopt(LineReader reader, PrintStream out, PrintStream err, - String[] argv) { + String[] argv) throws HelpException { final String[] usage = { "unsetopt - unset options", "Usage: unsetopt [-m] option ...", @@ -821,8 +814,7 @@ public static void unsetopt(LineReader reader, }; Options opt = Options.compile(usage).parse(argv); if (opt.isSet("help")) { - opt.usage(err); - return; + throw new HelpException(opt.usage()); } if (opt.args().isEmpty()) { for (Option option : Option.values()) { diff --git a/builtins/src/main/java/org/jline/builtins/Options.java b/builtins/src/main/java/org/jline/builtins/Options.java index 1675ef6e6..2f13f3951 100644 --- a/builtins/src/main/java/org/jline/builtins/Options.java +++ b/builtins/src/main/java/org/jline/builtins/Options.java @@ -38,6 +38,13 @@ import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.jline.terminal.Terminal; +import org.jline.utils.AttributedStyle; +import org.jline.utils.AttributedString; +import org.jline.utils.StyleResolver; /** * Yet another GNU long options parser. This one is configured by parsing its Usage string. @@ -220,7 +227,12 @@ public List args() { return args; } + // Added for backword compability public void usage(PrintStream err) { + err.print(usage()); + } + + public String usage() { StringBuilder buf = new StringBuilder(); int index = 0; @@ -235,9 +247,7 @@ public void usage(PrintStream err) { buf.append(NL); } - String msg = buf.toString(); - - HelpPrinter.getInstance().print(err, msg); + return buf.toString(); } /** @@ -506,44 +516,77 @@ public String toString() { } public static class HelpPrinter { - private static HelpPrinter instance = new HelpPrinter(); - + private final List names = Arrays.asList("ti", "co", "ar", "op"); private final Pattern patternCommand = Pattern.compile("(^\\s*)([a-z]+[a-z-]*){1}\\b"); private final Pattern patternArgument = Pattern.compile("(\\[|\\s|=)([A-Za-z]+[A-Za-z_-]*){1}\\b"); private final Pattern patternArgumentInComment = Pattern.compile("(\\s)([a-z]+[-]+[a-z]+|[A-Z_]{2,}){1}(\\s)"); private final Pattern patternOption = Pattern.compile("(\\s|\\[)(-\\?|[-]{1,2}[A-Za-z-]+\\b){1}"); private final String title = "Usage"; private final String ansiReset = "\033[0m"; - private String ansi4title = "\033[34;1m"; - private String ansi4command = "\033[1m"; - private String ansi4argument = "\033[3m"; - private String ansi4option = "\033[33m"; - private boolean color = false; - - private HelpPrinter() {} + private String ansi4title; + private String ansi4command; + private String ansi4argument; + private String ansi4option; + private boolean color = true; + private Terminal terminal; - public static HelpPrinter getInstance() { - return instance; + public HelpPrinter() { + this(null); } - + + public HelpPrinter(Terminal terminal) { + this.terminal = terminal; + setColors("ti=1;34:co=1:ar=3:op=33"); + } + public void setColor(boolean color) { this.color = color; } - public void setAnsi4title(String ansicode) { - this.ansi4title = ansicode; + public void setColor4title(AttributedStyle style) { + this.ansi4title = styleToAnsiCode(style); } - public void setAnsi4command(String ansicode) { - this.ansi4command = ansicode; + public void setColor4command(AttributedStyle style) { + this.ansi4command = styleToAnsiCode(style); } - public void setAnsi4argument(String ansicode) { - this.ansi4argument = ansicode; + public void setColor4argument(AttributedStyle style) { + this.ansi4argument = styleToAnsiCode(style); } - public void setAnsi4option(String ansicode) { - this.ansi4option = ansicode; + public void setColor4option(AttributedStyle style) { + this.ansi4option = styleToAnsiCode(style); + } + + public void setColors (Map colors) { + for (String n: names) { + if (colors.containsKey(n)) { + AttributedStyle s = new StyleResolver(colors::get).resolve(colors.get(n)); + if (n.equals("ti")) { + setColor4title(s); + } else if (n.equals("co")) { + setColor4command(s); + } else if (n.equals("ar")) { + setColor4argument(s); + } else if (n.equals("op")) { + setColor4option(s); + } + } + } + } + + public void setColors (String str) { + String sep = str.matches("[a-z]{2}=[0-9]*(;[0-9]+)*(:[a-z]{2}=[0-9]*(;[0-9]+)*)*") ? ":" : " "; + setColors(Arrays.stream(str.split(sep)) + .collect(Collectors.toMap(s -> s.substring(0, s.indexOf('=')), + s -> s.substring(s.indexOf('=') + 1)))); + + } + + private String styleToAnsiCode(AttributedStyle style) { + String[] as = new AttributedString("HP", style).toAnsi(terminal).split("HP"); + return as.length > 0 ? as[0] : ansiReset; } public void print(PrintStream err, String msg) { @@ -603,4 +646,11 @@ private String highlightComment(String comment) { } } + @SuppressWarnings("serial") + public static class HelpException extends Exception { + public HelpException(String message) { + super(message); + } + } + } diff --git a/builtins/src/main/java/org/jline/builtins/TTop.java b/builtins/src/main/java/org/jline/builtins/TTop.java index ee77a06e9..a9a229806 100644 --- a/builtins/src/main/java/org/jline/builtins/TTop.java +++ b/builtins/src/main/java/org/jline/builtins/TTop.java @@ -8,6 +8,7 @@ */ package org.jline.builtins; +import org.jline.builtins.Options.HelpException; import org.jline.keymap.BindingReader; import org.jline.keymap.KeyMap; import org.jline.terminal.Attributes; @@ -84,8 +85,7 @@ public static void ttop(Terminal terminal, PrintStream out, PrintStream err, }; Options opt = Options.compile(usage).parse(argv); if (opt.isSet("help")) { - opt.usage(err); - return; + throw new HelpException(opt.usage()); } TTop ttop = new TTop(terminal); ttop.sort = opt.isSet("order") ? Arrays.asList(opt.get("order").split(",")) : null; diff --git a/builtins/src/main/java/org/jline/builtins/Tmux.java b/builtins/src/main/java/org/jline/builtins/Tmux.java index 97093f01e..e03469c70 100644 --- a/builtins/src/main/java/org/jline/builtins/Tmux.java +++ b/builtins/src/main/java/org/jline/builtins/Tmux.java @@ -39,6 +39,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.jline.builtins.Options.HelpException; import org.jline.keymap.BindingReader; import org.jline.keymap.KeyMap; import org.jline.reader.ParsedLine; @@ -466,7 +467,7 @@ public synchronized void execute(PrintStream out, PrintStream err, List } } - protected void setOption(PrintStream out, PrintStream err, List args) throws IOException { + protected void setOption(PrintStream out, PrintStream err, List args) throws Exception { final String[] usage = { "set-option - ", "Usage: set-option [-agosquw] option [value]", @@ -475,13 +476,11 @@ protected void setOption(PrintStream out, PrintStream err, List args) th }; Options opt = Options.compile(usage).parse(args); if (opt.isSet("help")) { - opt.usage(err); - return; + throw new HelpException(opt.usage()); } int nbargs = opt.args().size(); if (nbargs < 1 || nbargs > 2) { - opt.usage(err); - return; + throw new HelpException(opt.usage()); } String name = opt.args().get(0); String value = nbargs > 1 ? opt.args().get(1) : null; @@ -514,7 +513,7 @@ protected void setOption(PrintStream out, PrintStream err, List args) th } } - protected void bindKey(PrintStream out, PrintStream err, List args) throws IOException { + protected void bindKey(PrintStream out, PrintStream err, List args) throws Exception { final String[] usage = { "bind-key - ", "Usage: bind-key key command [arguments]", /* [-cnr] [-t mode-table] [-T key-table] */ @@ -522,13 +521,11 @@ protected void bindKey(PrintStream out, PrintStream err, List args) thro }; Options opt = Options.compile(usage).setOptionsFirst(true).parse(args); if (opt.isSet("help")) { - opt.usage(err); - return; + throw new HelpException(opt.usage()); } List vargs = opt.args(); if (vargs.size() < 2) { - opt.usage(err); - return; + throw new HelpException(opt.usage()); } String prefix = serverOptions.get(OPT_PREFIX); String key = prefix + KeyMap.translate(vargs.remove(0)); @@ -536,7 +533,7 @@ protected void bindKey(PrintStream out, PrintStream err, List args) thro keyMap.bind(vargs.toArray(new String[vargs.size()]), key); } - protected void unbindKey(PrintStream out, PrintStream err, List args) throws IOException { + protected void unbindKey(PrintStream out, PrintStream err, List args) throws Exception { final String[] usage = { "unbind-key - ", "Usage: unbind-key key", /* [-an] [-t mode-table] [-T key-table] */ @@ -544,13 +541,11 @@ protected void unbindKey(PrintStream out, PrintStream err, List args) th }; Options opt = Options.compile(usage).setOptionsFirst(true).parse(args); if (opt.isSet("help")) { - opt.usage(err); - return; + throw new HelpException(opt.usage()); } List vargs = opt.args(); if (vargs.size() != 1) { - opt.usage(err); - return; + throw new HelpException(opt.usage()); } String prefix = serverOptions.get(OPT_PREFIX); String key = prefix + KeyMap.translate(vargs.remove(0)); @@ -558,7 +553,7 @@ protected void unbindKey(PrintStream out, PrintStream err, List args) th keyMap.bind(Binding.Discard, key); } - protected void listKeys(PrintStream out, PrintStream err, List args) throws IOException { + protected void listKeys(PrintStream out, PrintStream err, List args) throws Exception { final String[] usage = { "list-keys - ", "Usage: list-keys ", /* [-t mode-table] [-T key-table] */ @@ -566,8 +561,7 @@ protected void listKeys(PrintStream out, PrintStream err, List args) thr }; Options opt = Options.compile(usage).parse(args); if (opt.isSet("help")) { - opt.usage(err); - return; + throw new HelpException(opt.usage()); } String prefix = serverOptions.get(OPT_PREFIX); keyMap.getBoundKeys().entrySet().stream() @@ -594,7 +588,7 @@ protected void listKeys(PrintStream out, PrintStream err, List args) thr .forEach(out::println); } - protected void sendKeys(PrintStream out, PrintStream err, List args) throws IOException { + protected void sendKeys(PrintStream out, PrintStream err, List args) throws Exception { final String[] usage = { "send-keys - ", "Usage: send-keys [-lXRM] [-N repeat-count] [-t target-pane] key...", @@ -604,8 +598,7 @@ protected void sendKeys(PrintStream out, PrintStream err, List args) thr }; Options opt = Options.compile(usage).parse(args); if (opt.isSet("help")) { - opt.usage(err); - return; + throw new HelpException(opt.usage()); } for (int i = 0, n = opt.getNumber("number"); i < n; i++) { for (String arg : opt.args()) { @@ -615,7 +608,7 @@ protected void sendKeys(PrintStream out, PrintStream err, List args) thr } } - protected void clockMode(PrintStream out, PrintStream err, List args) throws IOException { + protected void clockMode(PrintStream out, PrintStream err, List args) throws Exception { final String[] usage = { "clock-mode - ", "Usage: clock-mode", @@ -623,8 +616,7 @@ protected void clockMode(PrintStream out, PrintStream err, List args) th }; Options opt = Options.compile(usage).parse(args); if (opt.isSet("help")) { - opt.usage(err); - return; + throw new HelpException(opt.usage()); } active.clock = true; @@ -636,7 +628,7 @@ protected void clockMode(PrintStream out, PrintStream err, List args) th setDirty(); } - protected void displayPanes(PrintStream out, PrintStream err, List args) throws IOException { + protected void displayPanes(PrintStream out, PrintStream err, List args) throws Exception { final String[] usage = { "display-panes - ", "Usage: display-panes", @@ -644,8 +636,7 @@ protected void displayPanes(PrintStream out, PrintStream err, List args) }; Options opt = Options.compile(usage).parse(args); if (opt.isSet("help")) { - opt.usage(err); - return; + throw new HelpException(opt.usage()); } identify = true; setDirty(); @@ -655,7 +646,7 @@ protected void displayPanes(PrintStream out, PrintStream err, List args) }, 1, TimeUnit.SECONDS); } - protected void resizePane(PrintStream out, PrintStream err, List args) throws IOException { + protected void resizePane(PrintStream out, PrintStream err, List args) throws Exception { final String[] usage = { "resize-pane - ", "Usage: resize-pane [-UDLR] [-x width] [-y height] [-t target-pane] [adjustment]", @@ -669,8 +660,7 @@ protected void resizePane(PrintStream out, PrintStream err, List args) t }; Options opt = Options.compile(usage).parse(args); if (opt.isSet("help")) { - opt.usage(err); - return; + throw new HelpException(opt.usage()); } int adjust; if (opt.args().size() == 0) { @@ -678,8 +668,7 @@ protected void resizePane(PrintStream out, PrintStream err, List args) t } else if (opt.args().size() == 1) { adjust = Integer.parseInt(opt.args().get(0)); } else { - opt.usage(err); - return; + throw new HelpException(opt.usage()); } if (opt.isSet("width")) { int x = opt.getNumber("width"); @@ -701,7 +690,7 @@ protected void resizePane(PrintStream out, PrintStream err, List args) t setDirty(); } - protected void selectPane(PrintStream out, PrintStream err, List args) throws IOException { + protected void selectPane(PrintStream out, PrintStream err, List args) throws Exception { final String[] usage = { "select-pane - ", "Usage: select-pane [-UDLR] [-t target-pane]", @@ -713,8 +702,7 @@ protected void selectPane(PrintStream out, PrintStream err, List args) t }; Options opt = Options.compile(usage).parse(args); if (opt.isSet("help")) { - opt.usage(err); - return; + throw new HelpException(opt.usage()); } VirtualConsole prevActive = active; if (opt.isSet("L")) { @@ -757,7 +745,7 @@ else if (opt.isSet("R")) { } } - protected void sendPrefix(PrintStream out, PrintStream err, List args) throws IOException { + protected void sendPrefix(PrintStream out, PrintStream err, List args) throws Exception { final String[] usage = { "send-prefix - ", "Usage: send-prefix [-2] [-t target-pane]", @@ -765,13 +753,12 @@ protected void sendPrefix(PrintStream out, PrintStream err, List args) t }; Options opt = Options.compile(usage).parse(args); if (opt.isSet("help")) { - opt.usage(err); - return; + throw new HelpException(opt.usage()); } active.getMasterInputOutput().write(serverOptions.get(OPT_PREFIX).getBytes()); } - protected void splitWindow(PrintStream out, PrintStream err, List args) throws IOException { + protected void splitWindow(PrintStream out, PrintStream err, List args) throws Exception { final String[] usage = { "split-window - ", "Usage: split-window [-bdfhvP] [-c start-directory] [-F format] [-p percentage|-l size] [-t target-pane] [command]", @@ -786,8 +773,7 @@ protected void splitWindow(PrintStream out, PrintStream err, List args) }; Options opt = Options.compile(usage).parse(args); if (opt.isSet("help")) { - opt.usage(err); - return; + throw new HelpException(opt.usage()); } Layout.Type type = opt.isSet("horizontal") ? LeftRight : TopBottom; // If we're splitting the main pane, create a parent diff --git a/builtins/src/test/java/org/jline/example/Example.java b/builtins/src/test/java/org/jline/example/Example.java index a7ae9dd18..4b750254c 100644 --- a/builtins/src/test/java/org/jline/example/Example.java +++ b/builtins/src/test/java/org/jline/example/Example.java @@ -29,6 +29,7 @@ import org.jline.builtins.Completers.CompletionData; import org.jline.builtins.Completers.TreeCompleter; import org.jline.builtins.Options.HelpPrinter; +import org.jline.builtins.Options.HelpException; import org.jline.builtins.TTop; import org.jline.keymap.KeyMap; import org.jline.reader.*; @@ -241,7 +242,9 @@ public void complete(LineReader reader, ParsedLine line, List candida } Terminal terminal = builder.build(); - HelpPrinter.getInstance().setColor(true); + HelpPrinter helpPrinter = new HelpPrinter(terminal); + helpPrinter.setColors("ti=1;34:co=1:ar=3:op=33"); +// helpPrinter.setColor4title(AttributedStyle.DEFAULT.foreground(AttributedStyle.GREEN)); LineReader reader = LineReaderBuilder.builder() .terminal(terminal) @@ -318,125 +321,89 @@ public void complete(LineReader reader, ParsedLine line, List candida } ParsedLine pl = reader.getParser().parse(line, 0); String[] argv = pl.words().subList(1, pl.words().size()).toArray(new String[0]); - if ("set".equals(pl.word())) { - if (pl.words().size() == 3) { - reader.setVariable(pl.words().get(1), pl.words().get(2)); - } - } - else if ("tput".equals(pl.word())) { - if (pl.words().size() == 2) { - Capability vcap = Capability.byName(pl.words().get(1)); - if (vcap != null) { - terminal.puts(vcap); - } else { - terminal.writer().println("Unknown capability"); + try { + if ("set".equals(pl.word())) { + if (pl.words().size() == 3) { + reader.setVariable(pl.words().get(1), pl.words().get(2)); } } - } - else if ("testkey".equals(pl.word())) { - terminal.writer().write("Input the key event(Enter to complete): "); - terminal.writer().flush(); - StringBuilder sb = new StringBuilder(); - while (true) { - int c = ((LineReaderImpl) reader).readCharacter(); - if (c == 10 || c == 13) break; - sb.append(new String(Character.toChars(c))); - } - terminal.writer().println(KeyMap.display(sb.toString())); - terminal.writer().flush(); - } - else if ("bindkey".equals(pl.word())) { - if (pl.words().size() == 1) { - StringBuilder sb = new StringBuilder(); - Map bound = reader.getKeys().getBoundKeys(); - for (Map.Entry entry : bound.entrySet()) { - sb.append("\""); - entry.getKey().chars().forEachOrdered(c -> { - if (c < 32) { - sb.append('^'); - sb.append((char) (c + 'A' - 1)); - } else { - sb.append((char) c); - } - }); - sb.append("\" "); - if (entry.getValue() instanceof Macro) { - sb.append("\""); - ((Macro) entry.getValue()).getSequence().chars().forEachOrdered(c -> { - if (c < 32) { - sb.append('^'); - sb.append((char) (c + 'A' - 1)); - } else { - sb.append((char) c); - } - }); - sb.append("\""); - } else if (entry.getValue() instanceof Reference) { - sb.append(((Reference) entry.getValue()).name().toLowerCase().replace('_', '-')); + else if ("tput".equals(pl.word())) { + if (pl.words().size() == 2) { + Capability vcap = Capability.byName(pl.words().get(1)); + if (vcap != null) { + terminal.puts(vcap); } else { - sb.append(entry.getValue().toString()); + terminal.writer().println("Unknown capability"); } - sb.append("\n"); } - terminal.writer().print(sb.toString()); + } + else if ("testkey".equals(pl.word())) { + terminal.writer().write("Input the key event(Enter to complete): "); + terminal.writer().flush(); + StringBuilder sb = new StringBuilder(); + while (true) { + int c = ((LineReaderImpl) reader).readCharacter(); + if (c == 10 || c == 13) break; + sb.append(new String(Character.toChars(c))); + } + terminal.writer().println(KeyMap.display(sb.toString())); + terminal.writer().flush(); + } + else if ("cls".equals(pl.word())) { + terminal.puts(Capability.clear_screen); terminal.flush(); - } else if (pl.words().size() == 3) { - reader.getKeys().bind( - new Reference(pl.words().get(2)), KeyMap.translate(pl.words().get(1)) - ); + } + else if ("sleep".equals(pl.word())) { + Thread.sleep(3000); + } + // + // builtin commands are added in order to test HelpPrinter class + // + else if ("tmux".equals(pl.word())) { + Commands.tmux(terminal, System.out, System.err, + null, //Supplier getter, + null, //Consumer setter, + null, //Consumer runner, + argv); + } + else if ("nano".equals(pl.word())) { + Commands.nano(terminal, System.out, System.err, + Paths.get(""), + argv); + } + else if ("less".equals(pl.word())) { + Commands.less(terminal, System.in, System.out, System.err, + Paths.get(""), + argv); + } + else if ("history".equals(pl.word())) { + Commands.history(reader, System.out, System.err, argv); + } + else if ("complete".equals(pl.word())) { + Commands.complete(reader, System.out, System.err, + null, // Map> completions, + argv); + } + else if ("widget".equals(pl.word())) { + Commands.widget(reader, System.out, System.err, + null, //Function widgetCreator, + argv); + } + else if ("keymap".equals(pl.word())) { + Commands.keymap(reader, System.out, System.err, argv); + } + else if ("setopt".equals(pl.word())) { + Commands.setopt(reader, System.out, System.err, argv); + } + else if ("unsetopt".equals(pl.word())) { + Commands.unsetopt(reader, System.out, System.err, argv); + } + else if ("ttop".equals(pl.word())) { + TTop.ttop(terminal, System.out, System.err, argv); } } - else if ("cls".equals(pl.word())) { - terminal.puts(Capability.clear_screen); - terminal.flush(); - } - else if ("sleep".equals(pl.word())) { - Thread.sleep(3000); - } - // - // builtin commands are added in order to test HelpPrinter class - // - else if ("tmux".equals(pl.word())) { - Commands.tmux(terminal, System.out, System.err, - null, //Supplier getter, - null, //Consumer setter, - null, //Consumer runner, - argv); - } - else if ("nano".equals(pl.word())) { - Commands.nano(terminal, System.out, System.err, - Paths.get(""), - argv); - } - else if ("less".equals(pl.word())) { - Commands.less(terminal, System.in, System.out, System.err, - Paths.get(""), - argv); - } - else if ("history".equals(pl.word())) { - Commands.history(reader, System.out, System.err, argv); - } - else if ("complete".equals(pl.word())) { - Commands.complete(reader, System.out, System.err, - null, // Map> completions, - argv); - } - else if ("widget".equals(pl.word())) { - Commands.widget(reader, System.out, System.err, - null, //Function widgetCreator, - argv); - } - else if ("keymap".equals(pl.word())) { - Commands.keymap(reader, System.out, System.err, argv); - } - else if ("setopt".equals(pl.word())) { - Commands.setopt(reader, System.out, System.err, argv); - } - else if ("unsetopt".equals(pl.word())) { - Commands.unsetopt(reader, System.out, System.err, argv); - } - else if ("ttop".equals(pl.word())) { - TTop.ttop(terminal, System.out, System.err, argv); + catch (HelpException e) { + helpPrinter.print(System.err, e.getMessage()); } } } diff --git a/remote-ssh/src/main/java/org/jline/builtins/ssh/Ssh.java b/remote-ssh/src/main/java/org/jline/builtins/ssh/Ssh.java index e217297d3..713f713b1 100644 --- a/remote-ssh/src/main/java/org/jline/builtins/ssh/Ssh.java +++ b/remote-ssh/src/main/java/org/jline/builtins/ssh/Ssh.java @@ -29,6 +29,7 @@ import org.apache.sshd.server.scp.ScpCommandFactory; import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory; import org.jline.builtins.Options; +import org.jline.builtins.Options.HelpException; import org.jline.reader.LineReader; import org.jline.terminal.Attributes; import org.jline.terminal.Size; @@ -124,8 +125,7 @@ public void ssh(Terminal terminal, List args = opt.args(); if (opt.isSet("help") || args.isEmpty()) { - opt.usage(stderr); - return; + throw new HelpException(opt.usage()); } String username = user; @@ -300,7 +300,7 @@ private ClientSession connectWithRetries(PrintWriter stdout, SshClient client, S return session; } - public void sshd(PrintStream stdout, PrintStream stderr, String[] argv) throws IOException { + public void sshd(PrintStream stdout, PrintStream stderr, String[] argv) throws Exception { final String[] usage = {"sshd - start an ssh server", "Usage: sshd [-i ip] [-p port] start | stop | status", " -i --ip=INTERFACE listen interface (default=127.0.0.1)", @@ -311,8 +311,7 @@ public void sshd(PrintStream stdout, PrintStream stderr, String[] argv) throws I List args = opt.args(); if (opt.isSet("help") || args.isEmpty()) { - opt.usage(stderr); - return; + throw new HelpException(opt.usage()); } String command = args.get(0); diff --git a/remote-telnet/src/main/java/org/jline/builtins/telnet/Telnet.java b/remote-telnet/src/main/java/org/jline/builtins/telnet/Telnet.java index 635880069..179b592cf 100644 --- a/remote-telnet/src/main/java/org/jline/builtins/telnet/Telnet.java +++ b/remote-telnet/src/main/java/org/jline/builtins/telnet/Telnet.java @@ -15,6 +15,7 @@ import java.util.List; import java.util.Map; +import org.jline.builtins.Options.HelpException; import org.jline.builtins.Options; import org.jline.terminal.Size; import org.jline.terminal.Terminal; @@ -48,7 +49,7 @@ public Telnet(Terminal terminal, ShellProvider provider) { this.provider = provider; } - public void telnetd(String[] argv) throws IOException { + public void telnetd(String[] argv) throws Exception { final String[] usage = {"telnetd - start simple telnet server", "Usage: telnetd [-i ip] [-p port] start | stop | status", " -i --ip=INTERFACE listen interface (default=127.0.0.1)", @@ -59,8 +60,7 @@ public void telnetd(String[] argv) throws IOException { List args = opt.args(); if (opt.isSet("help") || args.isEmpty()) { - opt.usage(System.err); - return; + throw new HelpException(opt.usage()); } String command = args.get(0);