Skip to content

Commit

Permalink
Implemented pipe line alias
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Feb 10, 2020
1 parent 95ff132 commit 96c2801
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 18 deletions.
12 changes: 6 additions & 6 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public String getAlias(String name) {

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

private void doNameCommand() {
Expand Down Expand Up @@ -716,7 +716,7 @@ public void trace(final Object object) {
Map<String, Object> options = new HashMap<>();
if (level < 2) {
options.put("exception", "message");
}
}
if (level == 0) {
if (!(object instanceof Exception)) {
toPrint = null;
Expand Down Expand Up @@ -961,21 +961,21 @@ private Object pipe(Builtins.CommandInput input) {
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");
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;
return out;
}

private List<OptDesc> commandOptions(String command) {
Expand Down Expand Up @@ -1068,7 +1068,7 @@ private List<Completer> pipeCompleter(String command) {
, this::commandOptions
, 1)
));
return completers;
return completers;
}

}
75 changes: 63 additions & 12 deletions builtins/src/main/java/org/jline/builtins/SystemRegistryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.jline.builtins.Completers.OptDesc;
Expand Down Expand Up @@ -191,7 +193,7 @@ public boolean hasCommand(String command) {
private boolean isLocalCommand(String command) {
return nameCommand.containsKey(command) || aliasCommand.containsKey(command);
}

private boolean isCommandOrScript(String command) {
if (hasCommand(command)) {
return true;
Expand Down Expand Up @@ -450,14 +452,63 @@ public void reset() {
}
}

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

private List<CommandData> compileCommandLine(String commandLine) {
List<CommandData> out = new ArrayList<>();
ParsedLine pl = parser.parse(commandLine, 0, ParseContext.ACCEPT_LINE);
List<String> words = pl.words();
List<String> ws = pl.words();
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)) {
StringBuilder sb = new StringBuilder();
boolean trace = false;
for (int i = 0 ; i < ws.size(); i++) {
if (ws.get(i).equals(PIPE_NAMED)) {
if (i + 1 >= ws.size()) {
throw new IllegalArgumentException();
}
if (consoleEngine().hasAlias(ws.get(i + 1))) {
trace = true;
List<String> args = new ArrayList<>();
String pipeAlias = consoleEngine().getAlias(ws.get(++i));
while (i < ws.size() - 1 && !isPipe(ws.get(i + 1), customPipes.keySet())) {
args.add(ws.get(++i));
}
for (int j = 0; j < args.size(); j++) {
pipeAlias = pipeAlias.replaceAll("\\s\\$" + j + "\\b", " " + args.get(j));
pipeAlias = pipeAlias.replaceAll("\\$\\{" + j + "(|:-.*)\\}", args.get(j));
}
pipeAlias = pipeAlias.replaceAll("\\s\\$\\d\\b", "");
pipeAlias = pipeAlias.replaceAll("\\$\\{\\d+\\}", "");
Matcher matcher=Pattern.compile("\\$\\{\\d+:-(.*?)\\}").matcher(pipeAlias);
if (matcher.find()) {
pipeAlias = matcher.replaceAll("$1");
}
sb.append(pipeAlias);
} else {
sb.append(ws.get(i));
}
} else {
sb.append(ws.get(i));
}
sb.append(" ");
}
nextRawLine = sb.toString();
pl = parser.parse(nextRawLine, 0, ParseContext.ACCEPT_LINE);
words = pl.words();
if (trace) {
consoleEngine().trace(nextRawLine);
}
} else {
words = ws;
nextRawLine = commandLine;
}
int first = 0;
int last = words.size();
String nextRawLine = commandLine;
Map<String,List<String>> customPipes = consoleId != null ? consoleEngine().getPipes() : new HashMap<>();
List<String> pipes = new ArrayList<>();
String pipeSource = null;
String rawLine = null;
Expand Down Expand Up @@ -499,18 +550,18 @@ 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(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 (i + 1 >= last) {
throw new IllegalArgumentException("Pipe is NULL!");
throw new IllegalArgumentException("Pipe is NULL!");
}
pipe = words.get(i + 1);
if (!pipe.matches("\\w+") || !customPipes.containsKey(pipe)) {
throw new IllegalArgumentException("Unknown or illegal pipe name: " + pipe);
throw new IllegalArgumentException("Unknown or illegal pipe name: " + pipe);
}
}
}
pipes.add(pipe);
last = i;
lastarg = i;
Expand All @@ -519,7 +570,7 @@ private List<CommandData> compileCommandLine(String commandLine) {
pipeResult = variable;
variable = pipeSource;
pipeStart = true;
}
}
}
}
if (last == words.size()) {
Expand Down Expand Up @@ -560,7 +611,7 @@ private List<CommandData> compileCommandLine(String commandLine) {
if (pipeResult != null) {
rawLine = pipeResult + " = " + rawLine;
}
}
}
} else if (pipes.get(pipes.size() - 1).equals(PIPE_FLIP) || pipeStart) {
if (pipeStart && pipeResult != null) {
subLine = subLine.substring(subLine.indexOf("=") + 1);
Expand Down Expand Up @@ -698,15 +749,15 @@ public Object execute(String line) throws Exception {
}
}
} else if (consoleId != null) {
consoleScript = true;
consoleScript = true;
}
if (consoleScript) {
if (cmd.command().isEmpty() || !consoleEngine().scripts().contains(cmd.command())) {
statement = true;
}
if (statement && outputStream.isByteStream()) {
outputStream.close();
outputStream.reset();
outputStream.reset();
}
out = consoleEngine().execute(cmd.command(), cmd.rawLine(), cmd.args());
}
Expand Down

0 comments on commit 96c2801

Please sign in to comment.