Skip to content

Commit

Permalink
REPL console: parameter ${@} expansion and two new pipes in demo
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Mar 10, 2020
1 parent 8912399 commit 71e3564
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 17 deletions.
9 changes: 8 additions & 1 deletion builtins/src/main/java/org/jline/builtins/ConsoleEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ static String plainCommand(String command) {
*/
String expandCommandLine(String line);

/**
* Expands parameter list to string
* @param params list of script parameters
* @return expanded parameters list
*/
String expandToList(List<String> params);

/**
* Returns all scripts found from PATH
* @return map keys have script file names and value is true if it is console script
Expand Down Expand Up @@ -182,7 +189,7 @@ default Object execute(File script) throws Exception {
/**
* Test if variable with name exists
* @param name name of the variable
* @return true if variable with name exists
* @return true if variable with name exists
*/
boolean hasVariable(String name);

Expand Down
58 changes: 48 additions & 10 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,32 @@ public Object[] expandParameters(String[] args) throws Exception {
return out;
}

private String expandToList(String[] args) {
return expandToList(Arrays.asList(args));
}

@Override
public String expandToList(List<String> params) {
StringBuilder sb = new StringBuilder();
sb.append("[");
boolean first = true;
for (int j = 0; j < params.size(); j++) {
if (!first) {
sb.append(",");
}
if (params.get(j).equalsIgnoreCase("true") || params.get(j).equalsIgnoreCase("false") || params.get(j).equalsIgnoreCase("null")) {
sb.append(params.get(j).toLowerCase());
} else if (params.get(j).matches("-?\\d+(\\.\\d+)?")) {
sb.append(params.get(j));
} else {
sb.append(params.get(j).startsWith("$") ? params.get(j).substring(1) : quote(params.get(j)));
}
first = false;
}
sb.append("]");
return sb.toString();
}

private String expandName(String name) {
String regexVar = "[a-zA-Z_]{1,}[a-zA-Z0-9_-]*";
String out = name;
Expand Down Expand Up @@ -502,6 +528,8 @@ private void internalExecute() throws Exception {
line = line.replaceAll("\\$\\{" + i + "(|:-.*)\\}",
args[i].startsWith("$") ? expandName(args[i]) : quote(args[i]));
}
line = line.replaceAll("\\$\\{@\\}", expandToList(args));
line = line.replaceAll("\\$@", expandToList(args));
line = line.replaceAll("\\s\\$\\d\\b", "");
line = line.replaceAll("\\$\\{\\d+\\}", "");
Matcher matcher=Pattern.compile("\\$\\{\\d+:-(.*?)\\}").matcher(line);
Expand Down Expand Up @@ -599,7 +627,7 @@ public String expandCommandLine(String line) {
argv[i] = quote(argv[i]);
}
}
String cmd = hasAlias(ws.get(0).substring(idx + 1)) ? getAlias(ws.get(0).substring(idx + 1))
String cmd = hasAlias(ws.get(0).substring(idx + 1)) ? getAlias(ws.get(0).substring(idx + 1))
: ws.get(0).substring(idx + 1);
sb.append("org.jline.builtins.SystemRegistry.get().invoke('" + cmd + "'");
for (int i = 1; i < argv.length; i++) {
Expand Down Expand Up @@ -897,8 +925,17 @@ private void highlightAndPrint(int width, String style, String object) {
}

private Map<String,Object> keyToString(Map<Object,Object> map) {
return map.entrySet().stream()
.collect(Collectors.toMap(e -> e.getKey().toString(), Map.Entry::getValue));
Map<String,Object> out = new HashMap<>();
for (Map.Entry<Object,Object> entry : map.entrySet()) {
if (entry.getKey() instanceof String) {
out.put((String)entry.getKey(), entry.getValue());
} else if (entry.getKey() != null) {
out.put(entry.getKey().toString(), entry.getValue());
} else {
out.put("null", entry.getValue());
}
}
return out;
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -1192,14 +1229,15 @@ private Object aliascmd(Builtins.CommandInput input) {
} else if (args.size() == 1) {
out = aliases.getOrDefault(args.get(0), null);
} else {
for (int i = 1; i < args.size(); i++) {
for (int j = 0; j < 10; j++) {
args.set(i, args.get(i).replaceAll("%" + j , "\\$" + j));
args.set(i, args.get(i).replaceAll("%\\{" + j + "\\}", "\\$\\{" + j + "\\}"));
args.set(i, args.get(i).replaceAll("%\\{" + j + ":-", "\\$\\{" + j + ":-"));
}
String alias = String.join(" ", args.subList(1, args.size()));
for (int j = 0; j < 10; j++) {
alias = alias.replaceAll("%" + j , "\\$" + j);
alias = alias.replaceAll("%\\{" + j + "\\}", "\\$\\{" + j + "\\}");
alias = alias.replaceAll("%\\{" + j + ":-", "\\$\\{" + j + ":-");
}
aliases.put(args.get(0), String.join(" ", args.subList(1, args.size())));
alias = alias.replaceAll("%@" , "\\$@");
alias = alias.replaceAll("%\\{@\\}", "\\$\\{@\\}");
aliases.put(args.get(0), alias);
engine.persist(aliasFile, aliases);
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,8 @@ private List<CommandData> compileCommandLine(String commandLine) {
pipeAlias = pipeAlias.replaceAll("\\s\\$" + j + "\\b", " " + args.get(j));
pipeAlias = pipeAlias.replaceAll("\\$\\{" + j + "(|:-.*)\\}", args.get(j));
}
pipeAlias = pipeAlias.replaceAll("\\$\\{@\\}", consoleEngine().expandToList(args));
pipeAlias = pipeAlias.replaceAll("\\$@", consoleEngine().expandToList(args));
pipeAlias = pipeAlias.replaceAll("\\s+\\$\\d\\b", "");
pipeAlias = pipeAlias.replaceAll("\\s+\\$\\{\\d+\\}", "");
pipeAlias = pipeAlias.replaceAll("\\$\\{\\d+\\}", "");
Expand Down Expand Up @@ -695,8 +697,8 @@ private List<CommandData> compileCommandLine(String commandLine) {
int idx = subLine.indexOf(" ");
subLine = idx > 0 ? subLine.substring(idx + 1) : "";
}
rawLine += fixes.get(0)
+ (consoleId != null ? consoleEngine().expandCommandLine(subLine) : subLine)
rawLine += fixes.get(0)
+ (consoleId != null ? consoleEngine().expandCommandLine(subLine) : subLine)
+ fixes.get(1);
statement = true;
}
Expand Down Expand Up @@ -854,7 +856,7 @@ public String line() {
public String command() {
return ConsoleEngine.plainCommand(command);
}

public String rawCommand() {
return command;
}
Expand Down
3 changes: 3 additions & 0 deletions demo/src/main/scripts/hello2.jline
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ array.each {
:prnt $resp
}.identity{}

params = $@
prnt -s JSON $params

println('hello ' + ${1:-world} + '!')

exit 'ok'
5 changes: 4 additions & 1 deletion demo/src/main/scripts/init.jline
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ pipe grep '.collect{it.toString()}.findAll{it=~/' '/}'
alias null '|& identity{}'
alias xargs '|; %{0} %{1} %{2} %{3} %{4} %{5} %{6} %{7} %{8} %{9}'
alias to '|; %{0} ='
alias select '|. def m = it; def out = [:]; %{@}.each{def v = m; it.split("\\.").each{v=v[it]}; out.put(it,v)}; out'
alias sort '|& sort{def p = %{@}; p[0].startsWith("-") ? -it[p[0].substring(1)] : it[p[0]]}'
alias distinct '|& unique()'
#
# create test-widget and bind it to ctrl-alt-x
# It will read widget name from buffer and execute it
Expand All @@ -40,7 +43,7 @@ def _testWidget() {
def name = _buffer().toString().split('\\s+')[0]
_widget "$name"
}
widget -N test-widget _testWidget
widget -N test-widget _testWidget
keymap '^[^x' test-widget
#
# create _tailtip-toggle widget that changes also maximum candidates to display
Expand Down
4 changes: 2 additions & 2 deletions groovy/src/main/groovy/org/jline/groovy/Utils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class Utils {
private Utils() {}

static String toString(Object object) {
object.toString()
object ? object.toString() : 'null'
}

static Object toObject(String json) {
Expand All @@ -29,7 +29,7 @@ public class Utils {
}

static Map<String,Object> toMap(Object object) {
object.properties
object ? object.properties : null
}

static String toJson(Object object) {
Expand Down

0 comments on commit 71e3564

Please sign in to comment.