Skip to content

Commit

Permalink
Fixed repl demo shell help & improved console command completers
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed May 2, 2020
1 parent 8ff80c0 commit bf7f1ad
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 41 deletions.
17 changes: 10 additions & 7 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,6 @@ private AttributedString highlight(Integer width, SyntaxHighlighter highlighter,
}

private AttributedString highlight(Integer width, SyntaxHighlighter highlighter, String object, boolean doValueHighlight) {
Log.debug("highlighter:", highlighter, ", highlight: ", object);
AttributedString out = null;
AttributedStringBuilder asb = new AttributedStringBuilder();
String val = object;
Expand Down Expand Up @@ -1848,7 +1847,6 @@ private Object pipe(CommandInput input) {
" -d --delete Delete pipe operators",
" -l --list List pipe operators",
};
Object out = null;
try {
Options opt = parseOptions(usage, input.args());
if (opt.isSet("delete")) {
Expand All @@ -1860,7 +1858,9 @@ private Object pipe(CommandInput input) {
}
}
} else if (opt.isSet("list") || opt.args().size() == 0) {
out = pipes;
Map<String, Object> options = defaultPrntOptions(false);
options.put(Printer.MAX_DEPTH, 0);
internalPrintln(options, pipes);
} else if (opt.args().size() != 3) {
exception = new IllegalArgumentException("Bad number of arguments!");
} else if (systemRegistry.getPipeNames().contains(opt.args().get(0))) {
Expand All @@ -1874,7 +1874,7 @@ private Object pipe(CommandInput input) {
} catch (Exception e) {
exception = e;
}
return out;
return null;
}

private Object doc(CommandInput input) {
Expand Down Expand Up @@ -1945,7 +1945,8 @@ private List<Completer> slurpCompleter(String command) {
}
}
completers.add(new ArgumentCompleter(NullCompleter.INSTANCE
, new OptionCompleter(new FilesCompleter(workDir)
, new OptionCompleter(Arrays.asList(new FilesCompleter(workDir)
, NullCompleter.INSTANCE)
, optDescs
, 1)
));
Expand All @@ -1969,7 +1970,8 @@ private List<String> variableReferences() {
private List<Completer> prntCompleter(String command) {
List<Completer> completers = new ArrayList<>();
completers.add(new ArgumentCompleter(NullCompleter.INSTANCE
, new OptionCompleter(new StringsCompleter(this::variableReferences)
, new OptionCompleter(Arrays.asList(new StringsCompleter(this::variableReferences)
, NullCompleter.INSTANCE)
, this::commandOptions
, 1)
));
Expand Down Expand Up @@ -2043,7 +2045,8 @@ private List<String> docs() {
private List<Completer> docCompleter(String command) {
List<Completer> completers = new ArrayList<>();
completers.add(new ArgumentCompleter(NullCompleter.INSTANCE
, new OptionCompleter(new StringsCompleter(this::docs)
, new OptionCompleter(Arrays.asList(new StringsCompleter(this::docs)
, NullCompleter.INSTANCE)
, this::commandOptions
, 1)
));
Expand Down
25 changes: 13 additions & 12 deletions builtins/src/main/java/org/jline/builtins/SystemRegistryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1710,18 +1710,19 @@ public void extractNames(String line) {
}
String[] words = rest.split("\\s+");
for (String w : words) {
if (!w.matches("\\d+")) {
if (isQuoted(w)) {
addQuoted(w.substring(1, w.length() - 1));
} else if (w.contains(".")) {
for (String f : w.split("\\.")) {
if (!f.matches("\\d+") && f.matches("\\w+")) {
addFields(f);
}
if (w.length() < 3 || w.matches("\\d+")) {
continue;
}
if (isQuoted(w)) {
addQuoted(w.substring(1, w.length() - 1));
} else if (w.contains(".")) {
for (String f : w.split("\\.")) {
if (!f.matches("\\d+") && f.matches("\\w+")) {
addFields(f);
}
} else if (w.matches("\\w+")) {
addValues(w);
}
} else if (w.matches("\\w+")) {
addValues(w);
}
}
}
Expand All @@ -1741,9 +1742,9 @@ public String encloseBy(String param) {
}

private boolean isQuoted(String word) {
if ((word.startsWith("\"") && word.endsWith("\""))
if (word.length() > 1 && ((word.startsWith("\"") && word.endsWith("\""))
|| (word.startsWith("'") && word.endsWith("'"))
|| (word.startsWith("/") && word.endsWith("/"))) {
|| (word.startsWith("/") && word.endsWith("/")))) {
return true;
}
return false;
Expand Down
34 changes: 12 additions & 22 deletions demo/src/main/java/org/jline/demo/Repl.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,8 @@
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.jline.builtins.JlineCommandRegistry;
import org.jline.builtins.Builtins;
import org.jline.builtins.ConsoleEngine;
import org.jline.builtins.ConsoleEngineImpl;
import org.jline.builtins.Options;
import org.jline.builtins.*;
import org.jline.builtins.Completers.OptionCompleter;
import org.jline.builtins.Options.HelpException;
import org.jline.builtins.SystemRegistryImpl;
import org.jline.builtins.Widgets.TailTipWidgets;
import org.jline.builtins.Widgets.TailTipWidgets.TipType;
import org.jline.console.CommandInput;
Expand Down Expand Up @@ -193,21 +187,17 @@ private void shell(CommandInput input) {
final String[] usage = { "!<command> - execute shell command"
, "Usage: !<command>"
, " -? --help Displays command help" };
try {
parseOptions(usage, input.args());
} catch (HelpException e) {
saveException(e);
return;
} catch (Exception e) {
// ignore
}
List<String> argv = new ArrayList<>();
argv.addAll(Arrays.asList(input.args()));
if (!argv.isEmpty()) {
try {
executeCmnd(argv);
} catch (Exception e) {
saveException(e);
if (input.args().length == 1 && (input.args()[0].equals("-?") || input.args()[0].equals("--help"))) {
Options.HelpException.highlight(String.join("\n", usage), Styles.helpStyle()).println(terminal());
} else {
List<String> argv = new ArrayList<>();
argv.addAll(Arrays.asList(input.args()));
if (!argv.isEmpty()) {
try {
executeCmnd(argv);
} catch (Exception e) {
saveException(e);
}
}
}
}
Expand Down

0 comments on commit bf7f1ad

Please sign in to comment.