Skip to content

Commit

Permalink
prnt command: moved implementation to DefaultPrinter
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Jun 2, 2020
1 parent fcac969 commit f8b7615
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 128 deletions.
13 changes: 0 additions & 13 deletions console/src/main/java/org/jline/console/ConsoleEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ static String plainCommand(String command) {
*/
void setLineReader(LineReader reader);

/**
* Sets printer
* @param printer the printer
*/
public void setPrinter(Printer printer);

/**
* Sets systemRegistry
* @param systemRegistry SystemRegistry
Expand Down Expand Up @@ -202,13 +196,6 @@ default Object execute(File script) throws Exception {
*/
void println(Object object);

/**
* Print object.
* @param options println options
* @param object object to print
*/
void println(Map<String, Object> options, Object object);

/**
* Create console variable
* @param name name of the variable
Expand Down
11 changes: 11 additions & 0 deletions console/src/main/java/org/jline/console/Printer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
*/
package org.jline.console;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jline.reader.Completer;

/**
* Print object to the console.
*
Expand Down Expand Up @@ -178,4 +181,12 @@ default void println(Object object) {

void println(Map<String,Object> options, Object object);

default Exception prntCommand(CommandInput input) {
return null;
}

default List<Completer> prntCompleter(String command) {
return new ArrayList<>();
}

}
122 changes: 11 additions & 111 deletions console/src/main/java/org/jline/console/impl/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
*
* @author <a href="mailto:matti.rintanikkola@gmail.com">Matti Rinta-Nikkola</a>
*/
public class ConsoleEngineImpl extends JlineCommandRegistry implements ConsoleEngine, Printer {
public class ConsoleEngineImpl extends JlineCommandRegistry implements ConsoleEngine {
public enum Command {SHOW
, DEL
, PRNT
Expand All @@ -72,17 +72,18 @@ public enum Command {SHOW
private boolean executing = false;
private Printer printer;

public ConsoleEngineImpl(ScriptEngine engine
public ConsoleEngineImpl(ScriptEngine engine, Printer printer
, Supplier<Path> workDir, ConfigurationPath configPath) throws IOException {
this(null, engine, workDir, configPath);
this(null, engine, printer, workDir, configPath);
}

@SuppressWarnings("unchecked")
public ConsoleEngineImpl(Set<Command> commands, ScriptEngine engine
public ConsoleEngineImpl(Set<Command> commands, ScriptEngine engine, Printer printer
, Supplier<Path> workDir, ConfigurationPath configPath) throws IOException {
super();
this.engine = engine;
this.workDir = workDir;
this.printer = printer;
Map<Command,String> commandName = new HashMap<>();
Map<Command,CommandMethods> commandExecute = new HashMap<>();
Set<Command> cmds = null;
Expand All @@ -96,7 +97,7 @@ public ConsoleEngineImpl(Set<Command> commands, ScriptEngine engine
}
commandExecute.put(Command.DEL, new CommandMethods(this::del, this::variableCompleter));
commandExecute.put(Command.SHOW, new CommandMethods(this::show, this::variableCompleter));
commandExecute.put(Command.PRNT, new CommandMethods(this::prnt, this::prntCompleter));
commandExecute.put(Command.PRNT, new CommandMethods(this::prnt, printer::prntCompleter));
commandExecute.put(Command.SLURP, new CommandMethods(this::slurpcmd, this::slurpCompleter));
commandExecute.put(Command.ALIAS, new CommandMethods(this::aliascmd, this::aliasCompleter));
commandExecute.put(Command.UNALIAS, new CommandMethods(this::unalias, this::unaliasCompleter));
Expand All @@ -117,11 +118,6 @@ public void setLineReader(LineReader reader) {
this.reader = reader;
}

@Override
public void setPrinter(Printer printer) {
this.printer = printer;
}

private Parser parser() {
return reader.getParser();
}
Expand Down Expand Up @@ -868,11 +864,6 @@ public void println(Object object) {
printer.println(object);
}

@Override
public void println(Map<String, Object> options, Object object) {
printer.println(options, object);
}

private Object show(CommandInput input) {
final String[] usage = {
"show - list console variables",
Expand All @@ -883,7 +874,7 @@ private Object show(CommandInput input) {
parseOptions(usage, input.args());
Map<String, Object> options = new HashMap<>();
options.put(Printer.MAX_DEPTH, 0);
println(options, engine.find(input.args().length > 0 ? input.args()[0] : null));
printer.println(options, engine.find(input.args().length > 0 ? input.args()[0] : null));
} catch (Exception e) {
exception = e;
}
Expand All @@ -906,89 +897,9 @@ private Object del(CommandInput input) {
}

private Object prnt(CommandInput input) {
final String[] usage = {
"prnt - print object",
"Usage: prnt [OPTIONS] object",
" -? --help Displays command help",
" -a --all Ignore columnsOut configuration",
" -c --columns=COLUMNS,... Display given columns on table",
" -e --exclude=COLUMNS,... Exclude given columns on table",
" -i --include=COLUMNS,... Include given columns on table",
" --indention=IDENTION Indention size",
" --maxColumnWidth=WIDTH Maximum column width",
" -d --maxDepth=DEPTH Maximum depth objects are resolved",
" --maxrows=ROWS Maximum number of lines to display",
" --oneRowTable Display one row data on table",
" -r --rownum Display table row numbers",
" --shortNames Truncate table column names (property.field -> field)",
" --skipDefaultOptions Ignore all options defined in PRNT_OPTIONS",
" --structsOnTable Display structs and lists on table",
" -s --style=STYLE Use nanorc STYLE",
" --toString use object's toString() method to get print value",
" DEFAULT: object's fields are put to property map before printing",
" --valueStyle=STYLE Use nanorc style to highlight column/map values",
" -w --width=WIDTH Display width (default terminal width)"
};
try {
Options opt = parseOptions(usage, input.xargs());
Map<String, Object> options = new HashMap<>();
if (opt.isSet(Printer.SKIP_DEFAULT_OPTIONS)) {
options.put(Printer.SKIP_DEFAULT_OPTIONS, true);
} else if (opt.isSet(Printer.STYLE)) {
options.put(Printer.STYLE, opt.get(Printer.STYLE));
}
if (opt.isSet(Printer.TO_STRING)) {
options.put(Printer.TO_STRING, true);
}
if (opt.isSet(Printer.WIDTH)) {
options.put(Printer.WIDTH, opt.getNumber(Printer.WIDTH));
}
if (opt.isSet(Printer.ROWNUM)) {
options.put(Printer.ROWNUM, true);
}
if (opt.isSet(Printer.ONE_ROW_TABLE)) {
options.put(Printer.ONE_ROW_TABLE, true);
}
if (opt.isSet(Printer.SHORT_NAMES)) {
options.put(Printer.SHORT_NAMES, true);
}
if (opt.isSet(Printer.STRUCT_ON_TABLE)) {
options.put(Printer.STRUCT_ON_TABLE, true);
}
if (opt.isSet(Printer.COLUMNS)) {
options.put(Printer.COLUMNS, Arrays.asList(opt.get(Printer.COLUMNS).split(",")));
}
if (opt.isSet(Printer.EXCLUDE)) {
options.put(Printer.EXCLUDE, Arrays.asList(opt.get(Printer.EXCLUDE).split(",")));
}
if (opt.isSet(Printer.INCLUDE)) {
options.put(Printer.INCLUDE, Arrays.asList(opt.get(Printer.INCLUDE).split(",")));
}
if (opt.isSet(Printer.ALL)) {
options.put(Printer.ALL, true);
}
if (opt.isSet(Printer.MAXROWS)) {
options.put(Printer.MAXROWS, opt.getNumber(Printer.MAXROWS));
}
if (opt.isSet(Printer.MAX_COLUMN_WIDTH)) {
options.put(Printer.MAX_COLUMN_WIDTH, opt.getNumber(Printer.MAX_COLUMN_WIDTH));
}
if (opt.isSet(Printer.MAX_DEPTH)) {
options.put(Printer.MAX_DEPTH, opt.getNumber(Printer.MAX_DEPTH));
}
if (opt.isSet(Printer.INDENTION)) {
options.put(Printer.INDENTION, opt.getNumber(Printer.INDENTION));
}
if (opt.isSet(Printer.VALUE_STYLE)) {
options.put(Printer.VALUE_STYLE, opt.get(Printer.VALUE_STYLE));
}
options.put("exception", "stack");
List<Object> args = opt.argObjects();
if (args.size() > 0) {
println(options, args.get(0));
}
} catch (Exception e) {
exception = e;
Exception result = printer.prntCommand(input);
if (result != null) {
exception = result;
}
return null;
}
Expand Down Expand Up @@ -1117,7 +1028,7 @@ private Object pipe(CommandInput input) {
}
} else if (opt.isSet("list") || opt.args().size() == 0) {
options.put(Printer.MAX_DEPTH, 0);
println(options, pipes);
printer.println(options, pipes);
} else if (opt.args().size() != 3) {
exception = new IllegalArgumentException("Bad number of arguments!");
} else if (systemRegistry.getPipeNames().contains(opt.args().get(0))) {
Expand Down Expand Up @@ -1226,17 +1137,6 @@ private List<String> variableReferences() {
return out;
}

private List<Completer> prntCompleter(String command) {
List<Completer> completers = new ArrayList<>();
completers.add(new ArgumentCompleter(NullCompleter.INSTANCE
, new OptionCompleter(Arrays.asList(new StringsCompleter(this::variableReferences)
, NullCompleter.INSTANCE)
, this::commandOptions
, 1)
));
return completers;
}

private static class AliasValueCompleter implements Completer {
private final Map<String,String> aliases;

Expand Down
118 changes: 117 additions & 1 deletion console/src/main/java/org/jline/console/impl/DefaultPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@
import org.jline.builtins.ConfigurationPath;
import org.jline.builtins.Options;
import org.jline.builtins.Styles;
import org.jline.builtins.Completers.OptionCompleter;
import org.jline.builtins.Nano.SyntaxHighlighter;
import org.jline.console.CmdDesc;
import org.jline.console.CommandInput;
import org.jline.console.Printer;
import org.jline.console.ScriptEngine;
import org.jline.console.SystemRegistry;
import org.jline.reader.Completer;
import org.jline.reader.impl.completer.ArgumentCompleter;
import org.jline.reader.impl.completer.NullCompleter;
import org.jline.reader.impl.completer.StringsCompleter;
import org.jline.terminal.Terminal;
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStringBuilder;
Expand All @@ -35,7 +41,7 @@
*
* @author <a href="mailto:matti.rintanikkola@gmail.com">Matti Rinta-Nikkola</a>
*/
public class DefaultPrinter implements Printer {
public class DefaultPrinter extends JlineCommandRegistry implements Printer {
private static final String VAR_PRNT_OPTIONS = "PRNT_OPTIONS";
private static final String VAR_NANORC = "NANORC";
private static final int PRNT_MAX_ROWS = 100000;
Expand Down Expand Up @@ -72,6 +78,116 @@ public void println(Map<String, Object> options, Object object) {
internalPrintln(options, object);
}

@Override
public Exception prntCommand(CommandInput input) {
final String[] usage = {
"prnt - print object",
"Usage: prnt [OPTIONS] object",
" -? --help Displays command help",
" -a --all Ignore columnsOut configuration",
" -c --columns=COLUMNS,... Display given columns on table",
" -e --exclude=COLUMNS,... Exclude given columns on table",
" -i --include=COLUMNS,... Include given columns on table",
" --indention=IDENTION Indention size",
" --maxColumnWidth=WIDTH Maximum column width",
" -d --maxDepth=DEPTH Maximum depth objects are resolved",
" --maxrows=ROWS Maximum number of lines to display",
" --oneRowTable Display one row data on table",
" -r --rownum Display table row numbers",
" --shortNames Truncate table column names (property.field -> field)",
" --skipDefaultOptions Ignore all options defined in PRNT_OPTIONS",
" --structsOnTable Display structs and lists on table",
" -s --style=STYLE Use nanorc STYLE",
" --toString use object's toString() method to get print value",
" DEFAULT: object's fields are put to property map before printing",
" --valueStyle=STYLE Use nanorc style to highlight column/map values",
" -w --width=WIDTH Display width (default terminal width)"
};
Exception out = null;
try {
Options opt = parseOptions(usage, input.xargs());
Map<String, Object> options = new HashMap<>();
if (opt.isSet(Printer.SKIP_DEFAULT_OPTIONS)) {
options.put(Printer.SKIP_DEFAULT_OPTIONS, true);
} else if (opt.isSet(Printer.STYLE)) {
options.put(Printer.STYLE, opt.get(Printer.STYLE));
}
if (opt.isSet(Printer.TO_STRING)) {
options.put(Printer.TO_STRING, true);
}
if (opt.isSet(Printer.WIDTH)) {
options.put(Printer.WIDTH, opt.getNumber(Printer.WIDTH));
}
if (opt.isSet(Printer.ROWNUM)) {
options.put(Printer.ROWNUM, true);
}
if (opt.isSet(Printer.ONE_ROW_TABLE)) {
options.put(Printer.ONE_ROW_TABLE, true);
}
if (opt.isSet(Printer.SHORT_NAMES)) {
options.put(Printer.SHORT_NAMES, true);
}
if (opt.isSet(Printer.STRUCT_ON_TABLE)) {
options.put(Printer.STRUCT_ON_TABLE, true);
}
if (opt.isSet(Printer.COLUMNS)) {
options.put(Printer.COLUMNS, Arrays.asList(opt.get(Printer.COLUMNS).split(",")));
}
if (opt.isSet(Printer.EXCLUDE)) {
options.put(Printer.EXCLUDE, Arrays.asList(opt.get(Printer.EXCLUDE).split(",")));
}
if (opt.isSet(Printer.INCLUDE)) {
options.put(Printer.INCLUDE, Arrays.asList(opt.get(Printer.INCLUDE).split(",")));
}
if (opt.isSet(Printer.ALL)) {
options.put(Printer.ALL, true);
}
if (opt.isSet(Printer.MAXROWS)) {
options.put(Printer.MAXROWS, opt.getNumber(Printer.MAXROWS));
}
if (opt.isSet(Printer.MAX_COLUMN_WIDTH)) {
options.put(Printer.MAX_COLUMN_WIDTH, opt.getNumber(Printer.MAX_COLUMN_WIDTH));
}
if (opt.isSet(Printer.MAX_DEPTH)) {
options.put(Printer.MAX_DEPTH, opt.getNumber(Printer.MAX_DEPTH));
}
if (opt.isSet(Printer.INDENTION)) {
options.put(Printer.INDENTION, opt.getNumber(Printer.INDENTION));
}
if (opt.isSet(Printer.VALUE_STYLE)) {
options.put(Printer.VALUE_STYLE, opt.get(Printer.VALUE_STYLE));
}
options.put("exception", "stack");
List<Object> args = opt.argObjects();
if (args.size() > 0) {
println(options, args.get(0));
}
} catch (Exception e) {
out = e;
}
return out;
}

private List<String> variableReferences() {
List<String> out = new ArrayList<>();
for (String v : engine.find().keySet()) {
out.add("$" + v);
}
return out;
}

@Override
public List<Completer> prntCompleter(String command) {
List<Completer> completers = new ArrayList<>();
completers.add(new ArgumentCompleter(NullCompleter.INSTANCE
, new OptionCompleter(Arrays.asList(new StringsCompleter(this::variableReferences)
, NullCompleter.INSTANCE)
, this::commandOptions
, 1)
));
return completers;
}

/**
* Override ScriptEngine toMap() method
* @param objectToMap key: object class, value: toMap function
Expand Down

0 comments on commit f8b7615

Please sign in to comment.