17 */ |
17 */ |
18 package info.globalcode.sql.dk.formatting; |
18 package info.globalcode.sql.dk.formatting; |
19 |
19 |
20 import info.globalcode.sql.dk.ColorfulPrintWriter; |
20 import info.globalcode.sql.dk.ColorfulPrintWriter; |
21 import static info.globalcode.sql.dk.ColorfulPrintWriter.*; |
21 import static info.globalcode.sql.dk.ColorfulPrintWriter.*; |
|
22 import info.globalcode.sql.dk.Functions; |
22 import static info.globalcode.sql.dk.Functions.lpad; |
23 import static info.globalcode.sql.dk.Functions.lpad; |
23 import static info.globalcode.sql.dk.Functions.rpad; |
24 import static info.globalcode.sql.dk.Functions.rpad; |
24 import static info.globalcode.sql.dk.Functions.repeat; |
25 import static info.globalcode.sql.dk.Functions.repeat; |
25 import info.globalcode.sql.dk.configuration.PropertyDeclaration; |
26 import info.globalcode.sql.dk.configuration.PropertyDeclaration; |
26 import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL; |
27 import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL; |
27 import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL_DESCRIPTION; |
28 import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL_DESCRIPTION; |
28 import java.util.List; |
29 import java.util.List; |
29 import java.util.regex.Matcher; |
|
30 import java.util.regex.Pattern; |
|
31 |
30 |
32 /** |
31 /** |
33 * <p> |
32 * <p> |
34 * Prints human-readable output – tables of result sets and text messages with update counts. |
33 * Prints human-readable output – tables of result sets and text messages with update counts. |
35 * </p> |
34 * </p> |
51 public static final String NAME = "tabular"; // bash-completion:formatter |
50 public static final String NAME = "tabular"; // bash-completion:formatter |
52 private static final String HEADER_TYPE_PREFIX = " ("; |
51 private static final String HEADER_TYPE_PREFIX = " ("; |
53 private static final String HEADER_TYPE_SUFFIX = ")"; |
52 private static final String HEADER_TYPE_SUFFIX = ")"; |
54 public static final String PROPERTY_ASCII = "ascii"; |
53 public static final String PROPERTY_ASCII = "ascii"; |
55 public static final String PROPERTY_TRIM = "trim"; |
54 public static final String PROPERTY_TRIM = "trim"; |
56 private static final String NBSP = " "; |
|
57 private static final Pattern whitespaceToReplace = Pattern.compile("\\n|\\r|\\t|" + NBSP); |
|
58 protected ColorfulPrintWriter out; |
55 protected ColorfulPrintWriter out; |
59 private boolean firstResult = true; |
56 private boolean firstResult = true; |
60 private int[] columnWidth; |
57 private int[] columnWidth; |
61 /** |
58 /** |
62 * use ASCII borders instead of unicode ones |
59 * use ASCII borders instead of unicode ones |
163 printTableBorder("│ "); |
160 printTableBorder("│ "); |
164 } else { |
161 } else { |
165 printTableBorder(" │ "); |
162 printTableBorder(" │ "); |
166 } |
163 } |
167 |
164 |
168 String valueString = toString(value); |
165 Functions.printValueWithWhitespaceReplaced(out, toString(value), TerminalColor.Cyan, TerminalColor.Red); |
169 printValueWithWhitespaceReplaced(valueString); |
|
170 |
166 |
171 if (isCurrentColumnLast()) { |
167 if (isCurrentColumnLast()) { |
172 printTableBorder(" │"); |
168 printTableBorder(" │"); |
173 } |
169 } |
174 |
170 |
275 } |
271 } |
276 |
272 |
277 protected void printTableIndent() { |
273 protected void printTableIndent() { |
278 out.print(" "); |
274 out.print(" "); |
279 } |
275 } |
280 |
|
281 protected void printValueWithWhitespaceReplaced(String valueString) { |
|
282 |
|
283 Matcher m = whitespaceToReplace.matcher(valueString); |
|
284 |
|
285 int start = 0; |
|
286 while (m.find(start)) { |
|
287 |
|
288 out.print(TerminalColor.Cyan, valueString.substring(start, m.start())); |
|
289 |
|
290 switch (m.group()) { |
|
291 case "\n": |
|
292 out.print(TerminalColor.Red, "↲"); |
|
293 break; |
|
294 case "\r": |
|
295 out.print(TerminalColor.Red, "⏎"); |
|
296 break; |
|
297 case "\t": |
|
298 out.print(TerminalColor.Red, "↹"); |
|
299 break; |
|
300 case NBSP: |
|
301 out.print(TerminalColor.Red, "⎵"); |
|
302 break; |
|
303 default: |
|
304 throw new IllegalStateException("Unexpected whitespace token: „" + m.group() + "“"); |
|
305 } |
|
306 |
|
307 start = m.end(); |
|
308 } |
|
309 |
|
310 out.print(TerminalColor.Cyan, valueString.substring(start, valueString.length())); |
|
311 } |
|
312 } |
276 } |