Skip to content

Commit

Permalink
REPL console: calculate exit status also for groovy statements and
Browse files Browse the repository at this point in the history
fixed pipe line compilation
  • Loading branch information
mattirn committed Feb 17, 2020
1 parent 88e451b commit 9109f98
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 25 deletions.
15 changes: 11 additions & 4 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ default Object execute(File script) throws Exception {
*/
ExecutionResult postProcess(String line, Object result, String output);

/**
* Post processes execution result.
* @param result command result to process
* @return processed result
*/
ExecutionResult postProcess(Object result);

/**
* @param object object to print
*/
Expand Down Expand Up @@ -182,20 +189,20 @@ default Object execute(File script) throws Exception {
* @return true if consoleEngine is executing script
*/
boolean isExecuting();

static class ExecutionResult {
final int status;
final Object result;

public ExecutionResult(int status, Object result) {
this.status = status;
this.result = result;
}

public int status() {
return status;
}

public Object result() {
return result;
}
Expand Down
22 changes: 17 additions & 5 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ private <T>T consoleOption(String option, T defval) {
@Override
public ExecutionResult postProcess(String line, Object result, String output) {
ExecutionResult out = new ExecutionResult(1, null);
Object _output = output != null && !output.trim().isEmpty() && consoleOption("splitOutput", true)
Object _output = output != null && !output.trim().isEmpty() && consoleOption("splitOutput", true)
? output.split("\\r?\\n") : output;
String consoleVar = parser().getVariable(line);
if (consoleVar != null && result != null) {
Expand All @@ -698,12 +698,21 @@ private ExecutionResult postProcess(String line, Object result) {
if (consoleVar != null) {
status = saveResult(consoleVar, result);
out = null;
} else if (!parser().getCommand(line).equals("show") && result != null) {
status = saveResult("_", result);
} else if (!parser().getCommand(line).equals("show")) {
if (result != null) {
status = saveResult("_", result);
} else {
status = 1;
}
}
return new ExecutionResult(status, out);
}

@Override
public ExecutionResult postProcess(Object result) {
return new ExecutionResult(saveResult(null, result), result);
}

private int saveResult(String var, Object result) {
int out = 0;
try {
Expand Down Expand Up @@ -812,8 +821,11 @@ public void println(Map<String, Object> options, Object object) {

private void highlight(int attrStyle, Object obj) {
AttributedStringBuilder asb = new AttributedStringBuilder();
asb.append(obj.toString(), AttributedStyle.DEFAULT.foreground(attrStyle));
asb.toAttributedString().println(terminal());
String tp = obj.toString();
if (!tp.isEmpty()) {
asb.append(tp, AttributedStyle.DEFAULT.foreground(attrStyle));
asb.toAttributedString().println(terminal());
}
}

private void highlight(int width, String style, String object) {
Expand Down
57 changes: 41 additions & 16 deletions builtins/src/main/java/org/jline/builtins/SystemRegistryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,11 @@ public void closeAndReset() {
reset();
}
}

private boolean isPipe(String arg) {
Map<String,List<String>> customPipes = consoleId != null ? consoleEngine().getPipes() : new HashMap<>();
return isPipe(arg, customPipes.keySet());
}

private boolean hasPipes(Collection<String> args) {
Map<String,List<String>> customPipes = consoleId != null ? consoleEngine().getPipes() : new HashMap<>();
Expand Down Expand Up @@ -556,15 +561,17 @@ private List<CommandData> compileCommandLine(String commandLine) {
String rawLine = null;
String pipeResult = null;
do {
String variable = null;
String command = ConsoleEngine.plainCommand(parser.getCommand(words.get(first)));
String variable = variable = parser.getVariable(words.get(first));
if (parser.validCommandName(command) && consoleId != null) {
variable = parser.getVariable(words.get(first));
if (consoleEngine().hasAlias(command)) {
pl = parser.parse(nextRawLine.replaceFirst(command, consoleEngine().getAlias(command)), 0, ParseContext.SPLIT_LINE);
command = ConsoleEngine.plainCommand(parser.getCommand(pl.word()));
words = pl.words();
first = 0;
String value = consoleEngine().getAlias(command).split("\\s+")[0];
if (!isPipe(value)) {
pl = parser.parse(nextRawLine.replaceFirst(command, consoleEngine().getAlias(command)), 0, ParseContext.SPLIT_LINE);
command = ConsoleEngine.plainCommand(parser.getCommand(pl.word()));
words = pl.words();
first = 0;
}
}
}
last = words.size();
Expand Down Expand Up @@ -617,23 +624,18 @@ private List<CommandData> compileCommandLine(String commandLine) {
}
break;
} else if (words.get(i).equals(pipeName.get(Pipe.OR)) || words.get(i).equals(pipeName.get(Pipe.AND))) {
if (pipeResult != null) {
if (variable != null || pipeSource != null) {
pipes.add(words.get(i));
variable = pipeResult;
pipeResult = null;
} else if (pipeSource != null
|| variable != null
) {
pipes.add(words.get(i));
} else if (pipes.size() > 0 && (
pipes.get(pipes.size() - 1).equals(">")
pipes.get(pipes.size() - 1).equals(">")
|| pipes.get(pipes.size() - 1).equals(">>"))) {
pipes.remove(pipes.size() - 1);
out.get(out.size() - 1).setPipe(words.get(i));
skipPipe = true;
} else {
pipes.add(pipeName.get(Pipe.FLIP));
pipeSource = "_pipe" + (pipes.size() - 1);
pipeResult = variable;
variable = pipeSource;
pipeStart = true;
i = i - 1;
Expand Down Expand Up @@ -710,6 +712,11 @@ private List<CommandData> compileCommandLine(String commandLine) {
}
}
out.add(new CommandData(rawLine, statement ? "" : command, args, variable, file, append, pipes.get(pipes.size() - 1)));
if ( pipes.get(pipes.size() - 1).equals(pipeName.get(Pipe.AND))
|| pipes.get(pipes.size() - 1).equals(pipeName.get(Pipe.OR))) {
pipeSource = null;
pipeResult = null;
}
rawLine = null;
}
first = last + 1;
Expand Down Expand Up @@ -861,7 +868,9 @@ public Object execute(String line) throws Exception {
Object out = null;
boolean statement = false;
boolean postProcessed = false;
for (CommandData cmd : compileCommandLine(line)) {
List<CommandData> cmds = compileCommandLine(line);
for (int i = 0; i < cmds.size(); i++) {
CommandData cmd = cmds.get(i);
try {
outputStream.closeAndReset();
if (!consoleEngine().isExecuting()) {
Expand Down Expand Up @@ -921,11 +930,23 @@ public Object execute(String line) throws Exception {
}
} catch (HelpException e) {
trace(e);
} catch (Exception e) {
if (cmd.pipe().equals(pipeName.get(Pipe.OR))) {
trace(e);
postProcessed = true;
} else if (cmd.pipe().equals(pipeName.get(Pipe.FLIP))
&& i + 1 < cmds.size() && cmds.get(i + 1).pipe().equals(pipeName.get(Pipe.OR)) ) {
i++;
trace(e);
postProcessed = true;
} else {
throw e;
}
} finally {
if (!postProcessed && consoleId != null) {
out = postProcess(cmd, statement, out).result();
if (consoleEngine().isExecuting()) {
consoleEngine().println(out);
consoleEngine().println(out);
}
}
}
Expand All @@ -945,6 +966,10 @@ private ExecutionResult postProcess(CommandData cmd, boolean statement, Object r
} else if (!statement) {
outputStream.flush();
out = consoleEngine().postProcess(cmd.rawLine(), result, outputStream.getOutput());
} else if (cmd.variable() != null) {
out = consoleEngine().postProcess(consoleEngine().getVariable(cmd.variable()));
} else {
out = consoleEngine().postProcess(result);
}
return out;
}
Expand Down

0 comments on commit 9109f98

Please sign in to comment.