Skip to content

Commit

Permalink
Refactoring command registers, step III
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Apr 19, 2020
1 parent 5f6a1e6 commit 4179fbd
Show file tree
Hide file tree
Showing 10 changed files with 354 additions and 251 deletions.
101 changes: 2 additions & 99 deletions builtins/src/main/java/org/jline/builtins/Builtins.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
import org.jline.builtins.Completers.OptDesc;
import org.jline.builtins.Completers.OptionCompleter;
import org.jline.builtins.Options.HelpException;
import org.jline.console.ArgDesc;
import org.jline.console.CmdDesc;
import org.jline.console.CommandRegistry;
import org.jline.console.ConfigurationPath;
import org.jline.console.*;
import org.jline.reader.*;
import org.jline.reader.LineReader.Option;
import org.jline.reader.impl.completer.ArgumentCompleter;
Expand All @@ -39,7 +36,7 @@
*
* @author <a href="mailto:matti.rintanikkola@gmail.com">Matti Rinta-Nikkola</a>
*/
public class Builtins extends AbstractCommandRegistry implements CommandRegistry {
public class Builtins extends JlineCommandRegistry implements CommandRegistry {
public enum Command {NANO
, LESS
, HISTORY
Expand Down Expand Up @@ -391,98 +388,4 @@ public static List<String> compileCommandInfo(String helpMessage) {
return out;
}

public static class CommandInput {
String command;
String[] args;
Object[] xargs;
Terminal terminal;
InputStream in;
PrintStream out;
PrintStream err;

public CommandInput(String command, Object[] xargs, CommandRegistry.CommandSession session) {
if (xargs != null) {
this.xargs = xargs;
this.args = new String[xargs.length];
for (int i = 0; i < xargs.length; i++) {
this.args[i] = xargs[i] != null ? xargs[i].toString() : null;
}
}
this.command = command;
this.terminal = session.terminal();
this.in = session.in();
this.out = session.out();
this.err = session.err();
}

public CommandInput(String command, Object[] args, Terminal terminal, InputStream in, PrintStream out, PrintStream err) {
this(command, args, new CommandRegistry.CommandSession(terminal, in, out, err));
}

public String command() {
return command;
}

public String[] args() {
return args;
}

public Object[] xargs() {
return xargs;
}

public Terminal terminal() {
return terminal;
}

public InputStream in() {
return in;
}

public PrintStream out() {
return out;
}

public PrintStream err() {
return err;
}

public CommandRegistry.CommandSession session() {
return new CommandRegistry.CommandSession(terminal, in, out, err);
}

}

public static class CommandMethods {
Consumer<CommandInput> execute;
Function<CommandInput, Object> executeFunction;
Function<String, List<Completer>> compileCompleter;

public CommandMethods(Function<CommandInput, Object> execute, Function<String, List<Completer>> compileCompleter) {
this.executeFunction = execute;
this.compileCompleter = compileCompleter;
}

public CommandMethods(Consumer<CommandInput> execute, Function<String, List<Completer>> compileCompleter) {
this.execute = execute;
this.compileCompleter = compileCompleter;
}

public Consumer<CommandInput> execute() {
return execute;
}

public Function<CommandInput, Object> executeFunction() {
return executeFunction;
}

public Function<String, List<Completer>> compileCompleter() {
return compileCompleter;
}

public boolean isConsumer() {
return execute != null;
}
}

}
21 changes: 11 additions & 10 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
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;
import org.jline.builtins.Nano.SyntaxHighlighter;
import org.jline.builtins.Options.HelpException;
import org.jline.console.CommandInput;
import org.jline.console.CommandMethods;
import org.jline.console.CommandRegistry;
import org.jline.console.ConfigurationPath;
import org.jline.console.ScriptEngine;
Expand All @@ -47,7 +48,7 @@
*
* @author <a href="mailto:matti.rintanikkola@gmail.com">Matti Rinta-Nikkola</a>
*/
public class ConsoleEngineImpl extends AbstractCommandRegistry implements ConsoleEngine {
public class ConsoleEngineImpl extends JlineCommandRegistry implements ConsoleEngine {
public enum Command {SHOW
, DEL
, PRNT
Expand Down Expand Up @@ -824,7 +825,7 @@ public Object invoke(CommandRegistry.CommandSession session, String command, Obj
exception = null;
Object out = null;
if (hasCommand(command)) {
out = getCommandMethods(command).executeFunction().apply(new Builtins.CommandInput(command, args, session));
out = getCommandMethods(command).executeFunction().apply(new CommandInput(command, args, session));
} else {
String[] _args = new String[args.length];
for (int i = 0; i < args.length; i++) {
Expand Down Expand Up @@ -1510,7 +1511,7 @@ private void highlightMap(Map<String, Object> options
}
}

private Object show(Builtins.CommandInput input) {
private Object show(CommandInput input) {
final String[] usage = {
"show - list console variables",
"Usage: show [VARIABLE]",
Expand All @@ -1527,7 +1528,7 @@ private Object show(Builtins.CommandInput input) {
return null;
}

private Object del(Builtins.CommandInput input) {
private Object del(CommandInput input) {
final String[] usage = {
"del - delete console variables, methods, classes and imports",
"Usage: del [var1] ...",
Expand All @@ -1542,7 +1543,7 @@ private Object del(Builtins.CommandInput input) {
return null;
}

private Object prnt(Builtins.CommandInput input) {
private Object prnt(CommandInput input) {
final String[] usage = {
"prnt - print object",
"Usage: prnt [OPTIONS] object",
Expand Down Expand Up @@ -1619,7 +1620,7 @@ private Object prnt(Builtins.CommandInput input) {
return null;
}

private Object slurpcmd(Builtins.CommandInput input) {
private Object slurpcmd(CommandInput input) {
final String[] usage = {
"slurp - slurp file context to string/object",
"Usage: slurp [OPTIONS] file",
Expand Down Expand Up @@ -1656,7 +1657,7 @@ private Object slurp(Path file, Charset encoding, String format) throws IOExcept
return engine.deserialize(new String(encoded, encoding), format);
}

private Object aliascmd(Builtins.CommandInput input) {
private Object aliascmd(CommandInput input) {
final String[] usage = {
"alias - create command alias",
"Usage: alias [ALIAS] [COMMANDLINE]",
Expand Down Expand Up @@ -1688,7 +1689,7 @@ private Object aliascmd(Builtins.CommandInput input) {
return out;
}

private Object unalias(Builtins.CommandInput input) {
private Object unalias(CommandInput input) {
final String[] usage = {
"unalias - remove command alias",
"Usage: unalias [ALIAS...]",
Expand All @@ -1706,7 +1707,7 @@ private Object unalias(Builtins.CommandInput input) {
return null;
}

private Object pipe(Builtins.CommandInput input) {
private Object pipe(CommandInput input) {
final String[] usage = {
"pipe - create/delete pipe operator",
"Usage: pipe [OPERATOR] [PREFIX] [POSTFIX]",
Expand Down
110 changes: 110 additions & 0 deletions builtins/src/main/java/org/jline/builtins/JlineCommandRegistry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright (c) 2002-2020, 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 java.util.*;
import java.util.stream.Collectors;

import org.jline.console.ArgDesc;
import org.jline.console.CmdDesc;
import org.jline.console.CommandRegistry;
import org.jline.utils.AttributedString;

import org.jline.builtins.Options;
import org.jline.builtins.Completers.OptDesc;
import org.jline.builtins.Completers.OptionCompleter;
import org.jline.builtins.Options.HelpException;
import org.jline.console.AbstractCommandRegistry;
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;

/**
* CommandRegistry common methods for JLine commands that are using HelpException.
*
* @author <a href="mailto:matti.rintanikkola@gmail.com">Matti Rinta-Nikkola</a>
*/
public abstract class JlineCommandRegistry extends AbstractCommandRegistry {

public JlineCommandRegistry() {
super();
}

public List<String> commandInfo(String command) {
try {
Object[] args = {"--help"};
if (command.equals("help")) {
args = new Object[] {};
}
invoke(new CommandSession(), command, args);
} catch (HelpException e) {
return Builtins.compileCommandInfo(e.getMessage());
} catch (Exception e) {

}
throw new IllegalArgumentException("JlineCommandRegistry.commandInfo() method must be overridden in class "
+ this.getClass().getCanonicalName());
}

public CmdDesc commandDescription(String command) {
try {
if (command != null && !command.isEmpty()) {
invoke(new CommandSession(), command, new Object[] {"--help"});
} else {
List<AttributedString> main = new ArrayList<>();
Map<String, List<AttributedString>> options = new HashMap<>();
for (String c : new TreeSet<String>(commandNames())) {
for (String info : commandInfo(c)) {
main.add(HelpException.highlightSyntax(c + " - " + info, HelpException.defaultStyle(), true));
break;
}
}
return new CmdDesc(main, ArgDesc.doArgNames(Arrays.asList("")), options);
}
} catch (HelpException e) {
return Builtins.compileCommandDescription(e.getMessage());
} catch (Exception e) {

}
throw new IllegalArgumentException("JlineCommandRegistry.commandDescription() method must be overridden in class "
+ this.getClass().getCanonicalName());
}

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

}
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 Options parseOptions(String[] usage, Object[] args) throws HelpException {
Options opt = Options.compile(usage).parse(args);
if (opt.isSet("help")) {
throw new HelpException(opt.usage());
}
return opt;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
import org.jline.builtins.Completers.OptionCompleter;
import org.jline.builtins.ConsoleEngine.ExecutionResult;
import org.jline.builtins.Widgets;
import org.jline.builtins.Builtins.CommandMethods;
import org.jline.builtins.Options.HelpException;
import org.jline.console.AbstractCommandRegistry;
import org.jline.console.CmdDesc;
import org.jline.console.CommandInput;
import org.jline.console.CommandMethods;
import org.jline.console.CommandRegistry;
import org.jline.console.ConfigurationPath;
import org.jline.reader.*;
Expand Down Expand Up @@ -161,7 +163,7 @@ public Map<String, String> commandAliases() {
*/
public void register(String command, CommandRegistry subcommandRegistry) {
subcommands.put(command, subcommandRegistry);
commandExecute.put(command, new Builtins.CommandMethods(this::subcommand, this::emptyCompleter));
commandExecute.put(command, new CommandMethods(this::subcommand, this::emptyCompleter));
}

private List<String> localCommandInfo(String command) {
Expand Down Expand Up @@ -358,7 +360,7 @@ public Object localExecute(String command, Object[] args) throws Exception {
throw new IllegalArgumentException();
}
Object out = commandExecute.get(command).executeFunction()
.apply(new Builtins.CommandInput(command, args, commandSession()));
.apply(new CommandInput(command, args, commandSession()));
if (exception != null) {
throw exception;
}
Expand Down Expand Up @@ -1306,7 +1308,7 @@ private Options parseOptions(String[] usage, Object[] args) throws HelpException
return opt;
}

private Object help(Builtins.CommandInput input) {
private Object help(CommandInput input) {
final String[] usage = { "help - command help"
, "Usage: help [TOPIC...]",
" -? --help Displays command help", };
Expand Down Expand Up @@ -1400,7 +1402,7 @@ private void helpTopic(List<String> topics) {
terminal().flush();
}

private Object exit(Builtins.CommandInput input) {
private Object exit(CommandInput input) {
final String[] usage = { "exit - exit from app/script"
, "Usage: exit [OBJECT]"
, " -? --help Displays command help"
Expand Down Expand Up @@ -1449,7 +1451,7 @@ private void registryHelp(CommandRegistry registry) throws Exception {
throw new HelpException(sb.toString());
}

private Object subcommand(Builtins.CommandInput input) {
private Object subcommand(CommandInput input) {
Object out = null;
try {
if (input.args().length > 0 && subcommands.get(input.command()).hasCommand(input.args()[0])) {
Expand Down

0 comments on commit 4179fbd

Please sign in to comment.