java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/TabularFormatter.java
tabular formatter: new option 'separateBy' to print horizontal separator on each change of given column
3 * Copyright © 2013 František Kučera (frantovo.cz)
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, version 3 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package info.globalcode.sql.dk.formatting;
19 import info.globalcode.sql.dk.ColorfulPrintWriter;
20 import static info.globalcode.sql.dk.ColorfulPrintWriter.*;
21 import info.globalcode.sql.dk.Functions;
22 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.repeat;
25 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_DESCRIPTION;
28 import java.sql.SQLException;
29 import java.sql.SQLXML;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Objects;
33 import java.util.logging.Level;
34 import java.util.logging.Logger;
35 import java.util.regex.Pattern;
39 * Prints human-readable output – tables of result sets and text messages with update counts.
43 * Longer values might break the table – overflow the cells – see alternative tabular formatters and
44 * the {@linkplain #PROPERTY_TRIM} property.
47 * @author Ing. František Kučera (frantovo.cz)
48 * @see TabularPrefetchingFormatter
49 * @see TabularWrappingFormatter
51 @PropertyDeclaration(name = COLORFUL, defaultValue = "true", type = Boolean.class, description = COLORFUL_DESCRIPTION)
52 @PropertyDeclaration(name = TabularFormatter.PROPERTY_ASCII, defaultValue = "false", type = Boolean.class, description = "whether to use ASCII table borders instead of unicode ones")
53 @PropertyDeclaration(name = TabularFormatter.PROPERTY_TRIM, defaultValue = "false", type = Boolean.class, description = "whether to trim the values to fit the column width")
54 @PropertyDeclaration(name = TabularFormatter.PROPERTY_HEADER_TYPE, defaultValue = "true", type = Boolean.class, description = "whether to print data types in column headers")
55 @PropertyDeclaration(name = TabularFormatter.PROPERTY_SEPARATE_BY, defaultValue = "", type = String.class, description = "colum(s) whose change triggers separator printing")
56 public class TabularFormatter extends AbstractFormatter {
58 private static final Logger log = Logger.getLogger(TabularFormatter.class.getName());
59 public static final String NAME = "tabular"; // bash-completion:formatter
60 private static final String HEADER_TYPE_PREFIX = " (";
61 private static final String HEADER_TYPE_SUFFIX = ")";
62 public static final String PROPERTY_ASCII = "ascii";
63 public static final String PROPERTY_TRIM = "trim";
64 public static final String PROPERTY_HEADER_TYPE = "headerTypes";
65 public static final String PROPERTY_SEPARATE_BY = "separateBy";
66 protected ColorfulPrintWriter out;
67 private boolean firstResult = true;
68 private int[] columnWidth;
70 * use ASCII borders instead of unicode ones
72 private final boolean asciiNostalgia;
74 * Trim values if they are longer than cell size
76 private final boolean trimValues;
78 * Print data type of each column in the header
80 private final boolean printHeaderTypes;
82 * When values of columns (names specified by this pattern) changes
83 * between two consecutive rows, horizontal separator is printed.
85 private final Pattern separateBy;
87 * Indexes of columns that matches {@linkplain #separateBy}
89 private final List<Boolean> separators = new ArrayList<>();
91 * Internal counter for buffered data.
93 private int currentColumnsCount = 0;
95 * Buffered data to be compared and printed later.
97 private final List<Object> currentRow = new ArrayList<>();
99 * Buffered data to be compared later.
101 private final List<Object> previousRow = new ArrayList<>();
103 public TabularFormatter(FormatterContext formatterContext) {
104 super(formatterContext);
105 out = new ColorfulPrintWriter(formatterContext.getOutputStream());
106 asciiNostalgia = formatterContext.getProperties().getBoolean(PROPERTY_ASCII, false);
107 trimValues = formatterContext.getProperties().getBoolean(PROPERTY_TRIM, false);
108 printHeaderTypes = formatterContext.getProperties().getBoolean(PROPERTY_HEADER_TYPE, true);
109 out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, true));
110 separateBy = formatterContext.getProperties().getPattern(PROPERTY_SEPARATE_BY, null);
114 public void writeStartResultSet(ColumnsHeader header) {
115 super.writeStartResultSet(header);
116 printResultSeparator();
118 initColumnWidths(header.getColumnCount());
124 printTableBorder("╭");
126 List<ColumnDescriptor> columnDescriptors = header.getColumnDescriptors();
128 if (separateBy != null) {
129 for (ColumnDescriptor cd : columnDescriptors) {
130 separators.add(separateBy.matcher(cd.getLabel()).matches());
134 for (ColumnDescriptor cd : columnDescriptors) {
135 // padding: make header cell at least same width as data cells in this column
136 int typeWidth = printHeaderTypes ? cd.getTypeName().length() + HEADER_TYPE_PREFIX.length() + HEADER_TYPE_SUFFIX.length() : 0;
137 cd.setLabel(rpad(cd.getLabel(), getColumnWidth(cd.getColumnNumber()) - typeWidth));
138 updateColumnWidth(cd.getColumnNumber(), cd.getLabel().length() + typeWidth);
140 if (!cd.isFirstColumn()) {
141 printTableBorder("┬");
143 printTableBorder(repeat('─', getColumnWidth(cd.getColumnNumber()) + 2));
145 printTableBorder("╮");
148 for (ColumnDescriptor cd : columnDescriptors) {
149 if (cd.isFirstColumn()) {
151 printTableBorder("│ ");
153 printTableBorder(" │ ");
155 out.print(TerminalStyle.Bright, cd.getLabel());
156 if (printHeaderTypes) {
157 out.print(HEADER_TYPE_PREFIX);
158 out.print(cd.getTypeName());
159 out.print(HEADER_TYPE_SUFFIX);
161 if (cd.isLastColumn()) {
162 printTableBorder(" │");
168 printTableBorder("├");
169 for (int i = 1; i <= header.getColumnCount(); i++) {
171 printTableBorder("┼");
173 printTableBorder(repeat('─', getColumnWidth(i) + 2));
175 printTableBorder("┤");
182 * Must be called before {@linkplain #updateColumnWidth(int, int)} and
183 * {@linkplain #getColumnWidth(int)} for each result set.
185 * @param columnCount number of columns in current result set
187 protected void initColumnWidths(int columnCount) {
188 if (columnWidth == null) {
189 columnWidth = new int[columnCount];
193 protected void cleanColumnWidths() {
198 public void writeColumnValue(Object value) {
199 if (separateBy == null) super.writeColumnValue(value);
200 writeColumnValueInternal(value);
203 protected void writeColumnValueInternal(Object value) {
204 if (separateBy == null) {
205 printColumnValue(value);
207 currentRow.add(value);
208 currentColumnsCount++;
209 int columnsCount = getCurrentColumnsHeader().getColumnCount();
210 if (currentColumnsCount % columnsCount == 0) {
211 if (!previousRow.isEmpty()) {
212 boolean hasChanges = false;
213 for (int i = 0; i < previousRow.size(); i++) {
214 Object previous = previousRow.get(i);
215 Object current = currentRow.get(i);
216 boolean isSepating = separators.get(i);
217 if (isSepating && !Objects.equals(previous, current)) {
224 printRecordSeparator();
228 for (Object o : currentRow) {
229 super.writeColumnValue(value);
234 previousRow.addAll(currentRow);
236 currentColumnsCount = 0;
241 protected void printColumnValue(Object value) {
243 if (isCurrentColumnFirst()) {
245 printTableBorder("│ ");
247 printTableBorder(" │ ");
250 printValueWithWhitespaceReplaced(toString(value));
252 if (isCurrentColumnLast()) {
253 printTableBorder(" │");
258 protected void printRecordSeparator() {
259 int columnCount = getCurrentColumnsHeader().getColumnCount();
261 printTableBorder("├");
262 for (int i = 1; i <= columnCount; i++) {
264 printTableBorder("┼");
266 printTableBorder(repeat('─', getColumnWidth(i) + 2));
268 printTableBorder("┤");
272 protected void printValueWithWhitespaceReplaced(String text) {
273 Functions.printValueWithWhitespaceReplaced(out, text, TerminalColor.Cyan, TerminalColor.Red);
276 protected int getColumnWidth(int columnNumber) {
277 return columnWidth[columnNumber - 1];
280 private void setColumnWidth(int columnNumber, int width) {
281 columnWidth[columnNumber - 1] = width;
284 protected void updateColumnWidth(int columnNumber, int width) {
285 int oldWidth = getColumnWidth(columnNumber);
286 setColumnWidth(columnNumber, Math.max(width, oldWidth));
290 protected String toString(Object value) {
291 final int width = getColumnWidth(getCurrentColumnsCount());
293 if (value instanceof Number || value instanceof Boolean) {
294 result = lpad(String.valueOf(value), width);
296 if (value instanceof SQLXML) {
297 // TODO: move to a common method, share with other formatters
299 value = ((SQLXML) value).getString();
300 } catch (SQLException e) {
301 log.log(Level.SEVERE, "Unable to format XML", e);
305 result = rpad(String.valueOf(value), width);
307 // ? value = (boolean) value ? "✔" : "✗";
309 if (trimValues && result.length() > width) {
310 result = result.substring(0, width - 1) + "…";
317 public void writeStartRow() {
318 super.writeStartRow();
319 currentColumnsCount = 0;
323 public void writeEndRow() {
325 writeEndRowInternal();
328 public void writeEndRowInternal() {
334 public void writeEndResultSet() {
335 int columnCount = getCurrentColumnsHeader().getColumnCount();
336 super.writeEndResultSet();
339 printTableBorder("╰");
340 for (int i = 1; i <= columnCount; i++) {
342 printTableBorder("┴");
344 printTableBorder(repeat('─', getColumnWidth(i) + 2));
346 printTableBorder("╯");
351 out.print(TerminalColor.Yellow, "Record count: ");
352 out.println(getCurrentRowCount());
358 public void writeUpdatesResult(int updatedRowsCount) {
359 super.writeUpdatesResult(updatedRowsCount);
360 printResultSeparator();
361 out.print(TerminalColor.Red, "Updated records: ");
362 out.println(updatedRowsCount);
368 public void writeEndDatabase() {
369 super.writeEndDatabase();
373 private void printResultSeparator() {
381 protected void printTableBorder(String border) {
382 if (asciiNostalgia) {
383 border = border.replaceAll("─", "-");
384 border = border.replaceAll("│", "|");
385 border = border.replaceAll("[╭┬╮├┼┤╰┴╯]", "+");
388 out.print(TerminalColor.Green, border);
391 protected void printTableIndent() {
396 * @return whether should print only ASCII characters instead of unlimited Unicode.
398 protected boolean isAsciiNostalgia() {
399 return asciiNostalgia;