franta-hg@32
|
1 |
/**
|
franta-hg@32
|
2 |
* SQL-DK
|
franta-hg@32
|
3 |
* Copyright © 2013 František Kučera (frantovo.cz)
|
franta-hg@32
|
4 |
*
|
franta-hg@32
|
5 |
* This program is free software: you can redistribute it and/or modify
|
franta-hg@32
|
6 |
* it under the terms of the GNU General Public License as published by
|
franta-hg@32
|
7 |
* the Free Software Foundation, either version 3 of the License, or
|
franta-hg@32
|
8 |
* (at your option) any later version.
|
franta-hg@32
|
9 |
*
|
franta-hg@32
|
10 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@32
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@32
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@32
|
13 |
* GNU General Public License for more details.
|
franta-hg@32
|
14 |
*
|
franta-hg@32
|
15 |
* You should have received a copy of the GNU General Public License
|
franta-hg@32
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@32
|
17 |
*/
|
franta-hg@32
|
18 |
package info.globalcode.sql.dk.formatting;
|
franta-hg@32
|
19 |
|
franta-hg@34
|
20 |
import info.globalcode.sql.dk.ColorfulPrintWriter;
|
franta-hg@37
|
21 |
import static info.globalcode.sql.dk.ColorfulPrintWriter.*;
|
franta-hg@39
|
22 |
import static info.globalcode.sql.dk.Functions.lpad;
|
franta-hg@39
|
23 |
import static info.globalcode.sql.dk.Functions.rpad;
|
franta-hg@40
|
24 |
import static info.globalcode.sql.dk.Functions.repeat;
|
franta-hg@88
|
25 |
import java.util.List;
|
franta-hg@34
|
26 |
|
franta-hg@32
|
27 |
/**
|
franta-hg@155
|
28 |
* <p>Prints human-readable output – tables of result sets and text messages with update counts.</p>
|
franta-hg@155
|
29 |
*
|
franta-hg@155
|
30 |
* <p>Longer values might break the table – overflow the cells – see alternative tabular formatters
|
franta-hg@155
|
31 |
* and the {@linkplain #PROPERTY_TRIM} property.</p>
|
franta-hg@32
|
32 |
*
|
franta-hg@32
|
33 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@155
|
34 |
* @see TabularPrefetchingFormatter
|
franta-hg@155
|
35 |
* @see TabularWrappingFormatter
|
franta-hg@32
|
36 |
*/
|
franta-hg@32
|
37 |
public class TabularFormatter extends AbstractFormatter {
|
franta-hg@32
|
38 |
|
franta-hg@79
|
39 |
public static final String NAME = "tabular"; // bash-completion:formatter
|
franta-hg@39
|
40 |
private static final String HEADER_TYPE_PREFIX = " (";
|
franta-hg@39
|
41 |
private static final String HEADER_TYPE_SUFFIX = ")";
|
franta-hg@104
|
42 |
public static final String PROPERTY_ASCII = "ascii";
|
franta-hg@104
|
43 |
public static final String PROPERTY_COLORFUL = "color";
|
franta-hg@104
|
44 |
public static final String PROPERTY_TRIM = "trim";
|
franta-hg@123
|
45 |
protected ColorfulPrintWriter out;
|
franta-hg@37
|
46 |
private boolean firstResult = true;
|
franta-hg@39
|
47 |
private int[] columnWidth;
|
franta-hg@87
|
48 |
/**
|
franta-hg@87
|
49 |
* use ASCII borders instead of unicode ones
|
franta-hg@87
|
50 |
*/
|
franta-hg@104
|
51 |
private final boolean asciiNostalgia;
|
franta-hg@87
|
52 |
/**
|
franta-hg@87
|
53 |
* Trim values if they are longer than cell size
|
franta-hg@87
|
54 |
*/
|
franta-hg@104
|
55 |
private final boolean trimValues;
|
franta-hg@32
|
56 |
|
franta-hg@32
|
57 |
public TabularFormatter(FormatterContext formatterContext) {
|
franta-hg@32
|
58 |
super(formatterContext);
|
franta-hg@34
|
59 |
out = new ColorfulPrintWriter(formatterContext.getOutputStream());
|
franta-hg@104
|
60 |
asciiNostalgia = formatterContext.getProperties().getBoolean(PROPERTY_ASCII, false);
|
franta-hg@104
|
61 |
trimValues = formatterContext.getProperties().getBoolean(PROPERTY_TRIM, false);
|
franta-hg@104
|
62 |
out.setColorful(formatterContext.getProperties().getBoolean(PROPERTY_COLORFUL, true));
|
franta-hg@34
|
63 |
}
|
franta-hg@34
|
64 |
|
franta-hg@34
|
65 |
@Override
|
franta-hg@142
|
66 |
public void writeStartResultSet(ColumnsHeader header) {
|
franta-hg@142
|
67 |
super.writeStartResultSet(header);
|
franta-hg@37
|
68 |
printResultSeparator();
|
franta-hg@37
|
69 |
|
franta-hg@88
|
70 |
initColumnWidths(header.getColumnCount());
|
franta-hg@39
|
71 |
|
franta-hg@40
|
72 |
printTableIndent();
|
franta-hg@40
|
73 |
printTableBorder("╭");
|
franta-hg@88
|
74 |
|
franta-hg@88
|
75 |
List<ColumnDescriptor> columnDescriptors = header.getColumnDescriptors();
|
franta-hg@88
|
76 |
|
franta-hg@88
|
77 |
for (ColumnDescriptor cd : columnDescriptors) {
|
franta-hg@88
|
78 |
// padding: make header cell at least same width as data cells in this column
|
franta-hg@88
|
79 |
int typeWidth = cd.getTypeName().length() + HEADER_TYPE_PREFIX.length() + HEADER_TYPE_SUFFIX.length();
|
franta-hg@88
|
80 |
cd.setLabel(rpad(cd.getLabel(), getColumnWidth(cd.getColumnNumber()) - typeWidth));
|
franta-hg@88
|
81 |
updateColumnWidth(cd.getColumnNumber(), cd.getLabel().length() + typeWidth);
|
franta-hg@88
|
82 |
|
franta-hg@40
|
83 |
if (!cd.isFirstColumn()) {
|
franta-hg@40
|
84 |
printTableBorder("┬");
|
franta-hg@40
|
85 |
}
|
franta-hg@40
|
86 |
printTableBorder(repeat('─', getColumnWidth(cd.getColumnNumber()) + 2));
|
franta-hg@40
|
87 |
}
|
franta-hg@40
|
88 |
printTableBorder("╮");
|
franta-hg@40
|
89 |
out.println();
|
franta-hg@40
|
90 |
|
franta-hg@88
|
91 |
for (ColumnDescriptor cd : columnDescriptors) {
|
franta-hg@40
|
92 |
if (cd.isFirstColumn()) {
|
franta-hg@40
|
93 |
printTableIndent();
|
franta-hg@40
|
94 |
printTableBorder("│ ");
|
franta-hg@40
|
95 |
} else {
|
franta-hg@40
|
96 |
printTableBorder(" │ ");
|
franta-hg@40
|
97 |
}
|
franta-hg@37
|
98 |
out.print(TerminalStyle.Bright, cd.getLabel());
|
franta-hg@39
|
99 |
out.print(HEADER_TYPE_PREFIX);
|
franta-hg@37
|
100 |
out.print(cd.getTypeName());
|
franta-hg@39
|
101 |
out.print(HEADER_TYPE_SUFFIX);
|
franta-hg@40
|
102 |
if (cd.isLastColumn()) {
|
franta-hg@40
|
103 |
printTableBorder(" │");
|
franta-hg@37
|
104 |
}
|
franta-hg@37
|
105 |
}
|
franta-hg@37
|
106 |
out.println();
|
franta-hg@40
|
107 |
|
franta-hg@40
|
108 |
printTableIndent();
|
franta-hg@40
|
109 |
printTableBorder("├");
|
franta-hg@40
|
110 |
for (int i = 1; i <= header.getColumnCount(); i++) {
|
franta-hg@40
|
111 |
if (i > 1) {
|
franta-hg@40
|
112 |
printTableBorder("┼");
|
franta-hg@40
|
113 |
}
|
franta-hg@40
|
114 |
printTableBorder(repeat('─', getColumnWidth(i) + 2));
|
franta-hg@40
|
115 |
}
|
franta-hg@40
|
116 |
printTableBorder("┤");
|
franta-hg@40
|
117 |
out.println();
|
franta-hg@40
|
118 |
|
franta-hg@37
|
119 |
out.flush();
|
franta-hg@37
|
120 |
}
|
franta-hg@37
|
121 |
|
franta-hg@88
|
122 |
/**
|
franta-hg@88
|
123 |
* Must be called before
|
franta-hg@88
|
124 |
* {@linkplain #updateColumnWidth(int, int)}
|
franta-hg@88
|
125 |
* and {@linkplain #getColumnWidth(int)}
|
franta-hg@88
|
126 |
* for each result set.
|
franta-hg@88
|
127 |
*
|
franta-hg@88
|
128 |
* @param columnCount number of columns in current result set
|
franta-hg@88
|
129 |
*/
|
franta-hg@88
|
130 |
protected void initColumnWidths(int columnCount) {
|
franta-hg@88
|
131 |
if (columnWidth == null) {
|
franta-hg@88
|
132 |
columnWidth = new int[columnCount];
|
franta-hg@88
|
133 |
}
|
franta-hg@88
|
134 |
}
|
franta-hg@88
|
135 |
|
franta-hg@88
|
136 |
protected void cleanColumnWidths() {
|
franta-hg@88
|
137 |
columnWidth = null;
|
franta-hg@88
|
138 |
}
|
franta-hg@88
|
139 |
|
franta-hg@37
|
140 |
@Override
|
franta-hg@34
|
141 |
public void writeColumnValue(Object value) {
|
franta-hg@34
|
142 |
super.writeColumnValue(value);
|
franta-hg@123
|
143 |
writeColumnValueInternal(value);
|
franta-hg@123
|
144 |
}
|
franta-hg@123
|
145 |
|
franta-hg@123
|
146 |
protected void writeColumnValueInternal(Object value) {
|
franta-hg@34
|
147 |
|
franta-hg@40
|
148 |
if (isCurrentColumnFirst()) {
|
franta-hg@40
|
149 |
printTableIndent();
|
franta-hg@40
|
150 |
printTableBorder("│ ");
|
franta-hg@40
|
151 |
} else {
|
franta-hg@90
|
152 |
printTableBorder(" │ ");
|
franta-hg@34
|
153 |
}
|
franta-hg@37
|
154 |
|
franta-hg@123
|
155 |
String valueString = toString(value);
|
franta-hg@123
|
156 |
printValueWithNewLinesReplaced(valueString);
|
franta-hg@40
|
157 |
|
franta-hg@40
|
158 |
if (isCurrentColumnLast()) {
|
franta-hg@90
|
159 |
printTableBorder(" │");
|
franta-hg@40
|
160 |
}
|
franta-hg@40
|
161 |
|
franta-hg@39
|
162 |
}
|
franta-hg@39
|
163 |
|
franta-hg@123
|
164 |
protected int getColumnWidth(int columnNumber) {
|
franta-hg@39
|
165 |
return columnWidth[columnNumber - 1];
|
franta-hg@39
|
166 |
}
|
franta-hg@39
|
167 |
|
franta-hg@39
|
168 |
private void setColumnWidth(int columnNumber, int width) {
|
franta-hg@39
|
169 |
columnWidth[columnNumber - 1] = width;
|
franta-hg@39
|
170 |
}
|
franta-hg@39
|
171 |
|
franta-hg@88
|
172 |
protected void updateColumnWidth(int columnNumber, int width) {
|
franta-hg@39
|
173 |
int oldWidth = getColumnWidth(columnNumber);
|
franta-hg@39
|
174 |
setColumnWidth(columnNumber, Math.max(width, oldWidth));
|
franta-hg@39
|
175 |
|
franta-hg@39
|
176 |
}
|
franta-hg@39
|
177 |
|
franta-hg@39
|
178 |
protected String toString(Object value) {
|
franta-hg@39
|
179 |
final int width = getColumnWidth(getCurrentColumnsCount());
|
franta-hg@87
|
180 |
String result;
|
franta-hg@43
|
181 |
if (value instanceof Number || value instanceof Boolean) {
|
franta-hg@98
|
182 |
result = lpad(String.valueOf(value), width);
|
franta-hg@39
|
183 |
} else {
|
franta-hg@98
|
184 |
result = rpad(String.valueOf(value), width);
|
franta-hg@39
|
185 |
}
|
franta-hg@40
|
186 |
// ? value = (boolean) value ? "✔" : "✗";
|
franta-hg@87
|
187 |
|
franta-hg@87
|
188 |
if (trimValues && result.length() > width) {
|
franta-hg@87
|
189 |
result = result.substring(0, width - 1) + "…";
|
franta-hg@87
|
190 |
}
|
franta-hg@87
|
191 |
|
franta-hg@87
|
192 |
return result;
|
franta-hg@34
|
193 |
}
|
franta-hg@34
|
194 |
|
franta-hg@34
|
195 |
@Override
|
franta-hg@34
|
196 |
public void writeEndRow() {
|
franta-hg@34
|
197 |
super.writeEndRow();
|
franta-hg@123
|
198 |
writeEndRowInternal();
|
franta-hg@123
|
199 |
}
|
franta-hg@123
|
200 |
|
franta-hg@123
|
201 |
public void writeEndRowInternal() {
|
franta-hg@34
|
202 |
out.println();
|
franta-hg@34
|
203 |
out.flush();
|
franta-hg@32
|
204 |
}
|
franta-hg@37
|
205 |
|
franta-hg@37
|
206 |
@Override
|
franta-hg@37
|
207 |
public void writeEndResultSet() {
|
franta-hg@40
|
208 |
int columnCount = getCurrentColumnsHeader().getColumnCount();
|
franta-hg@37
|
209 |
super.writeEndResultSet();
|
franta-hg@40
|
210 |
|
franta-hg@40
|
211 |
printTableIndent();
|
franta-hg@40
|
212 |
printTableBorder("╰");
|
franta-hg@40
|
213 |
for (int i = 1; i <= columnCount; i++) {
|
franta-hg@40
|
214 |
if (i > 1) {
|
franta-hg@40
|
215 |
printTableBorder("┴");
|
franta-hg@40
|
216 |
}
|
franta-hg@40
|
217 |
printTableBorder(repeat('─', getColumnWidth(i) + 2));
|
franta-hg@40
|
218 |
}
|
franta-hg@40
|
219 |
printTableBorder("╯");
|
franta-hg@40
|
220 |
out.println();
|
franta-hg@40
|
221 |
|
franta-hg@88
|
222 |
cleanColumnWidths();
|
franta-hg@40
|
223 |
|
franta-hg@37
|
224 |
out.print(TerminalColor.Yellow, "Record count: ");
|
franta-hg@37
|
225 |
out.println(getCurrentRowCount());
|
franta-hg@40
|
226 |
out.bell();
|
franta-hg@37
|
227 |
out.flush();
|
franta-hg@37
|
228 |
}
|
franta-hg@37
|
229 |
|
franta-hg@37
|
230 |
@Override
|
franta-hg@142
|
231 |
public void writeUpdatesResult(int updatedRowsCount) {
|
franta-hg@142
|
232 |
super.writeUpdatesResult(updatedRowsCount);
|
franta-hg@37
|
233 |
printResultSeparator();
|
franta-hg@37
|
234 |
out.print(TerminalColor.Red, "Updated records: ");
|
franta-hg@37
|
235 |
out.println(updatedRowsCount);
|
franta-hg@40
|
236 |
out.bell();
|
franta-hg@37
|
237 |
out.flush();
|
franta-hg@37
|
238 |
}
|
franta-hg@37
|
239 |
|
franta-hg@37
|
240 |
@Override
|
franta-hg@37
|
241 |
public void writeEndDatabase() {
|
franta-hg@37
|
242 |
super.writeEndDatabase();
|
franta-hg@37
|
243 |
out.flush();
|
franta-hg@37
|
244 |
}
|
franta-hg@37
|
245 |
|
franta-hg@37
|
246 |
private void printResultSeparator() {
|
franta-hg@37
|
247 |
if (firstResult) {
|
franta-hg@37
|
248 |
firstResult = false;
|
franta-hg@37
|
249 |
} else {
|
franta-hg@37
|
250 |
out.println();
|
franta-hg@37
|
251 |
}
|
franta-hg@37
|
252 |
}
|
franta-hg@40
|
253 |
|
franta-hg@123
|
254 |
protected void printTableBorder(String border) {
|
franta-hg@76
|
255 |
if (asciiNostalgia) {
|
franta-hg@76
|
256 |
border = border.replaceAll("─", "-");
|
franta-hg@76
|
257 |
border = border.replaceAll("│", "|");
|
franta-hg@76
|
258 |
border = border.replaceAll("[╭┬╮├┼┤╰┴╯]", "+");
|
franta-hg@76
|
259 |
}
|
franta-hg@87
|
260 |
|
franta-hg@40
|
261 |
out.print(TerminalColor.Green, border);
|
franta-hg@40
|
262 |
}
|
franta-hg@40
|
263 |
|
franta-hg@123
|
264 |
protected void printTableIndent() {
|
franta-hg@40
|
265 |
out.print(" ");
|
franta-hg@40
|
266 |
}
|
franta-hg@123
|
267 |
|
franta-hg@123
|
268 |
protected void printValueWithNewLinesReplaced(String valueString) {
|
franta-hg@123
|
269 |
String[] valueParts = valueString.split("\n");
|
franta-hg@123
|
270 |
for (int i = 0; i < valueParts.length; i++) {
|
franta-hg@123
|
271 |
String valuePart = valueParts[i];
|
franta-hg@124
|
272 |
// TODO: replace also TABs
|
franta-hg@123
|
273 |
out.print(TerminalColor.Cyan, valuePart);
|
franta-hg@123
|
274 |
if (i < valueParts.length - 1) {
|
franta-hg@123
|
275 |
out.print(TerminalColor.Red, "↲");
|
franta-hg@123
|
276 |
}
|
franta-hg@123
|
277 |
}
|
franta-hg@123
|
278 |
}
|
franta-hg@32
|
279 |
}
|