Skip to content

Commit

Permalink
SystemRegistry: improved help
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jan 21, 2020
1 parent e753abe commit ce13d2a
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 18 deletions.
11 changes: 9 additions & 2 deletions builtins/src/main/java/org/jline/builtins/CommandRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ static Completers.SystemCompleter compileCompleters(CommandRegistry ... commandR
return out;
}

/**
* Returns the name of this registry.
* @return name
*/
default String name() {
return this.getClass().getSimpleName();
}
/**
* Returns the command names known by this registry.
* @return the set of known command names, excluding aliases
Expand Down Expand Up @@ -86,7 +93,7 @@ static Completers.SystemCompleter compileCompleters(CommandRegistry ... commandR
Widgets.CmdDesc commandDescription(String command);

/**
* Execute a command that have only string parameters and options. Implementation of the method is required
* Execute a command that have only string parameters and options. Implementation of the method is required
* when aggregating command registries using SystemRegistry.
* @param command
* @param args
Expand All @@ -98,7 +105,7 @@ default Object execute(String command, String[] args) throws Exception {
}

/**
* Execute a command. If command has other than string parameters a custom implementation is required.
* Execute a command. If command has other than string parameters a custom implementation is required.
* This method will be called only when we have ConsoleEngine in SystemRegistry.
* @param command
* @param args
Expand Down
111 changes: 95 additions & 16 deletions builtins/src/main/java/org/jline/builtins/SystemRegistryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@

import org.jline.builtins.CommandRegistry;
import org.jline.builtins.Widgets;
import org.jline.builtins.Options.HelpException;
import org.jline.reader.ParsedLine;
import org.jline.reader.Parser;
import org.jline.terminal.Terminal;
import org.jline.utils.AttributedStringBuilder;

/**
Expand All @@ -23,8 +25,10 @@
* @author <a href="mailto:matti.rintanikkola@gmail.com">Matti Rinta-Nikkola</a>
*/
public class SystemRegistryImpl implements SystemRegistry {
private static final Class<?>[] BUILTIN_REGISTERIES = {Builtins.class, ConsoleEngineImpl.class};
private final CommandRegistry[] commandRegistries;
private Integer consoleId = null;
private Terminal terminal;

public SystemRegistryImpl(CommandRegistry... commandRegistries) {
this.commandRegistries = commandRegistries;
Expand All @@ -43,6 +47,10 @@ public SystemRegistryImpl(CommandRegistry... commandRegistries) {
SystemRegistry.add(this);
}

public void setTerminal(Terminal terminal) {
this.terminal = terminal;
}

@Override
public void initialize(File script) {
if (consoleId != null) {
Expand Down Expand Up @@ -142,26 +150,97 @@ private ConsoleEngine consoleEngine() {
return consoleId != null ? (ConsoleEngine) commandRegistries[consoleId] : null;
}

private void help() {
System.out.println("List of available commands:");
for (CommandRegistry r : commandRegistries) {
TreeSet<String> commands = new TreeSet<>(r.commandNames());
AttributedStringBuilder asb = new AttributedStringBuilder().tabs(2);
private boolean isBuiltinRegistry(CommandRegistry registry) {
for (Class<?> c : BUILTIN_REGISTERIES) {
if (c == registry.getClass()) {
return true;
}
}
return false;
}

private void printHeader(String header) {
AttributedStringBuilder asb = new AttributedStringBuilder().tabs(2);
asb.append("\t");
asb.append(header, HelpException.defaultStyle().resolve(".ti"));
asb.append(":");
asb.toAttributedString().println(terminal);
}

private void printCommandInfo(String command, String info, int max) {
AttributedStringBuilder asb = new AttributedStringBuilder().tabs(Arrays.asList(4, max + 4));
asb.append("\t");
asb.append(command, HelpException.defaultStyle().resolve(".co"));
asb.append("\t");
asb.append(info);
asb.setLength(terminal.getWidth());
asb.toAttributedString().println(terminal);
}

private void printCommands(Collection<String> commands, int max) {
AttributedStringBuilder asb = new AttributedStringBuilder().tabs(Arrays.asList(4, max + 4));
int col = 0;
asb.append("\t");
col += 4;
boolean done = false;
for (String c : commands) {
asb.append(c, HelpException.defaultStyle().resolve(".co"));
asb.append("\t");
asb.append(r.getClass().getSimpleName());
asb.append(":");
System.out.println(asb.toString());
for (String c : commands) {
asb = new AttributedStringBuilder().tabs(Arrays.asList(4, 20));
col += max;
if (col + max > terminal.getWidth()) {
asb.toAttributedString().println(terminal);
asb = new AttributedStringBuilder().tabs(Arrays.asList(4, max + 4));
col = 0;
asb.append("\t");
asb.append(c);
asb.append("\t");
asb.append(r.commandInfo(c).size() > 0 ? r.commandInfo(c).get(0) : " ");
System.out.println(asb.toString());
col += 4;
done = true;
} else {
done = false;
}
}
if (!done) {
asb.toAttributedString().println(terminal);
}
}

private String doCommandInfo(List<String> info) {
return info.size() > 0 ? info.get(0) : " ";
}

private void help() {
Set<String> commands = commandNames();
boolean withInfo = commands.size() < terminal.getHeight() ? true : false;
int max = Collections.max(commands, Comparator.comparing(String::length)).length() + 1;
TreeMap<String,String> builtinCommands = new TreeMap<>();
for (CommandRegistry r : commandRegistries) {
if (isBuiltinRegistry(r)) {
for (String c: r.commandNames()) {
builtinCommands.put(c, doCommandInfo(r.commandInfo(c)));
}
}
}
printHeader("Builtins");
if (withInfo) {
for (Map.Entry<String, String> entry: builtinCommands.entrySet()) {
printCommandInfo(entry.getKey(), entry.getValue(), max);
}
} else {
printCommands(builtinCommands.keySet(), max);
}
for (CommandRegistry r : commandRegistries) {
if (isBuiltinRegistry(r)) {
continue;
}
TreeSet<String> cmds = new TreeSet<>(r.commandNames());
printHeader(r.name());
if (withInfo) {
for (String c : cmds) {
printCommandInfo(c, doCommandInfo(r.commandInfo(c)), max);
}
} else {
printCommands(cmds, max);
}
}
System.out.println(" Additional help:");
System.out.println(" <command> --help");
}

private int registryId(String command) {
Expand Down
1 change: 1 addition & 0 deletions builtins/src/test/java/org/jline/example/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ public void complete(LineReader reader, ParsedLine line, List<Candidate> candida
ExampleCommands exampleCommands = new ExampleCommands();
ConsoleEngine consoleEngine = new ConsoleEngineImpl(scriptEngine, parser, terminal, ()->Paths.get(""), null);
SystemRegistryImpl systemRegistry = new SystemRegistryImpl(consoleEngine, builtins, exampleCommands);
systemRegistry.setTerminal(terminal);
// systemRegistry.initialize(new File("./example/init.jline"));
//
// Command completers
Expand Down

0 comments on commit ce13d2a

Please sign in to comment.