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