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