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