java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 13 Sep 2015 19:25:36 +0200
branchv_0
changeset 227 0094319a274a
parent 224 36db9fd27436
child 234 305871254838
permissions -rw-r--r--
headerTypes: new option to hide column types in tabular headers
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@218
    22
import info.globalcode.sql.dk.Functions;
franta-hg@39
    23
import static info.globalcode.sql.dk.Functions.lpad;
franta-hg@39
    24
import static info.globalcode.sql.dk.Functions.rpad;
franta-hg@40
    25
import static info.globalcode.sql.dk.Functions.repeat;
franta-hg@206
    26
import info.globalcode.sql.dk.configuration.PropertyDeclaration;
franta-hg@206
    27
import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL;
franta-hg@206
    28
import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL_DESCRIPTION;
franta-hg@88
    29
import java.util.List;
franta-hg@34
    30
franta-hg@32
    31
/**
franta-hg@185
    32
 * <p>
franta-hg@185
    33
 * Prints human-readable output – tables of result sets and text messages with update counts.
franta-hg@185
    34
 * </p>
franta-hg@155
    35
 *
franta-hg@185
    36
 * <p>
franta-hg@185
    37
 * Longer values might break the table – overflow the cells – see alternative tabular formatters
franta-hg@185
    38
 * and the {@linkplain #PROPERTY_TRIM} property.
franta-hg@185
    39
 * </p>
franta-hg@32
    40
 *
franta-hg@32
    41
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@155
    42
 * @see TabularPrefetchingFormatter
franta-hg@155
    43
 * @see TabularWrappingFormatter
franta-hg@32
    44
 */
franta-hg@207
    45
@PropertyDeclaration(name = COLORFUL, defaultValue = "true", type = Boolean.class, description = COLORFUL_DESCRIPTION)
franta-hg@207
    46
@PropertyDeclaration(name = TabularFormatter.PROPERTY_ASCII, defaultValue = "false", type = Boolean.class, description = "whether to use ASCII table borders instead of unicode ones")
franta-hg@207
    47
@PropertyDeclaration(name = TabularFormatter.PROPERTY_TRIM, defaultValue = "false", type = Boolean.class, description = "whether to trim the values to fit the column width")
franta-hg@227
    48
@PropertyDeclaration(name = TabularFormatter.PROPERTY_HEADER_TYPE, defaultValue = "true", type = Boolean.class, description = "whether to print data types in column headers")
franta-hg@32
    49
public class TabularFormatter extends AbstractFormatter {
franta-hg@32
    50
franta-hg@79
    51
	public static final String NAME = "tabular"; // bash-completion:formatter
franta-hg@39
    52
	private static final String HEADER_TYPE_PREFIX = " (";
franta-hg@39
    53
	private static final String HEADER_TYPE_SUFFIX = ")";
franta-hg@104
    54
	public static final String PROPERTY_ASCII = "ascii";
franta-hg@104
    55
	public static final String PROPERTY_TRIM = "trim";
franta-hg@227
    56
	public static final String PROPERTY_HEADER_TYPE = "headerTypes";
franta-hg@123
    57
	protected ColorfulPrintWriter out;
franta-hg@37
    58
	private boolean firstResult = true;
franta-hg@39
    59
	private int[] columnWidth;
franta-hg@87
    60
	/**
franta-hg@87
    61
	 * use ASCII borders instead of unicode ones
franta-hg@87
    62
	 */
franta-hg@104
    63
	private final boolean asciiNostalgia;
franta-hg@87
    64
	/**
franta-hg@87
    65
	 * Trim values if they are longer than cell size
franta-hg@87
    66
	 */
franta-hg@104
    67
	private final boolean trimValues;
franta-hg@227
    68
	/**
franta-hg@227
    69
	 * Print data type of each column in the header
franta-hg@227
    70
	 */
franta-hg@227
    71
	private final boolean printHeaderTypes;
franta-hg@32
    72
franta-hg@32
    73
	public TabularFormatter(FormatterContext formatterContext) {
franta-hg@32
    74
		super(formatterContext);
franta-hg@34
    75
		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
franta-hg@104
    76
		asciiNostalgia = formatterContext.getProperties().getBoolean(PROPERTY_ASCII, false);
franta-hg@104
    77
		trimValues = formatterContext.getProperties().getBoolean(PROPERTY_TRIM, false);
franta-hg@227
    78
		printHeaderTypes = formatterContext.getProperties().getBoolean(PROPERTY_HEADER_TYPE, true);
franta-hg@206
    79
		out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, true));
