java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Fri, 27 Dec 2013 14:05:38 +0100
branchv_0
changeset 76 fe23cea7542f
parent 43 2813d3409afd
child 79 e19a13ed19a9
permissions -rw-r--r--
support for ACSII-nostalgia tables
     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";
    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 	private final boolean asciiNostalgia = false;
    39 
    40 	public TabularFormatter(FormatterContext formatterContext) {
    41 		super(formatterContext);
    42 		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
    43 	}
    44 
    45 	@Override
    46 	public void writeStartResultSet() {
    47 		super.writeStartResultSet();
    48 		printResultSeparator();
    49 	}
    50 
    51 	@Override
    52 	public void writeColumnsHeader(ColumnsHeader header) {
    53 		super.writeColumnsHeader(header);
    54 
    55 		columnWidth = new int[header.getColumnCount()];
    56 
    57 		printTableIndent();
    58 		printTableBorder("╭");
    59 		for (ColumnDescriptor cd : header.getColumnDescriptors()) {
    60 			setColumnWidth(cd.getColumnNumber(), cd.getLabel().length() + cd.getTypeName().length() + HEADER_TYPE_PREFIX.length() + HEADER_TYPE_SUFFIX.length());
    61 			if (!cd.isFirstColumn()) {
    62 				printTableBorder("┬");
    63 			}
    64 			printTableBorder(repeat('─', getColumnWidth(cd.getColumnNumber()) + 2));
    65 		}
    66 		printTableBorder("╮");
    67 		out.println();
    68 
    69 		for (ColumnDescriptor cd : header.getColumnDescriptors()) {
    70 			if (cd.isFirstColumn()) {
    71 				printTableIndent();
    72 				printTableBorder("│ ");
    73 			} else {
    74 				printTableBorder(" │ ");
    75 			}
    76 			out.print(TerminalStyle.Bright, cd.getLabel());
    77 			out.print(HEADER_TYPE_PREFIX);
    78 			out.print(cd.getTypeName());
    79 			out.print(HEADER_TYPE_SUFFIX);
    80 			if (cd.isLastColumn()) {
    81 				printTableBorder(" │");
    82 			}
    83 
    84 			setColumnWidth(cd.getColumnNumber(), cd.getLabel().length() + cd.getTypeName().length() + HEADER_TYPE_PREFIX.length() + HEADER_TYPE_SUFFIX.length());
    85 		}
    86 		out.println();
    87 
    88 		printTableIndent();
    89 		printTableBorder("├");
    90 		for (int i = 1; i <= header.getColumnCount(); i++) {
    91 			if (i > 1) {
    92 				printTableBorder("┼");
    93 			}
    94 			printTableBorder(repeat('─', getColumnWidth(i) + 2));
    95 		}
    96 		printTableBorder("┤");
    97 		out.println();
    98 
    99 		out.flush();
   100 	}
   101 
   102 	@Override
   103 	public void writeColumnValue(Object value) {
   104 		super.writeColumnValue(value);
   105 
   106 		if (isCurrentColumnFirst()) {
   107 			printTableIndent();
   108 			printTableBorder("│ ");
   109 		} else {
   110 			printTableBorder(" │ ");
   111 		}
   112 
   113 		out.print(TerminalColor.Cyan, toString(value));
   114 
   115 		if (isCurrentColumnLast()) {
   116 			printTableBorder(" │");
   117 		}
   118 
   119 	}
   120 
   121 	private int getColumnWidth(int columnNumber) {
   122 		return columnWidth[columnNumber - 1];
   123 	}
   124 
   125 	private void setColumnWidth(int columnNumber, int width) {
   126 		columnWidth[columnNumber - 1] = width;
   127 	}
   128 
   129 	private void updateColumnWidth(int columnNumber, int width) {
   130 		int oldWidth = getColumnWidth(columnNumber);
   131 		setColumnWidth(columnNumber, Math.max(width, oldWidth));
   132 
   133 	}
   134 
   135 	@Override
   136 	protected String toString(Object value) {
   137 		final int width = getColumnWidth(getCurrentColumnsCount());
   138 		if (value instanceof Number || value instanceof Boolean) {
   139 			return lpad(super.toString(value), width);
   140 		} else {
   141 			return rpad(super.toString(value), width);
   142 		}
   143 		// ?	value = (boolean) value ? "✔" : "✗";
   144 	}
   145 
   146 	@Override
   147 	public void writeEndRow() {
   148 		super.writeEndRow();
   149 		out.println();
   150 		out.flush();
   151 	}
   152 
   153 	@Override
   154 	public void writeEndResultSet() {
   155 		int columnCount = getCurrentColumnsHeader().getColumnCount();
   156 		super.writeEndResultSet();
   157 
   158 		printTableIndent();
   159 		printTableBorder("╰");
   160 		for (int i = 1; i <= columnCount; i++) {
   161 			if (i > 1) {
   162 				printTableBorder("┴");
   163 			}
   164 			printTableBorder(repeat('─', getColumnWidth(i) + 2));
   165 		}
   166 		printTableBorder("╯");
   167 		out.println();
   168 
   169 
   170 		out.print(TerminalColor.Yellow, "Record count: ");
   171 		out.println(getCurrentRowCount());
   172 		out.bell();
   173 		out.flush();
   174 	}
   175 
   176 	@Override
   177 	public void writeStartUpdatesResult() {
   178 		super.writeStartUpdatesResult();
   179 		printResultSeparator();
   180 	}
   181 
   182 	@Override
   183 	public void writeUpdatedRowsCount(int updatedRowsCount) {
   184 		super.writeUpdatedRowsCount(updatedRowsCount);
   185 		out.print(TerminalColor.Red, "Updated records: ");
   186 		out.println(updatedRowsCount);
   187 		out.bell();
   188 		out.flush();
   189 	}
   190 
   191 	@Override
   192 	public void writeEndDatabase() {
   193 		super.writeEndDatabase();
   194 		out.flush();
   195 	}
   196 
   197 	private void printResultSeparator() {
   198 		if (firstResult) {
   199 			firstResult = false;
   200 		} else {
   201 			out.println();
   202 		}
   203 	}
   204 
   205 	private void printTableBorder(String border) {
   206 		if (asciiNostalgia) {
   207 			border = border.replaceAll("─", "-");
   208 			border = border.replaceAll("│", "|");
   209 			border = border.replaceAll("[╭┬╮├┼┤╰┴╯]", "+");
   210 		}
   211 		
   212 		out.print(TerminalColor.Green, border);
   213 	}
   214 
   215 	private void printTableIndent() {
   216 		out.print(" ");
   217 	}
   218 }