43 * @see TabularWrappingFormatter |
43 * @see TabularWrappingFormatter |
44 */ |
44 */ |
45 @PropertyDeclaration(name = COLORFUL, defaultValue = "true", type = Boolean.class, description = COLORFUL_DESCRIPTION) |
45 @PropertyDeclaration(name = COLORFUL, defaultValue = "true", type = Boolean.class, description = COLORFUL_DESCRIPTION) |
46 @PropertyDeclaration(name = TabularFormatter.PROPERTY_ASCII, defaultValue = "false", type = Boolean.class, description = "whether to use ASCII table borders instead of unicode ones") |
46 @PropertyDeclaration(name = TabularFormatter.PROPERTY_ASCII, defaultValue = "false", type = Boolean.class, description = "whether to use ASCII table borders instead of unicode ones") |
47 @PropertyDeclaration(name = TabularFormatter.PROPERTY_TRIM, defaultValue = "false", type = Boolean.class, description = "whether to trim the values to fit the column width") |
47 @PropertyDeclaration(name = TabularFormatter.PROPERTY_TRIM, defaultValue = "false", type = Boolean.class, description = "whether to trim the values to fit the column width") |
|
48 @PropertyDeclaration(name = TabularFormatter.PROPERTY_HEADER_TYPE, defaultValue = "true", type = Boolean.class, description = "whether to print data types in column headers") |
48 public class TabularFormatter extends AbstractFormatter { |
49 public class TabularFormatter extends AbstractFormatter { |
49 |
50 |
50 public static final String NAME = "tabular"; // bash-completion:formatter |
51 public static final String NAME = "tabular"; // bash-completion:formatter |
51 private static final String HEADER_TYPE_PREFIX = " ("; |
52 private static final String HEADER_TYPE_PREFIX = " ("; |
52 private static final String HEADER_TYPE_SUFFIX = ")"; |
53 private static final String HEADER_TYPE_SUFFIX = ")"; |
53 public static final String PROPERTY_ASCII = "ascii"; |
54 public static final String PROPERTY_ASCII = "ascii"; |
54 public static final String PROPERTY_TRIM = "trim"; |
55 public static final String PROPERTY_TRIM = "trim"; |
|
56 public static final String PROPERTY_HEADER_TYPE = "headerTypes"; |
55 protected ColorfulPrintWriter out; |
57 protected ColorfulPrintWriter out; |
56 private boolean firstResult = true; |
58 private boolean firstResult = true; |
57 private int[] columnWidth; |
59 private int[] columnWidth; |
58 /** |
60 /** |
59 * use ASCII borders instead of unicode ones |
61 * use ASCII borders instead of unicode ones |
61 private final boolean asciiNostalgia; |
63 private final boolean asciiNostalgia; |
62 /** |
64 /** |
63 * Trim values if they are longer than cell size |
65 * Trim values if they are longer than cell size |
64 */ |
66 */ |
65 private final boolean trimValues; |
67 private final boolean trimValues; |
|
68 /** |
|
69 * Print data type of each column in the header |
|
70 */ |
|
71 private final boolean printHeaderTypes; |
66 |
72 |
67 public TabularFormatter(FormatterContext formatterContext) { |
73 public TabularFormatter(FormatterContext formatterContext) { |
68 super(formatterContext); |
74 super(formatterContext); |
69 out = new ColorfulPrintWriter(formatterContext.getOutputStream()); |
75 out = new ColorfulPrintWriter(formatterContext.getOutputStream()); |
70 asciiNostalgia = formatterContext.getProperties().getBoolean(PROPERTY_ASCII, false); |
76 asciiNostalgia = formatterContext.getProperties().getBoolean(PROPERTY_ASCII, false); |
71 trimValues = formatterContext.getProperties().getBoolean(PROPERTY_TRIM, false); |
77 trimValues = formatterContext.getProperties().getBoolean(PROPERTY_TRIM, false); |
|
78 printHeaderTypes = formatterContext.getProperties().getBoolean(PROPERTY_HEADER_TYPE, true); |
72 out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, true)); |
79 out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, true)); |
73 } |
80 } |
74 |
81 |
75 @Override |
82 @Override |
76 public void writeStartResultSet(ColumnsHeader header) { |
83 public void writeStartResultSet(ColumnsHeader header) { |
84 |
91 |
85 List<ColumnDescriptor> columnDescriptors = header.getColumnDescriptors(); |
92 List<ColumnDescriptor> columnDescriptors = header.getColumnDescriptors(); |
86 |
93 |
87 for (ColumnDescriptor cd : columnDescriptors) { |
94 for (ColumnDescriptor cd : columnDescriptors) { |
88 // padding: make header cell at least same width as data cells in this column |
95 // padding: make header cell at least same width as data cells in this column |
89 int typeWidth = cd.getTypeName().length() + HEADER_TYPE_PREFIX.length() + HEADER_TYPE_SUFFIX.length(); |
96 int typeWidth = printHeaderTypes ? cd.getTypeName().length() + HEADER_TYPE_PREFIX.length() + HEADER_TYPE_SUFFIX.length() : 0; |
90 cd.setLabel(rpad(cd.getLabel(), getColumnWidth(cd.getColumnNumber()) - typeWidth)); |
97 cd.setLabel(rpad(cd.getLabel(), getColumnWidth(cd.getColumnNumber()) - typeWidth)); |
91 updateColumnWidth(cd.getColumnNumber(), cd.getLabel().length() + typeWidth); |
98 updateColumnWidth(cd.getColumnNumber(), cd.getLabel().length() + typeWidth); |
92 |
99 |
93 if (!cd.isFirstColumn()) { |
100 if (!cd.isFirstColumn()) { |
94 printTableBorder("┬"); |
101 printTableBorder("┬"); |
104 printTableBorder("│ "); |
111 printTableBorder("│ "); |
105 } else { |
112 } else { |
106 printTableBorder(" │ "); |
113 printTableBorder(" │ "); |
107 } |
114 } |
108 out.print(TerminalStyle.Bright, cd.getLabel()); |
115 out.print(TerminalStyle.Bright, cd.getLabel()); |
109 out.print(HEADER_TYPE_PREFIX); |
116 if (printHeaderTypes) { |
110 out.print(cd.getTypeName()); |
117 out.print(HEADER_TYPE_PREFIX); |
111 out.print(HEADER_TYPE_SUFFIX); |
118 out.print(cd.getTypeName()); |
|
119 out.print(HEADER_TYPE_SUFFIX); |
|
120 } |
112 if (cd.isLastColumn()) { |
121 if (cd.isLastColumn()) { |
113 printTableBorder(" │"); |
122 printTableBorder(" │"); |
114 } |
123 } |
115 } |
124 } |
116 out.println(); |
125 out.println(); |