franta-hg@34
    80
	}
franta-hg@34
    81
franta-hg@34
    82
	@Override
franta-hg@142
    83
	public void writeStartResultSet(ColumnsHeader header) {
franta-hg@142
    84
		super.writeStartResultSet(header);
franta-hg@37
    85
		printResultSeparator();
franta-hg@37
    86
franta-hg@88
    87
		initColumnWidths(header.getColumnCount());
franta-hg@39
    88
franta-hg@40
    89
		printTableIndent();
franta-hg@40
    90
		printTableBorder("╭");
franta-hg@88
    91
franta-hg@88
    92
		List<ColumnDescriptor> columnDescriptors = header.getColumnDescriptors();
franta-hg@88
    93
franta-hg@88
    94
		for (ColumnDescriptor cd : columnDescriptors) {
franta-hg@88
    95
			// padding: make header cell at least same width as data cells in this column
franta-hg@227
    96
			int typeWidth = printHeaderTypes ? cd.getTypeName().length() + HEADER_TYPE_PREFIX.length() + HEADER_TYPE_SUFFIX.length() : 0;
franta-hg@88
    97
			cd.setLabel(rpad(cd.getLabel(), getColumnWidth(cd.getColumnNumber()) - typeWidth));
franta-hg@88
    98
			updateColumnWidth(cd.getColumnNumber(), cd.getLabel().length() + typeWidth);
franta-hg@88
    99
franta-hg@40
   100
			if (!cd.isFirstColumn()) {
franta-hg@40
   101
				printTableBorder("┬");
franta-hg@40
   102
			}
franta-hg@40
   103
			printTableBorder(repeat('─', getColumnWidth(cd.getColumnNumber()) + 2));
franta-hg@40
   104
		}
franta-hg@40
   105
		printTableBorder("╮");
franta-hg@40
   106
		out.println();
franta-hg@40
   107
franta-hg@88
   108
		for (ColumnDescriptor cd : columnDescriptors) {
franta-hg@40
   109
			if (cd.isFirstColumn()) {
franta-hg@40
   110
				printTableIndent();
franta-hg@40
   111
				printTableBorder("│ ");
franta-hg@40
   112
			} else {
franta-hg@40
   113
				printTableBorder(" │ ");
franta-hg@40
   114
			}
franta-hg@37
   115
			out.print(TerminalStyle.Bright, cd.getLabel());
franta-hg@227
   116
			if (printHeaderTypes) {
franta-hg@227
   117
				out.print(HEADER_TYPE_PREFIX);
franta-hg@227
   118
				out.print(cd.getTypeName());
franta-hg@227
   119
				out.print(HEADER_TYPE_SUFFIX);
franta-hg@227
   120
			}
franta-hg@40
   121
			if (cd.isLastColumn()) {
franta-hg@40
   122
				printTableBorder(" │");
franta-hg@37
   123
			}
franta-hg@37
   124
		}
franta-hg@37
   125
		out.println();
franta-hg@40
   126
franta-hg@40
   127
		printTableIndent();
franta-hg@40
   128
		printTableBorder("├");
franta-hg@40
   129
		for (int i = 1; i <= header.getColumnCount(); i++) {
franta-hg@40
   130
			if (i > 1) {
franta-hg@40
   131
				printTableBorder("┼");
franta-hg@40
   132
			}
franta-hg@40
   133
			printTableBorder(repeat('─', getColumnWidth(i) + 2));
franta-hg@40
   134
		}
franta-hg@40
   135
		printTableBorder("┤");
franta-hg@40
   136
		out.println();
franta-hg@40
   137
franta-hg@37
   138
		out.flush();
franta-hg@37
   139
	}
franta-hg@37
   140
