Skip to content

Commit

Permalink
Merge pull request #511 from mattirn/fun-pipes
Browse files Browse the repository at this point in the history
Custom pipes
  • Loading branch information
mattirn authored Feb 11, 2020
2 parents b471f94 + 96c2801 commit 1354c23
Show file tree
Hide file tree
Showing 8 changed files with 475 additions and 99 deletions.
11 changes: 11 additions & 0 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ static String plainCommand(String command) {
*/
String getAlias(String name);

/**
* Returns defined pipes
* @return map of defined pipes
*/
Map<String,List<String>> getPipes();

/**
* Returns script and variable completers
* @return script and variable completers
Expand Down Expand Up @@ -127,6 +133,11 @@ default Object execute(File script) throws Exception {
*/
Object postProcess(String line, Object result, String output);

/**
* @param object object to print
*/
void trace(Object object);

/**
* Print object.
* @param object object to print
Expand Down
96 changes: 86 additions & 10 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public enum Command {SHOW
, DEL
, PRNT
, ALIAS
, PIPE
, UNALIAS
, SLURP};
private static final String VAR_CONSOLE_OPTIONS = "CONSOLE_OPTIONS";
Expand All @@ -70,6 +71,7 @@ public enum Command {SHOW
private final Supplier<Path> workDir;
private final ConfigurationPath configPath;
private final Map<String, String> aliases = new HashMap<>();
private final Map<String, List<String>> pipes = new HashMap<>();
private Path aliasFile;
private LineReader reader;
private boolean executing = false;
Expand All @@ -91,6 +93,7 @@ public ConsoleEngineImpl(ScriptEngine engine
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));
aliasFile = configPath.getUserConfig("aliases.json");
if (aliasFile == null) {
aliasFile = configPath.getUserConfig("aliases.json", true);
Expand Down Expand Up @@ -154,6 +157,11 @@ public String getAlias(String name) {
return aliases.getOrDefault(name, null);
}

@Override
public Map<String,List<String>> getPipes() {
return pipes;
}

private void doNameCommand() {
nameCommand = commandName.entrySet()
.stream()
Expand Down Expand Up @@ -241,7 +249,7 @@ public List<String> scripts() {
out.add(name.substring(0, name.lastIndexOf(".")));
}
} catch (Exception e) {
systemRegistry.println(e);
trace(e);
}
return out;
}
Expand All @@ -262,7 +270,7 @@ public Object[] expandParameters(String[] args) throws Exception {
}

private String expandName(String name) {
String regexVar = "[a-zA-Z]{1,}[a-zA-Z0-9_-]*";
String regexVar = "[a-zA-Z_]{1,}[a-zA-Z0-9_-]*";
String out = name;
if (name.matches("^\\$" + regexVar)) {
out = name.substring(1);
Expand Down Expand Up @@ -614,7 +622,7 @@ public boolean executeWidget(Object function) {
}
engine.execute("_widgetFunction()");
} catch (Exception e) {
systemRegistry.println(e);
trace(e);
return false;
}
return true;
Expand All @@ -628,7 +636,7 @@ private boolean splitCommandOutput() {
out = (boolean) ((Map<String, Object>) engine.get(VAR_CONSOLE_OPTIONS)).getOrDefault("splitOutput", true);
}
} catch (Exception e) {
systemRegistry.println(new Exception("Bad CONSOLE_OPTION value: " + e.getMessage()));
trace(new Exception("Bad CONSOLE_OPTION value: " + e.getMessage()));
}
return out;
}
Expand Down Expand Up @@ -697,6 +705,30 @@ private Map<String,Object> defaultPrntOptions() {
return out;
}

@SuppressWarnings("unchecked")
@Override
public void trace(final Object object) {
Object toPrint = object;
int level = 0;
if (engine.hasVariable(VAR_CONSOLE_OPTIONS)) {
level = (int) ((Map<String, Object>) engine.get(VAR_CONSOLE_OPTIONS)).getOrDefault("trace", 0);
}
Map<String, Object> options = new HashMap<>();
if (level < 2) {
options.put("exception", "message");
}
if (level == 0) {
if (!(object instanceof Exception)) {
toPrint = null;
}
} else if (level == 1) {
if (object instanceof SystemRegistryImpl.CommandData) {
toPrint = ((SystemRegistryImpl.CommandData)object).rawLine();
}
}
println(options, toPrint);
}

@Override
public void println(Object object) {
Map<String,Object> options = defaultPrntOptions();
Expand Down Expand Up @@ -747,11 +779,11 @@ private void highlight(int width, String style, String object) {
: null;
for (String s: object.split("\\r?\\n")) {
AttributedStringBuilder asb = new AttributedStringBuilder();
asb.append(s).subSequence(0, width); // setLength(width) fill nul-chars at the end of line
asb.append(s);
if (highlighter != null) {
highlighter.highlight(asb).println(terminal());
} else {
asb.toAttributedString().println(terminal());
asb.subSequence(0, width).println(terminal());
}
}
}
Expand Down Expand Up @@ -900,9 +932,7 @@ private Object unalias(Builtins.CommandInput input) {
Object out = null;
try {
for (String a : opt.args()) {
if (aliases.containsKey(a)) {
aliases.remove(a);
}
aliases.remove(a);
}
} catch (Exception e) {
exception = e;
Expand All @@ -912,13 +942,49 @@ private Object unalias(Builtins.CommandInput input) {
return out;
}

private Object pipe(Builtins.CommandInput input) {
final String[] usage = {
"pipe - create/delete pipe operator",
"Usage: pipe [OPERATOR] [PREFIX] [POSTFIX]",
" pipe --list",
" pipe --delete [OPERATOR...]",
" -? --help Displays command help",
" -d --delete Delete pipe operator",
" -l --list List pipe operators",
};
Options opt = Options.compile(usage).parse(input.args());
if (opt.isSet("help")) {
exception = new HelpException(opt.usage());
return null;
}
Object out = null;
if (opt.isSet("delete")) {
for (String p : opt.args()) {
pipes.remove(p);
}
} else if (opt.isSet("list") || opt.args().size() == 0) {
out = pipes;
} else if (opt.args().size() != 3) {
exception = new IllegalArgumentException("Bad number of arguments!");
} else if (opt.args().get(0).equals(SystemRegistryImpl.PIPE_FLIP)
|| opt.args().get(0).equals(SystemRegistryImpl.PIPE_NAMED)) {
exception = new IllegalArgumentException("Reserved pipe operator");
} else {
List<String> fixes = new ArrayList<>();
fixes.add(opt.args().get(1));
fixes.add(opt.args().get(2));
pipes.put(opt.args().get(0), fixes);
}
return out;
}

private List<OptDesc> commandOptions(String command) {
try {
invoke(new CommandSession(), command, "--help");
} catch (HelpException e) {
return Builtins.compileCommandOptions(e.getMessage());
} catch (Exception e) {
e.printStackTrace();
trace(e);
}
return null;
}
Expand Down Expand Up @@ -995,4 +1061,14 @@ private List<Completer> unaliasCompleter(String command) {
return completers;
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public interface SystemRegistry extends CommandRegistry {
* Print exception on terminal
* @param exception exception to print on terminal
*/
void println(Exception exception);
void trace(Exception exception);

/**
* @return terminal
Expand Down
Loading

0 comments on commit 1354c23

Please sign in to comment.