Skip to content

Commit

Permalink
Refactoring command registers, step II
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Apr 19, 2020
1 parent 6dcb288 commit 5f6a1e6
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@
import org.jline.builtins.Builtins.CommandInput;
import org.jline.builtins.Builtins.CommandMethods;
import org.jline.builtins.Completers.OptDesc;
import org.jline.builtins.Completers.OptionCompleter;
import org.jline.builtins.Options.HelpException;
import org.jline.console.CommandRegistry.CommandSession;
import org.jline.reader.Completer;
import org.jline.reader.impl.completer.ArgumentCompleter;
import org.jline.reader.impl.completer.NullCompleter;
import org.jline.reader.impl.completer.SystemCompleter;

public abstract class AbstractCommandRegistry {
Expand All @@ -36,7 +40,7 @@ public <T extends Enum<T>> void registerCommands(Map<T,String> commandName, Map
}

public void registerCommands(Map<String,CommandMethods> commandExecute) {
cmdRegistry = new StringCmdRegistry(commandExecute);
cmdRegistry = new NameCmdRegistry(commandExecute);
}

public List<String> commandInfo(String command) {
Expand Down Expand Up @@ -90,6 +94,16 @@ public List<OptDesc> commandOptions(String command) {
return null;
}

public List<Completer> defaultCompleter(String command) {
List<Completer> completers = new ArrayList<>();
completers.add(new ArgumentCompleter(NullCompleter.INSTANCE
, new OptionCompleter(NullCompleter.INSTANCE
, this::commandOptions
, 1)
));
return completers;
}

public Object execute(CommandRegistry.CommandSession session, String command, String[] args) throws Exception {
exception = null;
getCommandMethods(command).execute().accept(new CommandInput(command, args, session));
Expand All @@ -100,14 +114,25 @@ public Object execute(CommandRegistry.CommandSession session, String command, St
}

public Object invoke(CommandSession session, String command, Object... args) throws Exception {
String[] _args = new String[args.length];
for (int i = 0; i < args.length; i++) {
if (!(args[i] instanceof String)) {
throw new IllegalArgumentException();
Object out = null;
exception = null;
CommandMethods methods = getCommandMethods(command);
if (methods.isConsumer()) {
String[] _args = new String[args.length];
for (int i = 0; i < args.length; i++) {
if (!(args[i] instanceof String)) {
throw new IllegalArgumentException();
}
_args[i] = args[i].toString();
}
_args[i] = args[i].toString();
methods.execute().accept(new CommandInput(command, _args, session));
} else {
out = methods.executeFunction().apply(new CommandInput(command, args, session));
}
if (exception != null) {
throw exception;
}
return execute(session, command, _args);
return out;
}

public void saveException(Exception exception) {
Expand Down Expand Up @@ -242,11 +267,11 @@ public CommandMethods getCommandMethods(String command) {

}

private static class StringCmdRegistry implements CmdRegistry {
private static class NameCmdRegistry implements CmdRegistry {
private Map<String,CommandMethods> commandExecute;
private Map<String,String> aliasCommand = new HashMap<>();

public StringCmdRegistry(Map<String,CommandMethods> commandExecute) {
public NameCmdRegistry(Map<String,CommandMethods> commandExecute) {
this.commandExecute = commandExecute;
}

Expand Down
31 changes: 4 additions & 27 deletions builtins/src/main/java/org/jline/builtins/Builtins.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.jline.builtins.Builtins.CommandMethods;
import org.jline.builtins.Completers.FilesCompleter;
import org.jline.builtins.Completers.OptDesc;
import org.jline.builtins.Completers.OptionCompleter;
Expand All @@ -33,7 +31,6 @@
import org.jline.reader.impl.completer.ArgumentCompleter;
import org.jline.reader.impl.completer.NullCompleter;
import org.jline.reader.impl.completer.StringsCompleter;
import org.jline.reader.impl.completer.SystemCompleter;
import org.jline.terminal.Terminal;
import org.jline.utils.AttributedString;

Expand Down Expand Up @@ -89,11 +86,11 @@ public Builtins(Set<Command> commands, Supplier<Path> workDir, ConfigurationPath
commandExecute.put(Command.LESS, new CommandMethods(this::less, this::lessCompleter));
commandExecute.put(Command.HISTORY, new CommandMethods(this::history, this::historyCompleter));
commandExecute.put(Command.WIDGET, new CommandMethods(this::widget, this::widgetCompleter));
commandExecute.put(Command.KEYMAP, new CommandMethods(this::keymap, this::keymapCompleter));
commandExecute.put(Command.KEYMAP, new CommandMethods(this::keymap, this::defaultCompleter));
commandExecute.put(Command.SETOPT, new CommandMethods(this::setopt, this::setoptCompleter));
commandExecute.put(Command.SETVAR, new CommandMethods(this::setvar, this::setvarCompleter));
commandExecute.put(Command.UNSETOPT, new CommandMethods(this::unsetopt, this::unsetoptCompleter));
commandExecute.put(Command.TTOP, new CommandMethods(this::ttop, this::ttopCompleter));
commandExecute.put(Command.TTOP, new CommandMethods(this::ttop, this::defaultCompleter));
registerCommands(commandName, commandExecute);
}

Expand Down Expand Up @@ -256,16 +253,6 @@ private List<Completer> widgetCompleter(String name) {
return completers;
}

private List<Completer> keymapCompleter(String name) {
List<Completer> completers = new ArrayList<>();
completers.add(new ArgumentCompleter(NullCompleter.INSTANCE
, new OptionCompleter(NullCompleter.INSTANCE
, this::commandOptions
, 1)
));
return completers;
}

private List<Completer> setvarCompleter(String name) {
List<Completer> completers = new ArrayList<>();
completers.add(new ArgumentCompleter(NullCompleter.INSTANCE
Expand All @@ -287,18 +274,8 @@ private List<Completer> unsetoptCompleter(String name) {
return completers;
}

private List<Completer> ttopCompleter(String name) {
List<Completer> completers = new ArrayList<>();
completers.add(new ArgumentCompleter(NullCompleter.INSTANCE
, new OptionCompleter(NullCompleter.INSTANCE
, this::commandOptions
, 1)
));
return completers;
}

//
// utils
// Utils for helpMessage parsing
//
private static AttributedString highlightComment(String comment) {
return HelpException.highlightComment(comment, HelpException.defaultStyle());
Expand Down Expand Up @@ -503,7 +480,7 @@ public Function<String, List<Completer>> compileCompleter() {
return compileCompleter;
}

public boolean isVoid() {
public boolean isConsumer() {
return execute != null;
}
}
Expand Down
110 changes: 14 additions & 96 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.jline.builtins.Builtins;
import org.jline.builtins.Builtins.CommandMethods;
import org.jline.builtins.Completers.FilesCompleter;
import org.jline.builtins.Completers.OptDesc;
Expand All @@ -37,7 +36,6 @@
import org.jline.reader.impl.completer.ArgumentCompleter;
import org.jline.reader.impl.completer.NullCompleter;
import org.jline.reader.impl.completer.StringsCompleter;
import org.jline.reader.impl.completer.SystemCompleter;
import org.jline.terminal.Terminal;
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStringBuilder;
Expand Down Expand Up @@ -69,10 +67,6 @@ public enum Command {SHOW
private static final int PRNT_MAX_DEPTH = 1;
private static final int PRNT_INDENTION = 4;
private final ScriptEngine engine;
private Map<Command,String> commandName = new HashMap<>();
private Map<String,Command> nameCommand = new HashMap<>();
private Map<String,String> aliasCommand = new HashMap<>();
private final Map<Command,CommandMethods> commandExecute = new HashMap<>();
private Map<Class<?>, Function<Object, Map<String,Object>>> objectToMap = new HashMap<>();
private Map<Class<?>, Function<Object, String>> objectToString = new HashMap<>();
private Map<String, Function<Object, AttributedString>> highlightValue = new HashMap<>();
Expand All @@ -94,25 +88,27 @@ public ConsoleEngineImpl(ScriptEngine engine
this.engine = engine;
this.workDir = workDir;
this.configPath = configPath;
Map<Command,String> commandName = new HashMap<>();
Map<Command,CommandMethods> commandExecute = new HashMap<>();
Set<Command> cmds = new HashSet<>(EnumSet.allOf(Command.class));
for (Command c: cmds) {
commandName.put(c, c.name().toLowerCase());
}
doNameCommand();
commandExecute.put(Command.DEL, new CommandMethods(this::del, this::variableCompleter));
commandExecute.put(Command.SHOW, new CommandMethods(this::show, this::variableCompleter));
commandExecute.put(Command.PRNT, new CommandMethods(this::prnt, this::prntCompleter));
commandExecute.put(Command.SLURP, new CommandMethods(this::slurpcmd, this::slurpCompleter));
commandExecute.put(Command.ALIAS, new CommandMethods(this::aliascmd, this::aliasCompleter));
commandExecute.put(Command.UNALIAS, new CommandMethods(this::unalias, this::unaliasCompleter));
commandExecute.put(Command.PIPE, new CommandMethods(this::pipe, this::pipeCompleter));
commandExecute.put(Command.PIPE, new CommandMethods(this::pipe, this::defaultCompleter));
aliasFile = configPath.getUserConfig("aliases.json");
if (aliasFile == null) {
aliasFile = configPath.getUserConfig("aliases.json", true);
persist(aliasFile, aliases);
} else {
aliases.putAll((Map<String,String>)slurp(aliasFile));
}
registerCommands(commandName, commandExecute);
}

/**
Expand Down Expand Up @@ -166,23 +162,6 @@ public void setScriptExtension(String extension) {
this.scriptExtension = extension;
}

@Override
public Set<String> commandNames() {
return nameCommand.keySet();
}

public Map<String, String> commandAliases() {
return aliasCommand;
}

@Override
public boolean hasCommand(String name) {
if (nameCommand.containsKey(name) || aliasCommand.containsKey(name)) {
return true;
}
return false;
}

@Override
public boolean hasAlias(String name) {
return aliases.containsKey(name);
Expand Down Expand Up @@ -218,55 +197,6 @@ public List<String> getNamedPipes() {
return out;
}

private void doNameCommand() {
nameCommand = commandName.entrySet()
.stream()
.collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
}

private Command command(String name) {
Command out = null;
if (!hasCommand(name)) {
throw new IllegalArgumentException("Command does not exists!");
}
if (aliasCommand.containsKey(name)) {
name = aliasCommand.get(name);
}
if (nameCommand.containsKey(name)) {
out = nameCommand.get(name);
} else {
throw new IllegalArgumentException("Command does not exists!");
}
return out;
}

public void rename(Command command, String newName) {
if (nameCommand.containsKey(newName)) {
throw new IllegalArgumentException("Duplicate command name!");
} else if (!commandName.containsKey(command)) {
throw new IllegalArgumentException("Command does not exists!");
}
commandName.put(command, newName);
doNameCommand();
}

public void alias(String alias, String command) {
if (!nameCommand.keySet().contains(command)) {
throw new IllegalArgumentException("Command does not exists!");
}
aliasCommand.put(alias, command);
}

@Override
public SystemCompleter compileCompleters() {
SystemCompleter out = new SystemCompleter();
for (Map.Entry<Command, String> entry: commandName.entrySet()) {
out.add(entry.getValue(), commandExecute.get(entry.getKey()).compileCompleter().apply(entry.getValue()));
}
out.addAliases(aliasCommand);
return out;
}

private Set<String> variables() {
return engine.find().keySet();
}
Expand Down Expand Up @@ -894,7 +824,7 @@ public Object invoke(CommandRegistry.CommandSession session, String command, Obj
exception = null;
Object out = null;
if (hasCommand(command)) {
out = commandExecute.get(command(command)).executeFunction().apply(new Builtins.CommandInput(command, args, session));
out = getCommandMethods(command).executeFunction().apply(new Builtins.CommandInput(command, args, session));
} else {
String[] _args = new String[args.length];
for (int i = 0; i < args.length; i++) {
Expand Down Expand Up @@ -1815,18 +1745,6 @@ private Object pipe(Builtins.CommandInput input) {
return out;
}

@Override
public List<OptDesc> commandOptions(String command) {
try {
invoke(new CommandSession(), command, "--help");
} catch (HelpException e) {
return Builtins.compileCommandOptions(e.getMessage());
} catch (Exception e) {
trace(e);
}
return null;
}

private List<Completer> slurpCompleter(String command) {
List<Completer> completers = new ArrayList<>();
List<OptDesc> optDescs = commandOptions("slurp");
Expand Down Expand Up @@ -1895,25 +1813,25 @@ public void complete(LineReader reader, ParsedLine commandLine, List<Candidate>

private List<Completer> aliasCompleter(String command) {
List<Completer> completers = new ArrayList<>();
List<Completer> params = new ArrayList<>();
params.add(new StringsCompleter(aliases::keySet));
params.add(new AliasValueCompleter(aliases));
completers.add(new ArgumentCompleter(NullCompleter.INSTANCE
, new StringsCompleter(aliases::keySet), new AliasValueCompleter(aliases), NullCompleter.INSTANCE));
, new OptionCompleter(params
, this::commandOptions
, 1)
));
return completers;
}

private List<Completer> unaliasCompleter(String command) {
List<Completer> completers = new ArrayList<>();
completers.add(new ArgumentCompleter(NullCompleter.INSTANCE, new StringsCompleter(aliases::keySet)));
return completers;
}

private List<Completer> pipeCompleter(String command) {
List<Completer> completers = new ArrayList<>();
completers.add(new ArgumentCompleter(NullCompleter.INSTANCE
, new OptionCompleter(NullCompleter.INSTANCE
, new OptionCompleter(new StringsCompleter(aliases::keySet)
, this::commandOptions
, 1)
));
return completers;
return completers;
}

}

0 comments on commit 5f6a1e6

Please sign in to comment.