franta-hg@88
   141
	/**
franta-hg@88
   142
	 * Must be called before
franta-hg@88
   143
	 * {@linkplain #updateColumnWidth(int, int)}
franta-hg@88
   144
	 * and {@linkplain #getColumnWidth(int)}
franta-hg@88
   145
	 * for each result set.
franta-hg@88
   146
	 *
franta-hg@88
   147
	 * @param columnCount number of columns in current result set
franta-hg@88
   148
	 */
franta-hg@88
   149
	protected void initColumnWidths(int columnCount) {
franta-hg@88
   150
		if (columnWidth == null) {
franta-hg@88
   151
			columnWidth = new int[columnCount];
franta-hg@88
   152
		}
franta-hg@88
   153
	}
franta-hg@88
   154
franta-hg@88
   155
	protected void cleanColumnWidths() {
franta-hg@88
   156
		columnWidth = null;
franta-hg@88
   157
	}
franta-hg@88
   158
franta-hg@37
   159
	@Override
franta-hg@34
   160
	public void writeColumnValue(Object value) {
franta-hg@34
   161
		super.writeColumnValue(value);
franta-hg@123
   162
		writeColumnValueInternal(value);
franta-hg@123
   163
	}
franta-hg@123
   164
franta-hg@123
   165
	protected void writeColumnValueInternal(Object value) {
franta-hg@34
   166
franta-hg@40
   167
		if (isCurrentColumnFirst()) {
franta-hg@40
   168
			printTableIndent();
franta-hg@40
   169
			printTableBorder("│ ");
franta-hg@40
   170
		} else {
franta-hg@90
   171
			printTableBorder(" │ ");
franta-hg@34
   172
		}
franta-hg@37
   173
franta-hg@219
   174
		printValueWithWhitespaceReplaced(toString(value));
franta-hg@40
   175
franta-hg@40
   176
		if (isCurrentColumnLast()) {
franta-hg@90
   177
			printTableBorder(" │");
franta-hg@40
   178
		}
franta-hg@40
   179
franta-hg@39
   180
	}
franta-hg@39
   181
franta-hg@219
   182
	protected void printValueWithWhitespaceReplaced(String text) {
franta-hg@219
   183
		Functions.printValueWithWhitespaceReplaced(out, text, TerminalColor.Cyan, TerminalColor.Red);
franta-hg@219
   184
	}
franta-hg@219
   185
franta-hg@123
   186
	protected int getColumnWidth(int columnNumber) {
franta-hg@39
   187
		return columnWidth[columnNumber - 1];
franta-hg@39
   188
	}
franta-hg@39
   189
franta-hg@39
   190
	private void setColumnWidth(int columnNumber, int width) {
franta-hg@39
   191
		columnWidth[columnNumber - 1] = width;
franta-hg@39
   192
	}
franta-hg@39
   193
franta-hg@88
   194
	protected void updateColumnWidth(int columnNumber, int width) {
franta-hg@39
   195
		int oldWidth = getColumnWidth(columnNumber);
franta-hg@39
   196
		setColumnWidth(columnNumber, Math.max(width, oldWidth));
franta-hg@39
   197
franta-hg@39
   198
	}
franta-hg@39
   199
franta-hg@39
   200
	protected String toString(Object value) {
franta-hg@39
   201
		final int width = getColumnWidth(getCurrentColumnsCount());
franta-hg@87
   202
		String result;
franta-hg@43
   203
		if (value instanceof Number || value instanceof Boolean) {
franta-hg@98
   204
			result = lpad(String.valueOf(value), width);
franta-hg@39
   205
		} else {
franta-hg@98
   206
			result = rpad(String.valueOf(value), width);
franta-hg@39
   207
		}
franta-hg@40
   208
		// ?	value = (boolean) value ? "✔" : "✗";
franta-hg@87
   209
franta-hg@87
   210
		if (trimValues && result.length() > width) {
franta-hg@87
   211
			result = result.substring(0, width - 1) + "…";
franta-hg@87
   212
		}
franta-hg@87
   213
franta-hg@87
   214
		return result;
franta-hg@34
   215
	}
franta-hg@34
   216
