Skip to content

Commit

Permalink
ConsoleEngine: renamed named pipe operator to | and various improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Feb 12, 2020
1 parent bb952c5 commit 8a5513e
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 39 deletions.
47 changes: 36 additions & 11 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -654,14 +654,15 @@ private boolean splitCommandOutput() {
public Object postProcess(String line, Object result, String output) {
Object out = result;
Object _output = output != null && splitCommandOutput() ? output.split("\\r?\\n") : output;
if (parser().getVariable(line) != null && result != null) {
String consoleVar = parser().getVariable(line);
if (consoleVar != null && result != null) {
engine.put("output", _output);
}
if (systemRegistry.hasCommand(parser().getCommand(line))) {
out = postProcess(line, parser().getVariable(line) != null && result == null ? _output : result);
} else if (parser().getVariable(line) != null) {
out = postProcess(line, consoleVar != null && result == null ? _output : result);
} else if (consoleVar != null) {
if (result == null) {
engine.put(parser().getVariable(line), _output);
saveResult(consoleVar, _output);
}
out = null;
}
Expand All @@ -670,15 +671,29 @@ public Object postProcess(String line, Object result, String output) {

private Object postProcess(String line, Object result) {
Object out = result instanceof String && ((String)result).trim().length() == 0 ? null : result;
if (parser().getVariable(line) != null) {
engine.put(parser().getVariable(line), result);
String consoleVar = parser().getVariable(line);
if (consoleVar != null) {
saveResult(consoleVar, result);
out = null;
} else if (!parser().getCommand(line).equals("show") && result != null) {
engine.put("_", result);
}
return out;
}

private void saveResult(String var, Object result) {
if (var.contains(".") || var.contains("[")) {
engine.put("_executionResult", result);
try {
engine.execute(var + " = _executionResult");
} catch (Exception e) {
trace(e);
}
} else {
engine.put(var, result);
}
}

@Override
public Object invoke(CommandRegistry.CommandSession session, String command, Object... args) throws Exception {
exception = null;
Expand Down Expand Up @@ -917,6 +932,13 @@ private Object aliascmd(Builtins.CommandInput input) {
} else if (args.size() == 1) {
out = aliases.getOrDefault(args.get(0), null);
} else {
for (int i = 1; i < args.size(); i++) {
for (int j = 0; j < 10; j++) {
args.set(i, args.get(i).replaceAll("%" + j , "\\$" + j));
args.set(i, args.get(i).replaceAll("%\\{" + j + "\\}", "\\$\\{" + j + "\\}"));
args.set(i, args.get(i).replaceAll("%\\{" + j + ":-", "\\$\\{" + j + ":-"));
}
}
aliases.put(args.get(0), String.join(" ", args.subList(1, args.size())));
engine.persist(aliasFile, aliases);
}
Expand Down Expand Up @@ -957,7 +979,7 @@ private Object pipe(Builtins.CommandInput input) {
" pipe --list",
" pipe --delete [OPERATOR...]",
" -? --help Displays command help",
" -d --delete Delete pipe operator",
" -d --delete Delete pipe operators",
" -l --list List pipe operators",
};
Options opt = Options.compile(usage).parse(input.args());
Expand All @@ -967,15 +989,18 @@ private Object pipe(Builtins.CommandInput input) {
}
Object out = null;
if (opt.isSet("delete")) {
for (String p : opt.args()) {
pipes.remove(p);
if ( opt.args().size() == 1 && opt.args().get(0).equals("*")) {
pipes.clear();
} else {
for (String p: opt.args()) {
pipes.remove(p.trim());
}
}
} 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)) {
} else if (systemRegistry.getPipeNames().contains(opt.args().get(0))) {
exception = new IllegalArgumentException("Reserved pipe operator");
} else {
List<String> fixes = new ArrayList<>();
Expand Down
7 changes: 7 additions & 0 deletions builtins/src/main/java/org/jline/builtins/SystemRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.jline.builtins;

import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -36,6 +37,12 @@ public interface SystemRegistry extends CommandRegistry {
*/
void initialize(File script);

/**
*
* @return pipe names defined in systemRegistry
*/
Collection<String> getPipeNames();

/**
* Returns command completer that includes also console variable and script completion.
* @return command completer
Expand Down
53 changes: 41 additions & 12 deletions builtins/src/main/java/org/jline/builtins/SystemRegistryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ public class SystemRegistryImpl implements SystemRegistry {
public enum Command {
EXIT, HELP
};
protected final static String PIPE_FLIP = "|;";
protected final static String PIPE_NAMED = "|*";

public enum Pipe {
FLIP, NAMED
}

private static final Class<?>[] BUILTIN_REGISTERIES = { Builtins.class, ConsoleEngineImpl.class };
private CommandRegistry[] commandRegistries;
Expand All @@ -65,6 +67,7 @@ public enum Command {
private Map<Command, String> commandName = new HashMap<>();
private Map<String, Command> nameCommand = new HashMap<>();
private Map<String, String> aliasCommand = new HashMap<>();
private Map<Pipe, String> pipeName = new HashMap<>();
private final Map<Command, CommandMethods> commandExecute = new HashMap<>();
private Map<String, List<String>> commandInfos = new HashMap<>();
private Exception exception;
Expand All @@ -79,6 +82,8 @@ public SystemRegistryImpl(Parser parser, Terminal terminal, ConfigurationPath co
commandName.put(c, c.name().toLowerCase());
}
doNameCommand();
pipeName.put(Pipe.FLIP, "|;");
pipeName.put(Pipe.NAMED, "|");
commandExecute.put(Command.EXIT, new CommandMethods(this::exit, this::exitCompleter));
commandExecute.put(Command.HELP, new CommandMethods(this::help, this::helpCompleter));
}
Expand All @@ -87,6 +92,18 @@ private void doNameCommand() {
nameCommand = commandName.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
}

public void rename(Pipe pipe, String name) {
if (name.matches("/w+") || pipeName.containsValue(name)) {
throw new IllegalArgumentException();
}
pipeName.put(pipe, name);
}

@Override
public Collection<String> getPipeNames() {
return pipeName.values();
}

@Override
public void setCommandRegistries(CommandRegistry... commandRegistries) {
this.commandRegistries = commandRegistries;
Expand Down Expand Up @@ -259,7 +276,9 @@ public Widgets.CmdDesc commandDescription(Widgets.CmdLine line) {
switch (line.getDescriptionType()) {
case COMMAND:
String cmd = parser.getCommand(line.getArgs().get(0));
out = commandDescription(cmd);
if (isCommandOrScript(cmd) && !hasPipes(line.getArgs())) {
out = commandDescription(cmd);
}
break;
case METHOD:
case SYNTAX:
Expand Down Expand Up @@ -452,8 +471,18 @@ public void reset() {
}
}

private boolean hasPipes(Collection<String> args) {
Map<String,List<String>> customPipes = consoleId != null ? consoleEngine().getPipes() : new HashMap<>();
for (String a : args) {
if (isPipe(a, customPipes.keySet())) {
return true;
}
}
return false;
}

private boolean isPipe(String arg, Set<String> pipes) {
return arg.equals(PIPE_FLIP) || arg.equals(PIPE_NAMED) || pipes.contains(arg);
return pipeName.containsValue(arg) || pipes.contains(arg);
}

private List<CommandData> compileCommandLine(String commandLine) {
Expand All @@ -463,11 +492,11 @@ private List<CommandData> compileCommandLine(String commandLine) {
List<String> words = new ArrayList<>();
String nextRawLine = "";
Map<String,List<String>> customPipes = consoleId != null ? consoleEngine().getPipes() : new HashMap<>();
if (consoleId != null && pl.words().contains(PIPE_NAMED)) {
if (consoleId != null && pl.words().contains(pipeName.get(Pipe.NAMED))) {
StringBuilder sb = new StringBuilder();
boolean trace = false;
for (int i = 0 ; i < ws.size(); i++) {
if (ws.get(i).equals(PIPE_NAMED)) {
if (ws.get(i).equals(pipeName.get(Pipe.NAMED))) {
if (i + 1 >= ws.size()) {
throw new IllegalArgumentException();
}
Expand Down Expand Up @@ -541,7 +570,7 @@ private List<CommandData> compileCommandLine(String commandLine) {
last = i + 1;
lastarg = i;
break;
} else if (words.get(i).equals(PIPE_FLIP)) {
} else if (words.get(i).equals(pipeName.get(Pipe.FLIP))) {
if (variable != null || file != null || pipeResult != null || consoleId == null) {
throw new IllegalArgumentException();
}
Expand All @@ -550,10 +579,10 @@ private List<CommandData> compileCommandLine(String commandLine) {
lastarg = i;
variable = "_pipe" + (pipes.size() - 1);
break;
} else if ( words.get(i).equals(PIPE_NAMED)
} else if ( words.get(i).equals(pipeName.get(Pipe.NAMED))
|| (words.get(i).matches("^.*[^a-zA-Z0-9 ].*$") && customPipes.containsKey(words.get(i)))) {
String pipe = words.get(i);
if (pipe.equals(PIPE_NAMED)) {
if (pipe.equals(pipeName.get(Pipe.NAMED))) {
if (i + 1 >= last) {
throw new IllegalArgumentException("Pipe is NULL!");
}
Expand Down Expand Up @@ -600,7 +629,7 @@ private List<CommandData> compileCommandLine(String commandLine) {
rawLine += fixes.get(0) + subLine + fixes.get(1);
statement = true;
}
if (pipes.get(pipes.size() - 1).equals(PIPE_FLIP)) {
if (pipes.get(pipes.size() - 1).equals(pipeName.get(Pipe.FLIP))) {
done = true;
pipeSource = null;
rawLine = variable + " = " + rawLine;
Expand All @@ -612,7 +641,7 @@ private List<CommandData> compileCommandLine(String commandLine) {
rawLine = pipeResult + " = " + rawLine;
}
}
} else if (pipes.get(pipes.size() - 1).equals(PIPE_FLIP) || pipeStart) {
} else if (pipes.get(pipes.size() - 1).equals(pipeName.get(Pipe.FLIP)) || pipeStart) {
if (pipeStart && pipeResult != null) {
subLine = subLine.substring(subLine.indexOf("=") + 1);
}
Expand All @@ -633,7 +662,7 @@ private List<CommandData> compileCommandLine(String commandLine) {

private String flipArgument(final String command, final String subLine, final List<String> pipes, List<String> arglist) {
String out = null;
if (pipes.size() > 1 && pipes.get(pipes.size() - 2).equals(PIPE_FLIP)) {
if (pipes.size() > 1 && pipes.get(pipes.size() - 2).equals(pipeName.get(Pipe.FLIP))) {
String s = isCommandOrScript(command) ? "$" : "";
out = subLine + " " + s + "_pipe" + (pipes.size() - 2);
arglist.add(s + "_pipe" + (pipes.size() - 2));
Expand Down
36 changes: 25 additions & 11 deletions builtins/src/main/java/org/jline/builtins/Widgets.java
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ public enum TipType {
private TipType tipType;
private int descriptionSize = 0;
private boolean descriptionEnabled = true;
private boolean descriptionCache = true;

/**
* Creates tailtip widgets used in command line suggestions. Suggestions are created using a command
Expand Down Expand Up @@ -732,6 +733,10 @@ public void enable() {
}
}

public void setDescriptionCache(boolean cache) {
this.descriptionCache = cache;
}

/*
* widgets
*/
Expand Down Expand Up @@ -816,7 +821,7 @@ private void doCommandTailTip(String widget, CmdDesc cmdDesc, List<String> args)
}
String lastArg = "";
prevArg = args.get(args.size() - 1);
if (!prevChar().equals(" ")) {
if (!prevChar().equals(" ") && args.size() > 1) {
lastArg = args.get(args.size() - 1);
prevArg = args.get(args.size() - 2);
}
Expand Down Expand Up @@ -1007,6 +1012,7 @@ private void customBindings() {
private class CommandDescriptions {
Map<String,CmdDesc> descriptions = new HashMap<>();
Map<String,CmdDesc> temporaryDescs = new HashMap<>();
Map<String,CmdDesc> volatileDescs = new HashMap<>();
Function<CmdLine,CmdDesc> descFun;

public CommandDescriptions(Map<String,CmdDesc> descriptions) {
Expand Down Expand Up @@ -1072,19 +1078,23 @@ private Pair<String,Boolean> evaluateCommandLine(String line, List<String> args,
}
}
}
if (cmd != null && !descriptions.containsKey(cmd) && !temporaryDescs.containsKey(cmd)
&& descFun != null) {
if (descType == CmdLine.DescriptionType.COMMAND) {
if (cmd != null && descFun != null) {
if (!descriptionCache && descType == CmdLine.DescriptionType.COMMAND) {
CmdDesc c = descFun.apply(new CmdLine(line, head, tail, args, descType));
if (c != null) {
descriptions.put(cmd, c);
volatileDescs.put(cmd, c);
} else if (!descriptions.containsKey(cmd) && !temporaryDescs.containsKey(cmd)) {
if (descType == CmdLine.DescriptionType.COMMAND) {
CmdDesc c = descFun.apply(new CmdLine(line, head, tail, args, descType));
if (c != null) {
descriptions.put(cmd, c);
} else {
temporaryDescs.put(cmd, c);
}
} else if (descType == CmdLine.DescriptionType.METHOD) {
temporaryDescs.put(cmd, descFun.apply(new CmdLine(line, head, tail, args, descType)));
} else {
temporaryDescs.put(cmd, c);
temporaryDescs.put(cmd, descFun.apply(new CmdLine(line, head, tail, args, descType)));
}
} else if (descType == CmdLine.DescriptionType.METHOD) {
temporaryDescs.put(cmd, descFun.apply(new CmdLine(line, head, tail, args, descType)));
} else {
temporaryDescs.put(cmd, descFun.apply(new CmdLine(line, head, tail, args, descType)));
}
}
return new Pair<String,Boolean>(cmd, descType == CmdLine.DescriptionType.COMMAND ? true : false);
Expand All @@ -1096,13 +1106,17 @@ public CmdDesc getDescription(String command) {
out = descriptions.get(command);
} else if (temporaryDescs.containsKey(command)) {
out = temporaryDescs.get(command);
} else if (volatileDescs.containsKey(command)) {
out = volatileDescs.get(command);
volatileDescs.remove(command);
}
return out;
}

public void clearTemporaryDescs() {
temporaryDescs = new HashMap<>();
}

}

}
Expand Down
4 changes: 2 additions & 2 deletions demo/src/main/java/org/jline/demo/Repl.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ private List<Completer> tputCompleter(String command) {

}


public static void main(String[] args) {
try {
//
Expand Down Expand Up @@ -286,7 +285,8 @@ public static void main(String[] args) {
//
// widgets and console initialization
//
new TailTipWidgets(reader, systemRegistry::commandDescription, 5, TipType.COMPLETER);
TailTipWidgets ttw = new TailTipWidgets(reader, systemRegistry::commandDescription, 5, TipType.COMPLETER);
ttw.setDescriptionCache(false);
KeyMap<Binding> keyMap = reader.getKeyMaps().get("main");
keyMap.bind(new Reference("tailtip-toggle"), KeyMap.alt("s"));
systemRegistry.initialize(Paths.get(root, "init.jline").toFile());
Expand Down
4 changes: 3 additions & 1 deletion demo/src/main/scripts/init.jline
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import java.nio.file.*;
PATH = [ROOT + 'scripts']

#
# write jnanorc configuration file
# create jnanorc configuration file
#
_jnanorc = Paths.get(ROOT, 'jnanorc').toFile()
_jnanorc << 'include ' + ROOT + 'nanorc/*.nanorc\n'
Expand All @@ -19,6 +19,7 @@ CONSOLE_OPTIONS.splitOutput = true
#
# custom Groovy pipes
#
pipe --delete *
pipe |. '.collect{' '}'
pipe |: '.collectEntries{' '}'
pipe |:: '.collectMany{' '}'
Expand All @@ -28,3 +29,4 @@ pipe |! '.each{' '}'
pipe |# '.take(' ')'
pipe |& '.' ' '
pipe grep '.collect{it.toString()}.findAll{it=~/' '/}'
alias xargs '|; %{0} %{1} %{2} %{3} %{4} %{5} %{6} %{7} %{8} %{9}'

0 comments on commit 8a5513e

Please sign in to comment.