java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Fri, 10 Jan 2014 23:21:28 +0100
branchv_0
changeset 155 eb3676c6929b
parent 142 da1e38386d84
child 167 84aaa91642bf
permissions -rw-r--r--
more JavaDoc

/**
 * SQL-DK
 * Copyright © 2013 František Kučera (frantovo.cz)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package info.globalcode.sql.dk.formatting;

import info.globalcode.sql.dk.ColorfulPrintWriter;
import static info.globalcode.sql.dk.ColorfulPrintWriter.*;
import static info.globalcode.sql.dk.Functions.lpad;
import static info.globalcode.sql.dk.Functions.rpad;
import static info.globalcode.sql.dk.Functions.repeat;
import java.util.List;

/**
 * <p>Prints human-readable output – tables of result sets and text messages with update counts.</p>
 *
 * <p>Longer values might break the table – overflow the cells – see alternative tabular formatters
 * and the {@linkplain #PROPERTY_TRIM} property.</p>
 *
 * @author Ing. František Kučera (frantovo.cz)
 * @see TabularPrefetchingFormatter
 * @see TabularWrappingFormatter
 */
public class TabularFormatter extends AbstractFormatter {

	public static final String NAME = "tabular"; // bash-completion:formatter
	private static final String HEADER_TYPE_PREFIX = " (";
	private static final String HEADER_TYPE_SUFFIX = ")";
	public static final String PROPERTY_ASCII = "ascii";
	public static final String PROPERTY_COLORFUL = "color";
	public static final String PROPERTY_TRIM = "trim";
	protected ColorfulPrintWriter out;
	private boolean firstResult = true;
	private int[] columnWidth;
	/**
	 * use ASCII borders instead of unicode ones
	 */
	private final boolean asciiNostalgia;
	/**
	 * Trim values if they are longer than cell size
	 */
	private final boolean trimValues;

	public TabularFormatter(FormatterContext formatterContext) {
		super(formatterContext);
		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
		asciiNostalgia = formatterContext.getProperties().getBoolean(PROPERTY_ASCII, false);
		trimValues = formatterContext.getProperties().getBoolean(PROPERTY_TRIM, false);
		out.setColorful(formatterContext.getProperties().getBoolean(PROPERTY_COLORFUL, true));
	}

	@Override
	public void writeStartResultSet(ColumnsHeader header) {
		super.writeStartResultSet(header);
		printResultSeparator();

		initColumnWidths(header.getColumnCount());

		printTableIndent();
		printTableBorder("╭");

		List<ColumnDescriptor> columnDescriptors = header.getColumnDescriptors();

		for (ColumnDescriptor cd : columnDescriptors) {
			// padding: make header cell at least same width as data cells in this column
			int typeWidth = cd.getTypeName().length() + HEADER_TYPE_PREFIX.length() + HEADER_TYPE_SUFFIX.length();
			cd.setLabel(rpad(cd.getLabel(), getColumnWidth(cd.getColumnNumber()) - typeWidth));
			updateColumnWidth(cd.getColumnNumber(), cd.getLabel().length() + typeWidth);

			if (!cd.isFirstColumn()) {
				printTableBorder("┬");
			}
			printTableBorder(repeat('─', getColumnWidth(cd.getColumnNumber()) + 2));
		}
		printTableBorder("╮");
		out.println();

		for (ColumnDescriptor cd : columnDescriptors) {
			if (cd.isFirstColumn()) {
				printTableIndent();
				printTableBorder("│ ");
			} else {
				printTableBorder(" │ ");
			}
			out.print(TerminalStyle.Bright, cd.getLabel());
			out.print(HEADER_TYPE_PREFIX);
			out.print(cd.getTypeName());
			out.print(HEADER_TYPE_SUFFIX);
			if (cd.isLastColumn()) {
				printTableBorder(" │");
			}
		}
		out.println();

		printTableIndent();
		printTableBorder("├");
		for (int i = 1; i <= header.getColumnCount(); i++) {
			if (i > 1) {
				printTableBorder("┼");
			}
			printTableBorder(repeat('─', getColumnWidth(i) + 2));
		}
		printTableBorder("┤");
		out.println();

		out.flush();
	}

