diff --git a/builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java b/builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
index 25e433a93..52b88dd6d 100644
--- a/builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
+++ b/builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
@@ -440,7 +440,17 @@ public Object engines(Builtins.CommandInput input) {
}
public Object show(Builtins.CommandInput input) {
- return engine.get();
+ final String[] usage = {
+ "show - list console variables",
+ "Usage: show [VARIABLE]",
+ " -? --help Displays command help",
+ };
+ Options opt = Options.compile(usage).parse(input.args());
+ if (opt.isSet("help")) {
+ exception = new HelpException(opt.usage());
+ return null;
+ }
+ return engine.find(input.args().length > 0 ? input.args()[0] : null);
}
public Object del(Builtins.CommandInput input) {
diff --git a/groovy/src/main/java/org/jline/script/GroovyEngine.java b/groovy/src/main/java/org/jline/script/GroovyEngine.java
index 63520c37d..5bda09862 100644
--- a/groovy/src/main/java/org/jline/script/GroovyEngine.java
+++ b/groovy/src/main/java/org/jline/script/GroovyEngine.java
@@ -27,6 +27,7 @@
* @author Matti Rinta-Nikkola
*/
public class GroovyEngine implements ScriptEngine {
+ private static final String REGEX_SYSTEM_VAR = "[A-Z]{1}[A-Z_]*";
private GroovyShell shell;
private Binding sharedData;
private Map imports = new HashMap();
@@ -53,8 +54,16 @@ public Object get(String name) {
@SuppressWarnings("unchecked")
@Override
- public Map get() {
- return sharedData.getVariables();
+ public Map find(String name) {
+ Map out = new HashMap<>();
+ if (name == null) {
+ out = sharedData.getVariables();
+ } else {
+ for (String v : internalFind(name)) {
+ out.put(v, get(v));
+ }
+ }
+ return out;
}
@Override
@@ -110,6 +119,19 @@ public List getExtensions() {
}
@SuppressWarnings("unchecked")
+ private List internalFind(String var) {
+ List out = new ArrayList<>();
+ if(!var.contains(".") && var.contains("*")) {
+ var = var.replaceAll("\\*", ".*");
+ }
+ for (String v : (Set)sharedData.getVariables().keySet()) {
+ if (v.matches(var)) {
+ out.add(v);
+ }
+ }
+ return out;
+ }
+
private void del(String var) {
if (var == null) {
return;
@@ -119,16 +141,8 @@ private void del(String var) {
} else if (sharedData.hasVariable(var)) {
sharedData.getVariables().remove(var);
} else if (!var.contains(".") && var.contains("*")) {
- var = var.replaceAll("\\*", ".*");
- Map vars=sharedData.getVariables();
- List todel = new ArrayList();
- for (Map.Entry entry : vars.entrySet()){
- if (!entry.getKey().equals("_") && entry.getKey().matches(var)) {
- todel.add(entry.getKey());
- }
- }
- for (String v : todel){
- if (sharedData.hasVariable(v)) {
+ for (String v : internalFind(var)){
+ if (sharedData.hasVariable(v) && !v.equals("_") && !v.matches(REGEX_SYSTEM_VAR)) {
sharedData.getVariables().remove(v);
}
}
@@ -165,29 +179,29 @@ public List highlight(Map options, Object obj)
throw new IllegalArgumentException("Bad style option: " + style);
} else {
try {
- out = formatInternal(options, obj);
+ out = internalHighlight(options, obj);
} catch (Exception e) {
- out = formatInternal(options, Utils.convert(obj));
+ out = internalHighlight(options, Utils.convert(obj));
}
}
return out;
}
@SuppressWarnings("unchecked")
- private List formatInternal(Map options, Object obj) {
+ private List internalHighlight(Map options, Object obj) {
List out = new ArrayList<>();
int width = (int)options.getOrDefault("width", Integer.MAX_VALUE);
if (obj == null) {
// do nothing
} else if (obj instanceof Map) {
- out = formatMap((Map)obj, width);
+ out = highlightMap((Map)obj, width);
} else if (obj instanceof Collection>) {
Collection> collection = (Collection>)obj;
if (!collection.isEmpty()) {
if (collection.size() == 1) {
Object elem = collection.iterator().next();
if (elem instanceof Map) {
- out = formatMap((Map)elem, width);
+ out = highlightMap((Map)elem, width);
} else {
out.add(new AttributedString(Utils.toString(obj)));
}
@@ -284,7 +298,7 @@ private void toTabStops(List columns) {
}
}
- private List formatMap(Map map, int width) {
+ private List highlightMap(Map map, int width) {
List out = new ArrayList<>();
int max = map.keySet().stream().map(String::length).max(Integer::compareTo).get();
for (Map.Entry entry : map.entrySet()) {
diff --git a/reader/src/main/java/org/jline/reader/ScriptEngine.java b/reader/src/main/java/org/jline/reader/ScriptEngine.java
index 65ba17a9b..0d4a24711 100644
--- a/reader/src/main/java/org/jline/reader/ScriptEngine.java
+++ b/reader/src/main/java/org/jline/reader/ScriptEngine.java
@@ -16,6 +16,11 @@
import org.jline.utils.AttributedString;
+/**
+ * Manage scriptEngine variables, statements and script execution.
+ *
+ * @author Matti Rinta-Nikkola
+ */
public interface ScriptEngine {
String getEngineName();
@@ -28,7 +33,11 @@ public interface ScriptEngine {
Object get(String name);
- Map get();
+ default Map find() {
+ return find(null);
+ }
+
+ Map find(String name);
void del(String... vars);