Skip to content

Commit 751a7d7

Browse files
committed
prnt command: add option --multiColumns
displays the collection of simple values in multiple columns
1 parent ade7806 commit 751a7d7

File tree

2 files changed

+65
-21
lines changed

2 files changed

+65
-21
lines changed

console/src/main/java/org/jline/console/Printer.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002-2021, the original author or authors.
2+
* Copyright (c) 2002-2022, the original author or authors.
33
*
44
* This software is distributable under the BSD license. See the terms of the
55
* BSD license in the documentation provided with this software.
@@ -189,8 +189,16 @@ enum TableRows {EVEN, ODD, ALL}
189189
*/
190190
String VALUE_STYLE_ALL = "valueStyleAll";
191191

192+
/**
193+
* Value: Boolean<br>
194+
* Applies: TABLE<br>
195+
* List the collection of simple values in multiple columns
196+
* DEFAULT: list values in one column
197+
*/
198+
String MULTI_COLUMNS = "multiColumns";
199+
192200
List<String> BOOLEAN_KEYS = Arrays.asList(ALL, ONE_ROW_TABLE, ROWNUM, SHORT_NAMES, SKIP_DEFAULT_OPTIONS
193-
, STRUCT_ON_TABLE, TO_STRING, VALUE_STYLE_ALL);
201+
, STRUCT_ON_TABLE, TO_STRING, VALUE_STYLE_ALL, MULTI_COLUMNS);
194202

195203
default void println(Object object) {
196204
println(new HashMap<>(), object);

console/src/main/java/org/jline/console/impl/DefaultPrinter.java

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002-2021, the original author or authors.
2+
* Copyright (c) 2002-2022, the original author or authors.
33
*
44
* This software is distributable under the BSD license. See the terms of the
55
* BSD license in the documentation provided with this software.
@@ -110,6 +110,7 @@ public String[] appendUsage(String[] customUsage) {
110110
" --maxColumnWidth=WIDTH Maximum column width",
111111
" -d --maxDepth=DEPTH Maximum depth objects are resolved",
112112
" -n --maxrows=ROWS Maximum number of lines to display",
113+
" -m --multiColumns Display the collection of simple data in multiple columns",
113114
" --oneRowTable Display one row data on table",
114115
" -h --rowHighlight=ROW Highlight table rows. ROW = EVEN, ODD, ALL",
115116
" -r --rownum Display table row numbers",
@@ -199,6 +200,9 @@ public Map<String, Object> compileOptions(Options opt) {
199200
throw exception;
200201
}
201202
}
203+
if (opt.isSet(Printer.MULTI_COLUMNS)) {
204+
options.put(Printer.MULTI_COLUMNS, true);
205+
}
202206
options.put("exception", "stack");
203207
return options;
204208
}
@@ -990,26 +994,58 @@ private void highlightList(Map<String, Object> options
990994
int maxrows = (int)options.get(Printer.MAXROWS);
991995
int indent = (int)options.get(Printer.INDENTION);
992996
List<Integer> tabs = new ArrayList<>();
993-
tabs.add(indent*depth);
994-
if (options.containsKey(Printer.ROWNUM)) {
995-
tabs.add(indent*depth + digits(collection.size()) + 2);
996-
}
997-
options.remove(Printer.MAX_COLUMN_WIDTH);
998-
SyntaxHighlighter highlighter = depth == 0 ? (SyntaxHighlighter)options.get(Printer.STYLE) : null;
999-
for (Object o : collection) {
1000-
AttributedStringBuilder asb = new AttributedStringBuilder().tabs(tabs);
1001-
if (depth > 0) {
1002-
asb.append("\t");
1003-
}
997+
SyntaxHighlighter highlighter = depth == 0 ? (SyntaxHighlighter) options.get(Printer.STYLE) : null;
998+
if (!(boolean)options.getOrDefault(Printer.MULTI_COLUMNS, false)) {
999+
tabs.add(indent*depth);
10041000
if (options.containsKey(Printer.ROWNUM)) {
1005-
asb.styled(prntStyle.resolve(".rn"), Integer.toString(row)).append(":");
1006-
asb.append("\t");
1007-
row++;
1001+
tabs.add(indent*depth + digits(collection.size()) + 2);
10081002
}
1009-
if (highlighter != null && o instanceof String) {
1010-
asb.append(highlighter.highlight((String)o));
1011-
} else {
1012-
asb.append(highlightValue(options, null, o));
1003+
options.remove(Printer.MAX_COLUMN_WIDTH);
1004+
for (Object o : collection) {
1005+
AttributedStringBuilder asb = new AttributedStringBuilder().tabs(tabs);
1006+
if (depth > 0) {
1007+
asb.append("\t");
1008+
}
1009+
if (options.containsKey(Printer.ROWNUM)) {
1010+
asb.styled(prntStyle.resolve(".rn"), Integer.toString(row)).append(":");
1011+
asb.append("\t");
1012+
row++;
1013+
}
1014+
if (highlighter != null && o instanceof String) {
1015+
asb.append(highlighter.highlight((String) o));
1016+
} else {
1017+
asb.append(highlightValue(options, null, o));
1018+
}
1019+
println(asb.columnSubSequence(0, width), maxrows);
1020+
}
1021+
} else {
1022+
int maxWidth = 0;
1023+
for (Object o : collection) {
1024+
AttributedString as;
1025+
if (highlighter != null && o instanceof String) {
1026+
as = highlighter.highlight((String) o);
1027+
} else {
1028+
as = highlightValue(options, null, o);
1029+
}
1030+
if (as.length() > maxWidth) {
1031+
maxWidth = as.length();
1032+
}
1033+
}
1034+
int mcw = (int)options.getOrDefault(Printer.MAX_COLUMN_WIDTH, Integer.MAX_VALUE);
1035+
maxWidth = mcw < maxWidth ? mcw : maxWidth;
1036+
tabs.add(maxWidth + 1);
1037+
AttributedStringBuilder asb = new AttributedStringBuilder().tabs(tabs);
1038+
for (Object o : collection) {
1039+
if (asb.length() + maxWidth > width) {
1040+
println(asb.columnSubSequence(0, width), maxrows);
1041+
asb = new AttributedStringBuilder().tabs(tabs);
1042+
}
1043+
if (highlighter != null && o instanceof String) {
1044+
asb.append(highlighter.highlight((String) o));
1045+
} else {
1046+
asb.append(highlightValue(options, null, o));
1047+
}
1048+
asb.append("\t");
10131049
}
10141050
println(asb.columnSubSequence(0, width), maxrows);
10151051
}

0 commit comments

Comments
 (0)