java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Fri, 27 Dec 2013 21:26:30 +0100
branchv_0
changeset 87 03bf24449c7a
parent 79 e19a13ed19a9
child 88 102ba0fcb07f
permissions -rw-r--r--
TabularFormatter: support for value trim (if longer than cell size)
     1 /**
     2  * SQL-DK
     3  * Copyright © 2013 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package info.globalcode.sql.dk.formatting;
    19 
    20 import info.globalcode.sql.dk.ColorfulPrintWriter;
    21 import static info.globalcode.sql.dk.ColorfulPrintWriter.*;
    22 import static info.globalcode.sql.dk.Functions.lpad;
    23 import static info.globalcode.sql.dk.Functions.rpad;
    24 import static info.globalcode.sql.dk.Functions.repeat;
    25 
    26 /**
    27  *
    28  * @author Ing. František Kučera (frantovo.cz)
    29  */
    30 public class TabularFormatter extends AbstractFormatter {
    31 
    32 	public static final String NAME = "tabular"; // bash-completion:formatter
    33 	private static final String HEADER_TYPE_PREFIX = " (";
    34 	private static final String HEADER_TYPE_SUFFIX = ")";
    35 	private ColorfulPrintWriter out;
    36 	private boolean firstResult = true;
    37 	private int[] columnWidth;
    38 	/**
    39 	 * use ASCII borders instead of unicode ones
    40 	 */
    41 	private final boolean asciiNostalgia = false;
    42 	/**
    43 	 * Trim values if they are longer than cell size
    44 	 */
    45 	private final boolean trimValues = false;
    46 
    47 	public TabularFormatter(FormatterContext formatterContext) {
    48 		super(formatterContext);
    49 		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
    50 	}
    51 
    52 	@Override
    53 	public void writeStartResultSet() {
    54 		super.writeStartResultSet();
    55 		printResultSeparator();
    56 	}
    57 
    58 	@Override
    59 	public void writeColumnsHeader(ColumnsHeader header) {
    60 		super.writeColumnsHeader(header);
    61 
    62 		columnWidth = new int[header.getColumnCount()];
    63 
    64 		printTableIndent();
    65 		printTableBorder("╭");
    66 		for (ColumnDescriptor cd : header.getColumnDescriptors()) {
    67 			setColumnWidth(cd.getColumnNumber(), cd.getLabel().length() + cd.getTypeName().length() + HEADER_TYPE_PREFIX.length() + HEADER_TYPE_SUFFIX.length());
    68 			if (!cd.isFirstColumn()) {
    69 				printTableBorder("┬");
    70 			}
    71 			printTableBorder(repeat('─', getColumnWidth(cd.getColumnNumber()) + 2));
    72 		}
    73 		printTableBorder("╮");
    74 		out.println();
    75 
    76 		for (ColumnDescriptor cd : header.getColumnDescriptors()) {
    77 			if (cd.isFirstColumn()) {
    78 				printTableIndent();
    79 				printTableBorder("│ ");
    80 			} else {
    81 				printTableBorder(" │ ");
    82 			}
    83 			out.print(TerminalStyle.Bright, cd.getLabel());
    84 			out.print(HEADER_TYPE_PREFIX);
    85 			out.print(cd.getTypeName());
    86 			out.print(HEADER_TYPE_SUFFIX);
    87 			if (cd.isLastColumn()) {
    88 				printTableBorder(" │");
    89 			}
    90 
    91 			setColumnWidth(cd.getColumnNumber(), cd.getLabel().length() + cd.getTypeName().length() + HEADER_TYPE_PREFIX.length() + HEADER_TYPE_SUFFIX.length());
    92 		}
    93 		out.println();
    94 
    95 		printTableIndent();
    96 		printTableBorder("├");
    97 		for (int i = 1; i <= header.getColumnCount(); i++) {
    98 			if (i > 1) {
    99 				printTableBorder("┼");
   100 			}
   101 			printTableBorder(repeat('─', getColumnWidth(i) + 2));
   102 		}
   103 		printTableBorder("┤");
   104 		out.println();
   105 
   106 		out.flush();
   107 	}
   108 
   109 	@Override
   110 	public void writeColumnValue(Object value) {
   111 		super.writeColumnValue(value);
   112 
   113 		if (isCurrentColumnFirst()) {
   114 			printTableIndent();
   115 			printTableBorder("│ ");
   116 		} else {
   117 			printTableBorder(" │ ");
   118 		}
   119 
   120 		out.print(TerminalColor.Cyan, toString(value));
   121 
   122 		if (isCurrentColumnLast()) {
   123 			printTableBorder(" │");
   124 		}
   125 
   126 	}
   127 
   128 	private int getColumnWidth(int columnNumber) {
   129 		return columnWidth[columnNumber - 1];
   130 	}
   131 
   132 	private void setColumnWidth(int columnNumber, int width) {
   133 		columnWidth[columnNumber - 1] = width;
   134 	}
   135 
   136 	private void updateColumnWidth(int columnNumber, int width) {
   137 		int oldWidth = getColumnWidth(columnNumber);
   138 		setColumnWidth(columnNumber, Math.max(width, oldWidth));
   139 
   140 	}
   141 
   142 	@Override
   143 	protected String toString(Object value) {
   144 		final int width = getColumnWidth(getCurrentColumnsCount());
   145 		String result;
   146 		if (value instanceof Number || value instanceof Boolean) {
   147 			result = lpad(super.toString(value), width);
   148 		} else {
   149 			result = rpad(super.toString(value), width);
   150 		}
   151 		// ?	value = (boolean) value ? "✔" : "✗";
   152 
   153 		if (trimValues && result.length() > width) {
   154 			result = result.substring(0, width - 1) + "…";
   155 		}
   156 
   157 		return result;
   158 	}
   159 
   160 	@Override
   161 	public void writeEndRow() {
   162 		super.writeEndRow();
   163 		out.println();
   164 		out.flush();
   165 	}
   166 
   167 	@Override
   168 	public void writeEndResultSet() {
   169 		int columnCount = getCurrentColumnsHeader().getColumnCount();
   170 		super.writeEndResultSet();
   171 
   172 		printTableIndent();
   173 		printTableBorder("╰");
   174 		for (int i = 1; i <= columnCount; i++) {
   175 			if (i > 1) {
   176 				printTableBorder("┴");
   177 			}
   178 			printTableBorder(repeat('─', getColumnWidth(i) + 2));
   179 		}
   180 		printTableBorder("╯");
   181 		out.println();
   182 
   183 
   184 		out.print(TerminalColor.Yellow, "Record count: ");
   185 		out.println(getCurrentRowCount());
   186 		out.bell();
   187 		out.flush();
   188 	}
   189 
   190 	@Override
   191 	public void writeStartUpdatesResult() {
   192 		super.writeStartUpdatesResult();
   193 		printResultSeparator();
   194 	}
   195 
   196 	@Override
   197 	public void writeUpdatedRowsCount(int updatedRowsCount) {
   198 		super.writeUpdatedRowsCount(updatedRowsCount);
   199 		out.print(TerminalColor.Red, "Updated records: ");
   200 		out.println(updatedRowsCount);
   201 		out.bell();
   202 		out.flush();
   203 	}
   204 
   205 	@Override
   206 	public void writeEndDatabase() {
   207 		super.writeEndDatabase();
   208 		out.flush();
   209 	}
   210 
   211 	private void printResultSeparator() {
   212 		if (firstResult) {
   213 			firstResult = false;
   214 		} else {
   215 			out.println();
   216 		}
   217 	}
   218 
   219 	private void printTableBorder(String border) {
   220 		if (asciiNostalgia) {
   221 			border = border.replaceAll("─", "-");
   222 			border = border.replaceAll("│", "|");
   223 			border = border.replaceAll("[╭┬╮├┼┤╰┴╯]", "+");
   224 		}
   225 
   226 		out.print(TerminalColor.Green, border);
   227 	}
   228 
   229 	private void printTableIndent() {
   230 		out.print(" ");
   231 	}
   232 }