Skip to content

Commit

Permalink
prnt command: add option --multiColumns
Browse files Browse the repository at this point in the history
displays the collection of simple values in multiple columns
  • Loading branch information
mattirn committed Dec 13, 2022
1 parent ade7806 commit 751a7d7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 21 deletions.
12 changes: 10 additions & 2 deletions console/src/main/java/org/jline/console/Printer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2021, the original author or authors.
* Copyright (c) 2002-2022, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
Expand Down Expand Up @@ -189,8 +189,16 @@ enum TableRows {EVEN, ODD, ALL}
*/
String VALUE_STYLE_ALL = "valueStyleAll";

/**
* Value: Boolean<br>
* Applies: TABLE<br>
* List the collection of simple values in multiple columns
* DEFAULT: list values in one column
*/
String MULTI_COLUMNS = "multiColumns";

List<String> BOOLEAN_KEYS = Arrays.asList(ALL, ONE_ROW_TABLE, ROWNUM, SHORT_NAMES, SKIP_DEFAULT_OPTIONS
, STRUCT_ON_TABLE, TO_STRING, VALUE_STYLE_ALL);
, STRUCT_ON_TABLE, TO_STRING, VALUE_STYLE_ALL, MULTI_COLUMNS);

default void println(Object object) {
println(new HashMap<>(), object);
Expand Down
74 changes: 55 additions & 19 deletions console/src/main/java/org/jline/console/impl/DefaultPrinter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002-2021, the original author or authors.
* Copyright (c) 2002-2022, the original author or authors.
*
* This software is distributable under the BSD license. See the terms of the
* BSD license in the documentation provided with this software.
Expand Down Expand Up @@ -110,6 +110,7 @@ public String[] appendUsage(String[] customUsage) {
" --maxColumnWidth=WIDTH Maximum column width",
" -d --maxDepth=DEPTH Maximum depth objects are resolved",
" -n --maxrows=ROWS Maximum number of lines to display",
" -m --multiColumns Display the collection of simple data in multiple columns",
" --oneRowTable Display one row data on table",
" -h --rowHighlight=ROW Highlight table rows. ROW = EVEN, ODD, ALL",
" -r --rownum Display table row numbers",
Expand Down Expand Up @@ -199,6 +200,9 @@ public Map<String, Object> compileOptions(Options opt) {
throw exception;
}
}
if (opt.isSet(Printer.MULTI_COLUMNS)) {
options.put(Printer.MULTI_COLUMNS, true);
}
options.put("exception", "stack");
return options;
}
Expand Down Expand Up @@ -990,26 +994,58 @@ private void highlightList(Map<String, Object> options
int maxrows = (int)options.get(Printer.MAXROWS);
int indent = (int)options.get(Printer.INDENTION);
List<Integer> tabs = new ArrayList<>();
tabs.add(indent*depth);
if (options.containsKey(Printer.ROWNUM)) {
tabs.add(indent*depth + digits(collection.size()) + 2);
}
options.remove(Printer.MAX_COLUMN_WIDTH);
SyntaxHighlighter highlighter = depth == 0 ? (SyntaxHighlighter)options.get(Printer.STYLE) : null;
for (Object o : collection) {
AttributedStringBuilder asb = new AttributedStringBuilder().tabs(tabs);
if (depth > 0) {
asb.append("\t");
}
SyntaxHighlighter highlighter = depth == 0 ? (SyntaxHighlighter) options.get(Printer.STYLE) : null;
if (!(boolean)options.getOrDefault(Printer.MULTI_COLUMNS, false)) {
tabs.add(indent*depth);
if (options.containsKey(Printer.ROWNUM)) {
asb.styled(prntStyle.resolve(".rn"), Integer.toString(row)).append(":");
asb.append("\t");
row++;
tabs.add(indent*depth + digits(collection.size()) + 2);
}
if (highlighter != null && o instanceof String) {
asb.append(highlighter.highlight((String)o));
} else {
asb.append(highlightValue(options, null, o));
options.remove(Printer.MAX_COLUMN_WIDTH);
for (Object o : collection) {
AttributedStringBuilder asb = new AttributedStringBuilder().tabs(tabs);
if (depth > 0) {
asb.append("\t");
}
if (options.containsKey(Printer.ROWNUM)) {
asb.styled(prntStyle.resolve(".rn"), Integer.toString(row)).append(":");
asb.append("\t");
row++;
}
if (highlighter != null && o instanceof String) {
asb.append(highlighter.highlight((String) o));
} else {
asb.append(highlightValue(options, null, o));
}
println(asb.columnSubSequence(0, width), maxrows);
}
} else {
int maxWidth = 0;
for (Object o : collection) {
AttributedString as;
if (highlighter != null && o instanceof String) {
as = highlighter.highlight((String) o);
} else {
as = highlightValue(options, null, o);
}
if (as.length() > maxWidth) {
maxWidth = as.length();
}
}
int mcw = (int)options.getOrDefault(Printer.MAX_COLUMN_WIDTH, Integer.MAX_VALUE);
maxWidth = mcw < maxWidth ? mcw : maxWidth;
tabs.add(maxWidth + 1);
AttributedStringBuilder asb = new AttributedStringBuilder().tabs(tabs);
for (Object o : collection) {
if (asb.length() + maxWidth > width) {
println(asb.columnSubSequence(0, width), maxrows);
asb = new AttributedStringBuilder().tabs(tabs);
}
if (highlighter != null && o instanceof String) {
asb.append(highlighter.highlight((String) o));
} else {
asb.append(highlightValue(options, null, o));
}
asb.append("\t");
}
println(asb.columnSubSequence(0, width), maxrows);
}
Expand Down

0 comments on commit 751a7d7

Please sign in to comment.