	/**
	 * Must be called before
	 * {@linkplain #updateColumnWidth(int, int)}
	 * and {@linkplain #getColumnWidth(int)}
	 * for each result set.
	 *
	 * @param columnCount number of columns in current result set
	 */
	protected void initColumnWidths(int columnCount) {
		if (columnWidth == null) {
			columnWidth = new int[columnCount];
		}
	}

	protected void cleanColumnWidths() {
		columnWidth = null;
	}

	@Override
	public void writeColumnValue(Object value) {
		super.writeColumnValue(value);
		writeColumnValueInternal(value);
	}

	protected void writeColumnValueInternal(Object value) {

		if (isCurrentColumnFirst()) {
			printTableIndent();
			printTableBorder("│ ");
		} else {
			printTableBorder(" │ ");
		}

		String valueString = toString(value);
		printValueWithNewLinesReplaced(valueString);

		if (isCurrentColumnLast()) {
			printTableBorder(" │");
		}

	}

	protected int getColumnWidth(int columnNumber) {
		return columnWidth[columnNumber - 1];
	}

	private void setColumnWidth(int columnNumber, int width) {
		columnWidth[columnNumber - 1] = width;
	}

	protected void updateColumnWidth(int columnNumber, int width) {
		int oldWidth = getColumnWidth(columnNumber);
		setColumnWidth(columnNumber, Math.max(width, oldWidth));

	}

	protected String toString(Object value) {
		final int width = getColumnWidth(getCurrentColumnsCount());
		String result;
		if (value instanceof Number || value instanceof Boolean) {
			result = lpad(String.valueOf(value), width);
		} else {
			result = rpad(String.valueOf(value), width);
		}
		// ?	value = (boolean) value ? "✔" : "✗";

		if (trimValues && result.length() > width) {
			result = result.substring(0, width - 1) + "…";
		}

		return result;
	}

	@Override
	public void writeEndRow() {
		super.writeEndRow();
		writeEndRowInternal();
	}

	public void writeEndRowInternal() {
		out.println();
		out.flush();
	}

	@Override
	public void writeEndResultSet() {
		int columnCount = getCurrentColumnsHeader().getColumnCount();
		super.writeEndResultSet();

		printTableIndent();
		printTableBorder("╰");
		for (int i = 1; i <= columnCount; i++) {
			if (i > 1) {
				printTableBorder("┴");
			}
			printTableBorder(repeat('─', getColumnWidth(i) + 2));
		}
		printTableBorder("╯");
		out.println();

		cleanColumnWidths();

		out.print(TerminalColor.Yellow, "Record count: ");
		out.println(getCurrentRowCount());
		out.bell();
		out.flush();
	}

	@Override
	public void writeUpdatesResult(int updatedRowsCount) {
		super.writeUpdatesResult(updatedRowsCount);
		printResultSeparator();
		out.print(TerminalColor.Red, "Updated records: ");
		out.println(updatedRowsCount);
		out.bell();
		out.flush();
	}

	@Override
	public void writeEndDatabase() {
		super.writeEndDatabase();
		out.flush();
	}

	private void printResultSeparator() {
		if (firstResult) {
			firstResult = false;
		} else {
			out.println();
		}
	}

	protected void printTableBorder(String border) {
		if (asciiNostalgia) {
			border = border.replaceAll("─", "-");
			border = border.replaceAll("│", "|");
			border = border.replaceAll("[╭┬╮├┼┤╰┴╯]", "+");
		}

		out.print(TerminalColor.Green, border);
	}

	protected void printTableIndent() {
		out.print(" ");
	}

	protected void printValueWithNewLinesReplaced(String valueString) {
		String[] valueParts = valueString.split("\n");
		for (int i = 0; i < valueParts.length; i++) {
			String valuePart = valueParts[i];
			// TODO: replace also TABs
			out.print(TerminalColor.Cyan, valuePart);
			if (i < valueParts.length - 1) {
				out.print(TerminalColor.Red, "↲");
			}
		}
	}
}