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