|
1 | 1 | /* |
2 | | - * Copyright (c) 2002-2021, the original author or authors. |
| 2 | + * Copyright (c) 2002-2022, the original author or authors. |
3 | 3 | * |
4 | 4 | * This software is distributable under the BSD license. See the terms of the |
5 | 5 | * BSD license in the documentation provided with this software. |
@@ -110,6 +110,7 @@ public String[] appendUsage(String[] customUsage) { |
110 | 110 | " --maxColumnWidth=WIDTH Maximum column width", |
111 | 111 | " -d --maxDepth=DEPTH Maximum depth objects are resolved", |
112 | 112 | " -n --maxrows=ROWS Maximum number of lines to display", |
| 113 | + " -m --multiColumns Display the collection of simple data in multiple columns", |
113 | 114 | " --oneRowTable Display one row data on table", |
114 | 115 | " -h --rowHighlight=ROW Highlight table rows. ROW = EVEN, ODD, ALL", |
115 | 116 | " -r --rownum Display table row numbers", |
@@ -199,6 +200,9 @@ public Map<String, Object> compileOptions(Options opt) { |
199 | 200 | throw exception; |
200 | 201 | } |
201 | 202 | } |
| 203 | + if (opt.isSet(Printer.MULTI_COLUMNS)) { |
| 204 | + options.put(Printer.MULTI_COLUMNS, true); |
| 205 | + } |
202 | 206 | options.put("exception", "stack"); |
203 | 207 | return options; |
204 | 208 | } |
@@ -990,26 +994,58 @@ private void highlightList(Map<String, Object> options |
990 | 994 | int maxrows = (int)options.get(Printer.MAXROWS); |
991 | 995 | int indent = (int)options.get(Printer.INDENTION); |
992 | 996 | 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); |
1004 | 1000 | 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); |
1008 | 1002 | } |
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"); |
1013 | 1049 | } |
1014 | 1050 | println(asb.columnSubSequence(0, width), maxrows); |
1015 | 1051 | } |
|
0 commit comments