Skip to content

Commit

Permalink
prnt command: added options include and exclude
Browse files Browse the repository at this point in the history
  • Loading branch information
mattirn committed Apr 18, 2020
1 parent 0bd926d commit d53681a
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions builtins/src/main/java/org/jline/builtins/ConsoleEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,8 @@ private Map<String,Object> defaultPrntOptions() {
out.putIfAbsent("maxrows", PRNT_MAX_ROWS);
out.putIfAbsent("maxDepth", PRNT_MAX_DEPTH);
out.putIfAbsent("indention", PRNT_INDENTION);
out.putIfAbsent("columnsOut", new ArrayList<String>());
out.putIfAbsent("columnsIn", new ArrayList<String>());
return out;
}

Expand Down Expand Up @@ -965,10 +967,24 @@ public void println(Map<String, Object> options, Object object) {
internalPrintln(options, object);
}

@SuppressWarnings("unchecked")
private void internalPrintln(Map<String, Object> options, Object object) {
if (object == null) {
return;
}
if (options.containsKey("exclude")) {
List<String> colOut = optionList("exclude", options);
List<String> colIn = optionList("columnsIn", options);
colIn.removeAll(colOut);
colOut.addAll((List<String>)options.get("columnsOut"));
options.put("columnsIn", colIn);
options.put("columnsOut", colOut);
}
if (options.containsKey("include")) {
List<String> colIn = optionList("include", options);
colIn.addAll((List<String>)options.get("columnsIn"));
options.put("columnsIn", colIn);
}
options.putIfAbsent("width", terminal().getSize().getColumns());
String style = (String) options.getOrDefault("style", "");
int width = (int) options.get("width");
Expand Down Expand Up @@ -1070,7 +1086,8 @@ private List<String> optionList(String key, Map<String,Object> options) {
} else if (options.get(key) instanceof Collection) {
out.addAll((Collection<String>)options.get(key));
} else {
throw new IllegalArgumentException("Unsupported option list type: " + options.get(key).getClass());
throw new IllegalArgumentException("Unsupported option list: {key: " + key
+ ", type: " + options.get(key).getClass() + "}");
}
}
return out;
Expand Down Expand Up @@ -1304,7 +1321,7 @@ private List<AttributedString> highlight(Map<String, Object> options, Object obj
int headerWidth = 0;
Set<String> refKeys = new HashSet<>();
for (int i = 0; i < _header.size(); i++) {
if (!map.containsKey(_header.get(i).split("\\.")[0])) {
if (!map.containsKey(_header.get(i).split("\\.")[0]) && !map.containsKey(_header.get(i))) {
continue;
}
if (options.containsKey("columns")) {
Expand All @@ -1315,7 +1332,8 @@ private List<AttributedString> highlight(Map<String, Object> options, Object obj
continue;
}
}
refKeys.add(_header.get(i).split("\\.")[0]);
String rk = map.containsKey(_header.get(i)) ? _header.get(i) : _header.get(i).split("\\.")[0];
refKeys.add(rk);
header.add(_header.get(i));
columns.add(_header.get(i).length() + 1);
headerWidth += _header.get(i).length() + 1;
Expand Down Expand Up @@ -1597,7 +1615,9 @@ private Object prnt(Builtins.CommandInput input) {
" -? --help Displays command help",
" -a --all Ignore columnsOut configuration",
" -c --columns=COLUMNS,... Display given columns on table",
" -i --indention=IDENTION Indention size",
" -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 rows on table",
Expand Down Expand Up @@ -1636,6 +1656,12 @@ private Object prnt(Builtins.CommandInput input) {
if (opt.isSet("columns")) {
options.put("columns", Arrays.asList(opt.get("columns").split(",")));
}
if (opt.isSet("exclude")) {
options.put("exclude", Arrays.asList(opt.get("exclude").split(",")));
}
if (opt.isSet("include")) {
options.put("include", Arrays.asList(opt.get("include").split(",")));
}
if (opt.isSet("all")) {
options.put("all", true);
}
Expand Down

0 comments on commit d53681a

Please sign in to comment.