|
1 /** |
|
2 * SQL-DK |
|
3 * Copyright © 2014 František Kučera (frantovo.cz) |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation, either version 3 of the License, or |
|
8 * (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 */ |
|
18 package info.globalcode.sql.dk.formatting; |
|
19 |
|
20 import info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor; |
|
21 import java.util.ArrayList; |
|
22 import java.util.List; |
|
23 import static info.globalcode.sql.dk.Functions.lpad; |
|
24 import static info.globalcode.sql.dk.Functions.rpad; |
|
25 import static info.globalcode.sql.dk.Functions.repeat; |
|
26 |
|
27 /** |
|
28 * |
|
29 * @author Ing. František Kučera (frantovo.cz) |
|
30 */ |
|
31 public class TabularWrappingFormatter extends TabularFormatter { |
|
32 |
|
33 public static final String NAME = "tabular-wrapping"; // bash-completion:formatter |
|
34 private List<String[]> currentRow; |
|
35 |
|
36 public TabularWrappingFormatter(FormatterContext formatterContext) { |
|
37 super(formatterContext); |
|
38 } |
|
39 |
|
40 @Override |
|
41 public void writeColumnsHeader(ColumnsHeader header) { |
|
42 super.writeColumnsHeader(header); |
|
43 currentRow = new ArrayList<>(header.getColumnCount()); |
|
44 } |
|
45 |
|
46 @Override |
|
47 protected void writeColumnValueInternal(Object value) { |
|
48 boolean rightAlign = value instanceof Number || value instanceof Boolean; |
|
49 String valueString = String.valueOf(value); |
|
50 int columnWidth = getColumnWidth(getCurrentColumnsCount()) - 1; // -1 = space for new line symbol |
|
51 currentRow.add(split(valueString, columnWidth, rightAlign)); |
|
52 } |
|
53 |
|
54 @Override |
|
55 public void writeEndRow() { |
|
56 super.writeEndRow(); |
|
57 |
|
58 int wrappedLine = 0; |
|
59 boolean hasMoreWrappedLines; |
|
60 |
|
61 do { |
|
62 hasMoreWrappedLines = false; |
|
63 for (int i = 0; i < currentRow.size(); i++) { |
|
64 if (i == 0) { |
|
65 printTableIndent(); |
|
66 printTableBorder("│ "); |
|
67 } else { |
|
68 printTableBorder(" │ "); |
|
69 } |
|
70 String[] columnArray = currentRow.get(i); |
|
71 if (wrappedLine < columnArray.length) { |
|
72 printValueWithNewLinesReplaced(columnArray[wrappedLine]); |
|
73 |
|
74 if (wrappedLine < columnArray.length - 1) { |
|
75 out.print(TerminalColor.Red, "↩"); |
|
76 hasMoreWrappedLines = true; |
|
77 } else { |
|
78 out.print(" "); |
|
79 } |
|
80 |
|
81 } else { |
|
82 out.print(repeat(' ', getColumnWidth(i + 1))); |
|
83 } |
|
84 |
|
85 if (i == (currentRow.size() - 1)) { |
|
86 printTableBorder(" │"); |
|
87 } |
|
88 } |
|
89 out.println(); |
|
90 out.flush(); |
|
91 wrappedLine++; |
|
92 } while (hasMoreWrappedLines); |
|
93 |
|
94 currentRow.clear(); |
|
95 } |
|
96 |
|
97 @Override |
|
98 public void writeEndRowInternal() { |
|
99 // already done – wrapped row ends |
|
100 } |
|
101 |
|
102 private static String[] split(String s, int width, boolean rightAlign) { |
|
103 String[] array = new String[(s.length() - 1) / width + 1]; |
|
104 for (int i = 0; i < array.length; i++) { |
|
105 if (i == array.length - 1) { |
|
106 String part = s.substring(i * width, s.length()); |
|
107 array[i] = rightAlign ? lpad(part, width) : rpad(part, width); |
|
108 } else { |
|
109 array[i] = s.substring(i * width, (i + 1) * width); |
|
110 } |
|
111 } |
|
112 return array; |
|
113 } |
|
114 } |