diff --git a/builtins/src/main/java/org/jline/builtins/Builtins.java b/builtins/src/main/java/org/jline/builtins/Builtins.java index 3b10025c5..bcf788434 100644 --- a/builtins/src/main/java/org/jline/builtins/Builtins.java +++ b/builtins/src/main/java/org/jline/builtins/Builtins.java @@ -24,19 +24,17 @@ import java.util.function.Supplier; import java.util.stream.Collectors; -import org.jline.builtins.Commands; import org.jline.builtins.Completers.FilesCompleter; import org.jline.builtins.Completers.OptionCompleter; import org.jline.builtins.Completers.SystemCompleter; -import org.jline.builtins.TTop; import org.jline.builtins.Options.HelpException; import org.jline.builtins.Widgets.ArgDesc; import org.jline.builtins.Widgets.CmdDesc; import org.jline.reader.Completer; import org.jline.reader.ConfigurationPath; import org.jline.reader.LineReader; -import org.jline.reader.Widget; import org.jline.reader.LineReader.Option; +import org.jline.reader.Widget; import org.jline.reader.impl.completer.ArgumentCompleter; import org.jline.reader.impl.completer.NullCompleter; import org.jline.reader.impl.completer.StringsCompleter; @@ -48,7 +46,7 @@ * * @author Matti Rinta-Nikkola */ -public class Builtins { +public class Builtins implements CommandRegistry { public enum Command {NANO , LESS , HISTORY @@ -65,6 +63,7 @@ public enum Command {NANO private Map nameCommand = new HashMap<>(); private Map aliasCommand = new HashMap<>(); private final Map commandExecute = new HashMap<>(); + private Map> commandInfo = new HashMap<>(); private LineReader reader; private Exception exception; @@ -105,6 +104,21 @@ public Builtins(Set commands, Supplier workDir, ConfigurationPath commandExecute.put(Command.TTOP, new CommandMethods(this::ttop, this::ttopCompleter)); } + public Set commandNames() { + return nameCommand.keySet(); + } + + public Map commandAliases() { + return aliasCommand; + } + + public List commandInfo(String command) { + if (!commandInfo.containsKey(command(command))) { + commandOptions(command); + } + return commandInfo.get(command(command)); + } + private void doNameCommand() { nameCommand = commandName.entrySet() .stream() @@ -237,14 +251,22 @@ private Map commandOptions(String command) { try { execute(command, args); } catch (HelpException e) { + List info = new ArrayList<>(); String[] msg = e.getMessage().replaceAll("\r\n", "\n").replaceAll("\r", "\n").split("\n"); boolean start = false; + boolean first = true; for (String s: msg) { if (!start) { if (s.trim().startsWith("Usage: ")) { s = s.split("Usage:")[1]; start = true; } else { + if (first && s.contains(" - ")) { + info.add(s.substring(s.indexOf(" - ") + 3).trim()); + } else { + info.add(s.trim()); + } + first = false; continue; } } @@ -261,6 +283,7 @@ private Map commandOptions(String command) { } } } + commandInfo.put(command(command), info); } catch (Exception e) { } diff --git a/builtins/src/main/java/org/jline/builtins/CommandRegistry.java b/builtins/src/main/java/org/jline/builtins/CommandRegistry.java new file mode 100644 index 000000000..df53cc092 --- /dev/null +++ b/builtins/src/main/java/org/jline/builtins/CommandRegistry.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2002-2019, the original author or authors. + * + * This software is distributable under the BSD license. See the terms of the + * BSD license in the documentation provided with this software. + * + * https://opensource.org/licenses/BSD-3-Clause + */ +package org.jline.builtins; + +import org.jline.builtins.Completers; +import org.jline.builtins.Widgets; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +public interface CommandRegistry { + + /** + * Returns the command names known by this registry. + * @return the set of known command names, excluding aliases + */ + Set commandNames(); + + /** + * Returns a map of alias-to-command names known by this registry. + * @return a map with alias keys and command name values + */ + Map commandAliases(); + + /** + * Returns a short info about command known by this registry. + * @return a short info about command + */ + List commandInfo(String command); + + /** + * Returns whether a command with the specified name is known to this registry. + * @param command the command name to test + * @return true if the specified command is registered + */ + boolean hasCommand(String command); + + /** + * Returns a {@code SystemCompleter} that can provide detailed completion + * information for all registered commands. + * + * @return a SystemCompleter that can provide command completion for all registered commands + */ + Completers.SystemCompleter compileCompleters(); + + /** + * Returns a command description for use in the JLine Widgets framework. + * @param command name of the command whose description to return + * @return command description for JLine TailTipWidgets to be displayed + * in the terminal status bar. + */ + Widgets.CmdDesc commandDescription(String command); +} \ No newline at end of file