franta-hg@34
   217
	@Override
franta-hg@34
   218
	public void writeEndRow() {
franta-hg@34
   219
		super.writeEndRow();
franta-hg@123
   220
		writeEndRowInternal();
franta-hg@123
   221
	}
franta-hg@123
   222
franta-hg@123
   223
	public void writeEndRowInternal() {
franta-hg@34
   224
		out.println();
franta-hg@34
   225
		out.flush();
franta-hg@32
   226
	}
franta-hg@37
   227
franta-hg@37
   228
	@Override
franta-hg@37
   229
	public void writeEndResultSet() {
franta-hg@40
   230
		int columnCount = getCurrentColumnsHeader().getColumnCount();
franta-hg@37
   231
		super.writeEndResultSet();
franta-hg@40
   232
franta-hg@40
   233
		printTableIndent();
franta-hg@40
   234
		printTableBorder("╰");
franta-hg@40
   235
		for (int i = 1; i <= columnCount; i++) {
franta-hg@40
   236
			if (i > 1) {
franta-hg@40
   237
				printTableBorder("┴");
franta-hg@40
   238
			}
franta-hg@40
   239
			printTableBorder(repeat('─', getColumnWidth(i) + 2));
franta-hg@40
   240
		}
franta-hg@40
   241
		printTableBorder("╯");
franta-hg@40
   242
		out.println();
franta-hg@40
   243
franta-hg@88
   244
		cleanColumnWidths();
franta-hg@40
   245
franta-hg@37
   246
		out.print(TerminalColor.Yellow, "Record count: ");
franta-hg@37
   247
		out.println(getCurrentRowCount());
franta-hg@40
   248
		out.bell();
franta-hg@37
   249
		out.flush();
franta-hg@37
   250
	}
franta-hg@37
   251
franta-hg@37
   252
	@Override
franta-hg@142
   253
	public void writeUpdatesResult(int updatedRowsCount) {
franta-hg@142
   254
		super.writeUpdatesResult(updatedRowsCount);
franta-hg@37
   255
		printResultSeparator();
franta-hg@37
   256
		out.print(TerminalColor.Red, "Updated records: ");
franta-hg@37
   257
		out.println(updatedRowsCount);
franta-hg@40
   258
		out.bell();
franta-hg@37
   259
		out.flush();
franta-hg@37
   260
	}
franta-hg@37
   261
franta-hg@37
   262
	@Override
franta-hg@37
   263
	public void writeEndDatabase() {
franta-hg@37
   264
		super.writeEndDatabase();
franta-hg@37
   265
		out.flush();
franta-hg@37
   266
	}
franta-hg@37
   267
franta-hg@37
   268
	private void printResultSeparator() {
franta-hg@37
   269
		if (firstResult) {
franta-hg@37
   270
			firstResult = false;
franta-hg@37
   271
		} else {
franta-hg@37
   272
			out.println();
franta-hg@37
   273
		}
franta-hg@37
   274
	}
franta-hg@40
   275
franta-hg@123
   276
	protected void printTableBorder(String border) {
franta-hg@76
   277
		if (asciiNostalgia) {
franta-hg@76
   278
			border = border.replaceAll("─", "-");
franta-hg@76
   279
			border = border.replaceAll("│", "|");
franta-hg@76
   280
			border = border.replaceAll("[╭┬╮├┼┤╰┴╯]", "+");
franta-hg@76
   281
		}
franta-hg@87
   282
franta-hg@40
   283
		out.print(TerminalColor.Green, border);
franta-hg@40
   284
	}
franta-hg@40
   285
franta-hg@123
   286
	protected void printTableIndent() {
franta-hg@40
   287
		out.print(" ");
franta-hg@40
   288
	}
franta-hg@224
   289
franta-hg@224
   290
	/**
franta-hg@224
   291
	 * @return whether should print only ASCII characters instead of unlimited Unicode.
franta-hg@224
   292
	 */
franta-hg@224
   293
	protected boolean isAsciiNostalgia() {
franta-hg@224
   294
		return asciiNostalgia;
franta-hg@224
   295
	}
franta-hg@32
   296
}