Skip to content

Commit

Permalink
REPL console: improved exit status evaluation & pipe line compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Feb 18, 2020
1 parent 9109f98 commit 0181121
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 20 deletions.
11 changes: 9 additions & 2 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ static String plainCommand(String command) {

/**
* Returns all scripts found from PATH
* @return script file names
* @return map keys have script file names and value is true if it is console script
*/
List<String> scripts();
Map<String, Boolean> scripts();

/**
* Sets file name extension used by console scripts
Expand Down Expand Up @@ -172,6 +172,13 @@ default Object execute(File script) throws Exception {
*/
Object getVariable(String name);

/**
* Test if variable with name exists
* @param name name of the variable
* @return true if variable with name exists
*/
boolean hasVariable(String name);

/**
* Delete temporary console variables
*/
Expand Down
24 changes: 18 additions & 6 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ private Set<String> variables() {
public List<Completer> scriptCompleters() {
List<Completer> out = new ArrayList<>();
out.add(new ArgumentCompleter(new StringsCompleter(this::variables), NullCompleter.INSTANCE));
out.add(new ArgumentCompleter(new StringsCompleter(this::scripts)
out.add(new ArgumentCompleter(new StringsCompleter(this::scriptNames)
, new OptionCompleter(NullCompleter.INSTANCE
, this::commandOptions
, 1)
Expand All @@ -228,10 +228,14 @@ public List<Completer> scriptCompleters() {
return out;
}

private Set<String> scriptNames() {
return scripts().keySet();
}

@SuppressWarnings("unchecked")
@Override
public List<String> scripts() {
List<String> out = new ArrayList<>();
public Map<String, Boolean> scripts() {
Map<String, Boolean> out = new HashMap<>();
try {
List<Path> scripts = new ArrayList<>();
if (engine.hasVariable(VAR_PATH)) {
Expand All @@ -246,7 +250,8 @@ public List<String> scripts() {
}
for (Path p : scripts) {
String name = p.toFile().getName();
out.add(name.substring(0, name.lastIndexOf(".")));
int idx = name.lastIndexOf(".");
out.put(name.substring(0, idx), name.substring(idx + 1).equals(scriptExtension));
}
} catch (Exception e) {
trace(e);
Expand Down Expand Up @@ -639,6 +644,11 @@ public Object getVariable(String name) {
}
return engine.get(name);
}

@Override
public boolean hasVariable(String name) {
return engine.hasVariable(name);
}

@Override
public boolean executeWidget(Object function) {
Expand Down Expand Up @@ -686,7 +696,7 @@ public ExecutionResult postProcess(String line, Object result, String output) {
} else {
Object _result = result == null ? _output : result;
int status = saveResult(consoleVar, _result);
out = new ExecutionResult(status, consoleVar != null ? null : _result);
out = new ExecutionResult(status, consoleVar != null && !consoleVar.startsWith("_") ? null : _result);
}
return out;
}
Expand All @@ -697,7 +707,9 @@ private ExecutionResult postProcess(String line, Object result) {
String consoleVar = parser().getVariable(line);
if (consoleVar != null) {
status = saveResult(consoleVar, result);
out = null;
if (!consoleVar.startsWith("_")) {
out = null;
}
} else if (!parser().getCommand(line).equals("show")) {
if (result != null) {
status = saveResult("_", result);
Expand Down
40 changes: 28 additions & 12 deletions builtins/src/main/java/org/jline/builtins/SystemRegistryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ private List<String> localCommandInfo(String command) {
return new ArrayList<>();
}

private Set<String> scripts() {
return consoleEngine().scripts().keySet();
}

@Override
public List<String> commandInfo(String command) {
int id = registryId(command);
Expand All @@ -199,7 +203,7 @@ public List<String> commandInfo(String command) {
commandInfos.put(command, commandRegistries[id].commandInfo(command));
}
out = commandInfos.get(command);
} else if (consoleId > -1 && consoleEngine().scripts().contains(command)) {
} else if (consoleId > -1 && scripts().contains(command)) {
out = consoleEngine().commandInfo(command);
} else if (isLocalCommand(command)) {
out = localCommandInfo(command);
Expand All @@ -220,7 +224,7 @@ private boolean isCommandOrScript(String command) {
if (hasCommand(command)) {
return true;
}
return consoleId > -1 ? consoleEngine().scripts().contains(command) : false;
return consoleId > -1 ? scripts().contains(command) : false;
}

@Override
Expand Down Expand Up @@ -267,7 +271,7 @@ public Widgets.CmdDesc commandDescription(String command) {
int id = registryId(command);
if (id > -1) {
out = commandRegistries[id].commandDescription(command);
} else if (consoleId > -1 && consoleEngine().scripts().contains(command)) {
} else if (consoleId > -1 && scripts().contains(command)) {
out = consoleEngine().commandDescription(command);
} else if (isLocalCommand(command)) {
out = localCommandDescription(command);
Expand Down Expand Up @@ -482,7 +486,7 @@ 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());
Expand All @@ -509,6 +513,7 @@ private List<CommandData> compileCommandLine(String commandLine) {
List<String> words = new ArrayList<>();
String nextRawLine = "";
Map<String,List<String>> customPipes = consoleId != null ? consoleEngine().getPipes() : new HashMap<>();
Map<String, Boolean> scripts = consoleEngine().scripts();
if (consoleId != null && pl.words().contains(pipeName.get(Pipe.NAMED))) {
StringBuilder sb = new StringBuilder();
boolean trace = false;
Expand Down Expand Up @@ -562,7 +567,7 @@ private List<CommandData> compileCommandLine(String commandLine) {
String pipeResult = null;
do {
String command = ConsoleEngine.plainCommand(parser.getCommand(words.get(first)));
String variable = variable = parser.getVariable(words.get(first));
String variable = parser.getVariable(words.get(first));
if (parser.validCommandName(command) && consoleId != null) {
if (consoleEngine().hasAlias(command)) {
String value = consoleEngine().getAlias(command).split("\\s+")[0];
Expand All @@ -574,6 +579,9 @@ private List<CommandData> compileCommandLine(String commandLine) {
}
}
}
if (!hasCommand(command) && scripts.getOrDefault(command, false)) {
throw new IllegalArgumentException("Console scripts cannot be used in pipe!");
}
last = words.size();
File file = null;
boolean append = false;
Expand Down Expand Up @@ -633,12 +641,11 @@ private List<CommandData> compileCommandLine(String commandLine) {
out.get(out.size() - 1).setPipe(words.get(i));
skipPipe = true;
} else {
pipes.add(pipeName.get(Pipe.FLIP));
pipes.add(words.get(i));
pipeSource = "_pipe" + (pipes.size() - 1);
pipeResult = variable;
variable = pipeSource;
pipeStart = true;
i = i - 1;
}
last = i;
break;
Expand Down Expand Up @@ -909,7 +916,7 @@ public Object execute(String line) throws Exception {
consoleScript = true;
}
if (consoleScript) {
if (cmd.command().isEmpty() || !consoleEngine().scripts().contains(cmd.command())) {
if (cmd.command().isEmpty() || !scripts().contains(cmd.command())) {
statement = true;
}
if (statement && outputStream.isByteStream()) {
Expand Down Expand Up @@ -965,9 +972,18 @@ private ExecutionResult postProcess(CommandData cmd, boolean statement, Object r
out = new ExecutionResult(status, null);
} else if (!statement) {
outputStream.flush();
outputStream.close();
out = consoleEngine().postProcess(cmd.rawLine(), result, outputStream.getOutput());
outputStream.reset();
} else if (cmd.variable() != null) {
out = consoleEngine().postProcess(consoleEngine().getVariable(cmd.variable()));
if (consoleEngine().hasVariable(cmd.variable())) {
out = consoleEngine().postProcess(consoleEngine().getVariable(cmd.variable()));
} else {
out = consoleEngine().postProcess(result);
}
if (!cmd.variable().startsWith("_")) {
out = new ExecutionResult(out.status(), null);
}
} else {
out = consoleEngine().postProcess(result);
}
Expand Down Expand Up @@ -1082,7 +1098,7 @@ private Object help(Builtins.CommandInput input) {

Set<String> commands = commandNames();
if (consoleId > -1) {
commands.addAll(consoleEngine().scripts());
commands.addAll(scripts());
}
boolean withInfo = commands.size() < terminal().getHeight() || !opt.args().isEmpty() ? true : false;
int max = Collections.max(commands, Comparator.comparing(String::length)).length() + 1;
Expand Down Expand Up @@ -1125,11 +1141,11 @@ private Object help(Builtins.CommandInput input) {
if (consoleId > -1 && isInArgs(opt.args(), "Scripts")) {
printHeader("Scripts");
if (withInfo) {
for (String c : consoleEngine().scripts()) {
for (String c : scripts()) {
printCommandInfo(c, doCommandInfo(commandInfo(c)), max);
}
} else {
printCommands(consoleEngine().scripts(), max);
printCommands(scripts(), max);
}
}
terminal().flush();
Expand Down

0 comments on commit 0181121

Please sign in to comment.