Skip to content

Commit

Permalink
REPL demo: added an other example script and widget
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Feb 16, 2020
1 parent 2b7bb5b commit 88e451b
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 23 deletions.
57 changes: 39 additions & 18 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ public boolean execute() throws Exception {
internalExecute();
}
}
} else if (isScript()) {
} else {
internalExecute();
}
return true;
Expand All @@ -464,7 +464,6 @@ public boolean execute() throws Exception {
private void internalExecute() throws Exception {
if (isEngineScript()) {
result = engine.execute(script, expandParameters(args));
postProcess(cmdLine, result);
} else if (isConsoleScript()) {
executing = true;
boolean done = false;
Expand Down Expand Up @@ -525,6 +524,28 @@ public Object getResult() {
return result;
}

public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
try {
sb.append("script:").append(script.getCanonicalPath());
} catch (Exception e) {
sb.append(e.getMessage());
}
sb.append(", ");
sb.append("extension:").append(extension);
sb.append(", ");
sb.append("cmdLine:").append(cmdLine);
sb.append(", ");
sb.append("args:").append(Arrays.asList(args));
sb.append(", ");
sb.append("verbose:").append(verbose);
sb.append(", ");
sb.append("result:").append(result);
sb.append("]");
return sb.toString();
}

}

@Override
Expand Down Expand Up @@ -639,32 +660,33 @@ public boolean executeWidget(Object function) {
}

@SuppressWarnings("unchecked")
private boolean splitCommandOutput() {
boolean out = true;
private <T>T consoleOption(String option, T defval) {
T out = defval;
try {
if (engine.hasVariable(VAR_CONSOLE_OPTIONS)) {
out = (boolean) ((Map<String, Object>) engine.get(VAR_CONSOLE_OPTIONS)).getOrDefault("splitOutput", true);
out = (T) ((Map<String, Object>) engine.get(VAR_CONSOLE_OPTIONS)).getOrDefault(option, defval);
}
} catch (Exception e) {
trace(new Exception("Bad CONSOLE_OPTION value: " + e.getMessage()));
}
return out;
}


@Override
public ExecutionResult postProcess(String line, Object result, String output) {
ExecutionResult out = new ExecutionResult(1, null);
Object _output = output != null && !output.trim().isEmpty() && splitCommandOutput() ? output.split("\\r?\\n") : output;
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) {
engine.put("output", _output);
}
if (systemRegistry.hasCommand(parser().getCommand(line))) {
out = postProcess(line, consoleVar != null && result == null ? _output : result);
} else if (consoleVar != null) {
int status = saveResult(consoleVar, result == null ? _output : result);
out = new ExecutionResult(status, null);
} else {
Object _result = result == null ? _output : result;
int status = saveResult(consoleVar, _result);
out = new ExecutionResult(status, consoleVar != null ? null : _result);
}
return out;
}
Expand All @@ -686,10 +708,12 @@ private int saveResult(String var, Object result) {
int out = 0;
try {
engine.put("_executionResult", result);
if (var.contains(".") || var.contains("[")) {
engine.execute(var + " = _executionResult");
} else {
engine.put(var, result);
if (var != null) {
if (var.contains(".") || var.contains("[")) {
engine.execute(var + " = _executionResult");
} else {
engine.put(var, result);
}
}
out = (int) engine.execute("_executionResult ? 0 : 1");
} catch (Exception e) {
Expand Down Expand Up @@ -737,10 +761,7 @@ private Map<String,Object> defaultPrntOptions() {
@Override
public void trace(final Object object) {
Object toPrint = object;
int level = 0;
if (engine.hasVariable(VAR_CONSOLE_OPTIONS)) {
level = (int) ((Map<String, Object>) engine.get(VAR_CONSOLE_OPTIONS)).getOrDefault("trace", 0);
}
int level = consoleOption("trace", 0);
Map<String, Object> options = new HashMap<>();
if (level < 2) {
options.put("exception", "message");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -911,9 +911,7 @@ public Object execute(String line) throws Exception {
if (consoleId != null && cmd.pipe().equals(pipeName.get(Pipe.OR)) || cmd.pipe().equals(pipeName.get(Pipe.AND))) {
ExecutionResult er = postProcess(cmd, statement, out);
postProcessed = true;
if (!consoleEngine().isExecuting()) {
consoleEngine().println(er.result());
}
consoleEngine().println(er.result());
out = null;
boolean success = er.status() == 0 ? true : false;
if ( (cmd.pipe().equals(pipeName.get(Pipe.OR)) && success)
Expand All @@ -924,8 +922,11 @@ public Object execute(String line) throws Exception {
} catch (HelpException e) {
trace(e);
} finally {
if (!postProcessed && consoleId != null && !consoleEngine().isExecuting()) {
if (!postProcessed && consoleId != null) {
out = postProcess(cmd, statement, out).result();
if (consoleEngine().isExecuting()) {
consoleEngine().println(out);
}
}
}
}
Expand Down
File renamed without changes.
18 changes: 17 additions & 1 deletion demo/src/main/scripts/init.jline
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,27 @@ pipe grep '.collect{it.toString()}.findAll{it=~/' '/}'
alias null '|& identity{}'
alias xargs '|; %{0} %{1} %{2} %{3} %{4} %{5} %{6} %{7} %{8} %{9}'
#
# create test-widget and bind it to ctrl-alt-x
# create test-widget and bind it to ctrl-alt-x
# It will read widget name from buffer and execute it
#
def _testWidget() {
def name = _buffer().toString().split('\\s+')[0]
_widget "$name"
}
widget -N test-widget _testWidget
keymap '^[^x' test-widget
#
# create _tailtip-toggle widget that changes also maximum candidates to display
#
def _tailTipToggle() {
def al = _reader.getWidgets().get('accept-line').toString()
def enabled = al == '_tailtip-accept-line' ? true : false
if (enabled) {
_reader.setVariable('LIST_MAX',100)
} else {
_reader.setVariable('LIST_MAX',50)
}
_widget 'tailtip-toggle'
}
widget -N _tailtip-toggle _tailTipToggle
keymap '^[s' _tailtip-toggle
57 changes: 57 additions & 0 deletions demo/src/main/scripts/tget.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// END_HELP
import org.jline.utils.*
import org.jline.builtins.Options

class TerminalCapability {
def terminal

public TerminalCapability(def terminal)
{
this.terminal = terminal
}

def get(def args) {
String[] usage = [
"tget - get terminal capabilities",
"Usage: tget [CAPABILITY]",
" -? --help Displays command help"
]
Options opt = Options.compile(usage).parse(args)
if (opt.isSet("help")) {
throw new Options.HelpException(opt.usage())
}
def out = new TreeMap<>();
def capabilities = InfoCmp.getCapabilitiesByName()
if (!opt.args()) {
for (Map.Entry entry : capabilities.entrySet()) {
out[entry.key] = getCapability(entry.value)
}
} else {
String capability = opt.args().get(0)
if (!capabilities.containsKey(capability)) {
throw new IllegalArgumentException("Unknown capability!")
}
out[capability] = getCapability(capabilities.get(capability))
}
out;
}

def getCapability(def capability) {
def out = null
out = terminal.getStringCapability(capability)
if (!out) {
out = terminal.getNumericCapability(capability)
if (!out) {
out = terminal.getBooleanCapability(capability)
}
}
out
}

}

def static main(def _args){
def terminal = org.jline.builtins.SystemRegistry.get().consoleEngine().reader.getTerminal()
def capability = new TerminalCapability(terminal)
capability.get(_args)
}

0 comments on commit 88e451b

Please sign in to comment.