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