Skip to content

Commit

Permalink
Improved parameter expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jan 16, 2020
1 parent 8293232 commit b1e1ff9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
14 changes: 8 additions & 6 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,15 @@ public Widgets.CmdDesc commandDescription(String command) {
return null;
}

private Object[] expandVariables(String[] args) throws Exception {
private Object[] expandParameters(String[] args) throws Exception {
Object[] out = new Object[args.length];
for (int i = 0; i < args.length; i++) {
if (args[i].startsWith("$")) {
if (args[i].startsWith("${")) {
out[i] = engine.execute(expandName(args[i]));
} else if (args[i].startsWith("$")) {
out[i] = engine.get(expandName(args[i]));
} else {
out[i] = args[i];
out[i] = engine.expandParameter(args[i]);
}
}
return out;
Expand Down Expand Up @@ -218,7 +220,7 @@ public Object execute(ParsedLine pl) throws Exception {
String name = file.getName();
String ext = name.contains(".") ? name.substring(name.lastIndexOf(".") + 1) : "";
if(engine.getExtensions().contains(ext)) {
out = engine.execute(file, expandVariables(args));
out = engine.execute(file, expandParameters(args));
out = postProcess(pl.line(), out);
} else if (scriptExtension.equals(ext)) {
boolean done = false;
Expand Down Expand Up @@ -394,7 +396,7 @@ public Object echo(Builtins.CommandInput input) {
return null;
}
try {
Object[] args = expandVariables(opt.args().stream().toArray(String[]::new));
Object[] args = expandParameters(opt.args().stream().toArray(String[]::new));
if (args.length > 0) {
println(defaultPrntOptions(), args[0]);
}
Expand All @@ -406,7 +408,7 @@ public Object echo(Builtins.CommandInput input) {

public Object prnt(Builtins.CommandInput input) {
try {
invokePrnt(expandVariables(input.args()));
invokePrnt(expandParameters(input.args()));
} catch (Exception e) {
exception = e;
}
Expand Down
14 changes: 11 additions & 3 deletions groovy/src/main/groovy/org/jline/groovy/Utils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package org.jline.groovy;

import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import groovy.json.JsonParserType

public class Utils {

Expand All @@ -19,13 +20,20 @@ public class Utils {
object.toString()
}

static Object toObject(String json) {
def slurper = new JsonSlurper(type: JsonParserType.LAX)
slurper.parseText(json)
}

static Object convert(Object object) {
def slurper = new JsonSlurper()
slurper.parseText(JsonOutput.toJson(object))
}

static String toJson(Object object) {
return object instanceof String ? JsonOutput.prettyPrint(object)
: JsonOutput.prettyPrint(JsonOutput.toJson(object))
String json = object instanceof String ? object : JsonOutput.toJson(object)
((json.startsWith("{") && json.endsWith("}"))
|| (json.startsWith("[") && json.endsWith("]"))) ? JsonOutput.prettyPrint(json) : json
}

}
15 changes: 15 additions & 0 deletions groovy/src/main/java/org/jline/script/GroovyEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ public Map<String,Object> get() {
return sharedData.getVariables();
}

@Override
public Object expandParameter(String variable) {
Object out = variable;
if (variable.startsWith("[") && variable.endsWith("]")) {
try {
out = execute(variable);
} catch (Exception e) {
out = Utils.toObject(variable); // try json
}
} else if (variable.startsWith("{") && variable.endsWith("}")) {
out = Utils.toObject(variable);
}
return out;
}

@Override
public Object execute(File script, Object[] args) throws Exception {
sharedData.setProperty("_args", args);
Expand Down
2 changes: 2 additions & 0 deletions reader/src/main/java/org/jline/reader/ScriptEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public interface ScriptEngine {

List<AttributedString> highlight(Map<String, Object> options, Object object);

Object expandParameter(String variable);

Object execute(String statement) throws Exception;

default Object execute(File script) throws Exception {
Expand Down

0 comments on commit b1e1ff9

Please sign in to comment.