Skip to content

Commit

Permalink
DefaultPrinter can now be used also without ScriptEngine
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jun 2, 2020
1 parent f8b7615 commit aa84d18
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 14 deletions.
43 changes: 32 additions & 11 deletions console/src/main/java/org/jline/console/impl/DefaultPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public class DefaultPrinter extends JlineCommandRegistry implements Printer {
private ConfigurationPath configPath;
private StyleResolver prntStyle;

public DefaultPrinter(ConfigurationPath configPath) {
this(null, configPath);
}

public DefaultPrinter(ScriptEngine engine, ConfigurationPath configPath) {
this.engine = engine;
this.configPath = configPath;
Expand All @@ -69,7 +73,9 @@ public void println(Object object) {
}

@Override
public void println(Map<String, Object> options, Object object) {
public void println(Map<String, Object> optionsIn, Object object) {
Map<String, Object> options = new HashMap<>();
options.putAll(optionsIn);
for (Map.Entry<String, Object> entry
: defaultPrntOptions(options.containsKey(Printer.SKIP_DEFAULT_OPTIONS)).entrySet()) {
options.putIfAbsent(entry.getKey(), entry.getValue());
Expand Down Expand Up @@ -170,8 +176,10 @@ public Exception prntCommand(CommandInput input) {

private List<String> variableReferences() {
List<String> out = new ArrayList<>();
for (String v : engine.find().keySet()) {
out.add("$" + v);
if (engine != null) {
for (String v : engine.find().keySet()) {
out.add("$" + v);
}
}
return out;
}
Expand Down Expand Up @@ -230,7 +238,7 @@ private void manageBooleanOptions(Map<String, Object> options) {
@SuppressWarnings("unchecked")
private Map<String,Object> defaultPrntOptions(boolean skipDefault) {
Map<String, Object> out = new HashMap<>();
if (!skipDefault && engine.hasVariable(VAR_PRNT_OPTIONS)) {
if (engine != null && !skipDefault && engine.hasVariable(VAR_PRNT_OPTIONS)) {
out.putAll((Map<String,Object>)engine.get(VAR_PRNT_OPTIONS));
out.remove(Printer.SKIP_DEFAULT_OPTIONS);
manageBooleanOptions(out);
Expand All @@ -240,6 +248,11 @@ private Map<String,Object> defaultPrntOptions(boolean skipDefault) {
out.putIfAbsent(Printer.INDENTION, PRNT_INDENTION);
out.putIfAbsent(Printer.COLUMNS_OUT, new ArrayList<String>());
out.putIfAbsent(Printer.COLUMNS_IN, new ArrayList<String>());
if (engine == null) {
out.remove(Printer.OBJECT_TO_MAP);
out.remove(Printer.OBJECT_TO_STRING);
out.remove(Printer.HIGHLIGHT_VALUE);
}
return out;
}

Expand Down Expand Up @@ -273,6 +286,9 @@ private void internalPrintln(Map<String, Object> options, Object object) {
if (!style.isEmpty() && object instanceof String) {
highlightAndPrint(width, style, (String) object);
} else if (style.equalsIgnoreCase("JSON")) {
if (engine == null) {
throw new IllegalArgumentException("JSON style not supported!");
}
highlightAndPrint(width, style, engine.toJson(object));
} else if (options.containsKey(Printer.SKIP_DEFAULT_OPTIONS)) {
highlightAndPrint(options, object);
Expand Down Expand Up @@ -325,7 +341,7 @@ private SyntaxHighlighter valueHighlighter(String style) {
out = SyntaxHighlighter.build(style);
} else {
Path nanorc = configPath != null ? configPath.getConfig("jnanorc") : null;
if (engine.hasVariable(VAR_NANORC)) {
if (engine != null && engine.hasVariable(VAR_NANORC)) {
nanorc = Paths.get((String)engine.get(VAR_NANORC));
}
if (nanorc == null) {
Expand Down Expand Up @@ -487,19 +503,24 @@ private Map<String,Object> objectToMap(Map<String, Object> options, Object obj)

@SuppressWarnings("unchecked")
private String objectToString(Map<String, Object> options, Object obj) {
String out = "null";
if (obj != null) {
Map<Class<?>, Object> toString = options.containsKey(Printer.OBJECT_TO_STRING)
? (Map<Class<?>, Object>)options.get(Printer.OBJECT_TO_STRING)
: new HashMap<>();
if (toString.containsKey(obj.getClass())) {
return (String) engine.execute(toString.get(obj.getClass()), obj);
out = (String) engine.execute(toString.get(obj.getClass()), obj);
} else if (objectToString.containsKey(obj.getClass())) {
return objectToString.get(obj.getClass()).apply(obj);
out = objectToString.get(obj.getClass()).apply(obj);
} else if (obj instanceof Class) {
return ((Class<?>) obj).getName();
out = ((Class<?>) obj).getName();
} else if (engine != null) {
out = engine.toString(obj);
} else {
out = obj.toString();
}
}
return engine.toString(obj);
return out;
}

private AttributedString highlightMapValue(Map<String, Object> options, String key, Map<String, Object> map) {
Expand Down Expand Up @@ -774,7 +795,7 @@ private void highlightAndPrint(Map<String, Object> options, Object obj) {
for (Object o : collection) {
List<Object> inner = objectToList(o);
for (int i = 0; i < inner.size(); i++) {
int len1 = engine.toString(inner.get(i)).length() + 1;
int len1 = objectToString(options, inner.get(i)).length() + 1;
if (columns.size() <= i) {
columns.add(len1);
} else if (len1 > columns.get(i)) {
Expand Down Expand Up @@ -868,7 +889,7 @@ private boolean simpleObject(Object obj) {
}

private boolean canConvert(Object obj) {
if (obj == null || obj instanceof Class || obj instanceof Map || simpleObject(obj) || collectionObject(obj)) {
if (engine == null || obj == null || obj instanceof Class || obj instanceof Map || simpleObject(obj) || collectionObject(obj)) {
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,7 @@ private void printCommands(Collection<String> commands, int max) {
}

private String doCommandInfo(List<String> info) {
return info.size() > 0 ? info.get(0) : " ";
return info != null && info.size() > 0 ? info.get(0) : " ";
}

private boolean isInTopics(List<String> args, String name) {
Expand Down
38 changes: 36 additions & 2 deletions console/src/test/java/org/jline/example/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
import org.jline.console.CommandInput;
import org.jline.console.CommandMethods;
import org.jline.console.CommandRegistry;
import org.jline.console.Printer;
import org.jline.widget.AutopairWidgets;
import org.jline.widget.AutosuggestionWidgets;
import org.jline.widget.TailTipWidgets;
import org.jline.widget.TailTipWidgets.TipType;
import org.jline.console.impl.Builtins;
import org.jline.console.impl.DefaultPrinter;
import org.jline.console.impl.SystemRegistryImpl;
import org.jline.keymap.KeyMap;
import org.jline.reader.*;
Expand Down Expand Up @@ -90,8 +92,11 @@ private static class ExampleCommands implements CommandRegistry {
private final Map<String,List<String>> commandInfo = new HashMap<>();
private Map<String,String> aliasCommand = new HashMap<>();
private Exception exception;
private Printer printer;

public ExampleCommands() {
public ExampleCommands(Printer printer) {
this.printer = printer;
commandExecute.put("testprint", new CommandMethods(this::testprint, this::defaultCompleter));
commandExecute.put("testkey", new CommandMethods(this::testkey, this::defaultCompleter));
commandExecute.put("clear", new CommandMethods(this::clear, this::defaultCompleter));
commandExecute.put("autopair", new CommandMethods(this::autopair, this::defaultCompleter));
Expand Down Expand Up @@ -174,6 +179,34 @@ public CmdDesc commandDescription(String command) {
return new CmdDesc(false);
}

private Map<String,Object> fillMap(String name, Integer age, String country, String town) {
Map<String,Object> out = new HashMap<>();
Map<String,String> address = new HashMap<>();
address.put("country", country);
address.put("town", town);
out.put("name", name);
out.put("age", age);
out.put("address", address);
return out;
}

private void testprint(CommandInput input) {
List<Map<String,Object>> data = new ArrayList<>();
data.add(fillMap("heikki", 10, "finland", "helsinki"));
data.add(fillMap("pietro", 11, "italy", "milano"));
data.add(fillMap("john", 12, "england", "london"));
printer.println(data);
Map<String,Object> options = new HashMap<>();
options.put(Printer.STRUCT_ON_TABLE, true);
options.put(Printer.VALUE_STYLE, "classpath:/org/jline/example/gron.nanorc");
printer.println(options,data);
options.clear();
options.put(Printer.COLUMNS, Arrays.asList("name", "age", "address.country", "address.town"));
options.put(Printer.SHORT_NAMES, true);
options.put(Printer.VALUE_STYLE, "classpath:/org/jline/example/gron.nanorc");
printer.println(options,data);
}

private void testkey(CommandInput input) {
try {
terminal().writer().write("Input the key event(Enter to complete): ");
Expand Down Expand Up @@ -313,7 +346,8 @@ public void complete(LineReader reader, ParsedLine line, List<Candidate> candida
builtins.rename(Builtins.Command.TTOP, "top");
builtins.alias("zle", "widget");
builtins.alias("bindkey", "keymap");
ExampleCommands exampleCommands = new ExampleCommands();
DefaultPrinter printer = new DefaultPrinter(null);
ExampleCommands exampleCommands = new ExampleCommands(printer);
SystemRegistryImpl masterRegistry = new SystemRegistryImpl(parser, terminal, Console::workDir, null);
masterRegistry.setCommandRegistries(exampleCommands, builtins);
masterRegistry.addCompleter(completer);
Expand Down
12 changes: 12 additions & 0 deletions console/src/test/resources/org/jline/example/gron.nanorc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
syntax "GRON" "\.gron$"
header "^\[$"

color brightblue "\<[-]?[0-9]*([Ee][+-]?[0-9]+)?\>" "\<[-]?[0](\.[0-9]+)?\>"
color yellow ""(\\.|[^"])*"|'(\\.|[^'])*'|[a-zA-Z]+[a-zA-Z0-9]*"
color cyan "\<null\>"
color brightcyan "\<(true|false)\>"
color brightyellow "\"(\\"|[^"])*\"\s*:" "'(\'|[^'])*'\s*:" "(\[|,)\s*[a-zA-Z0-9]*\s*:"
color white "(:|\[|,|\])"
color magenta "\\u[0-9a-fA-F]{4}|\\[bfnrt'"/\\]"
color ,green "[[:space:]]+$"
color ,red " + +| + +"

0 comments on commit aa84d18

Please sign in to comment.