java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 23 Dec 2013 12:16:22 +0100
branchv_0
changeset 39 be8db46a38c3
parent 37 9e6f8e5d5f98
child 40 a9db7fb3ce65
permissions -rw-r--r--
TabularFormatter: basic column padding
     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 
    25 /**
    26  *
    27  * @author Ing. František Kučera (frantovo.cz)
    28  */
    29 public class TabularFormatter extends AbstractFormatter {
    30 
    31 	public static final String NAME = "tabular";
    32 	private static final String HEADER_TYPE_PREFIX = " (";
    33 	private static final String HEADER_TYPE_SUFFIX = ")";
    34 	private ColorfulPrintWriter out;
    35 	private boolean firstResult = true;
    36 	private int[] columnWidth;
    37 
    38 	public TabularFormatter(FormatterContext formatterContext) {
    39 		super(formatterContext);
    40 		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
    41 	}
    42 
    43 	@Override
    44 	public void writeStartResultSet() {
    45 		super.writeStartResultSet();
    46 		printResultSeparator();
    47 	}
    48 
    49 	@Override
    50 	public void writeColumnsHeader(ColumnsHeader header) {
    51 		super.writeColumnsHeader(header);
    52 
    53 		columnWidth = new int[header.getColumnCount()];
    54 
    55 		for (ColumnDescriptor cd : header.getColumnDescriptors()) {
    56 			out.print(TerminalStyle.Bright, cd.getLabel());
    57 			out.print(HEADER_TYPE_PREFIX);
    58 			out.print(cd.getTypeName());
    59 			out.print(HEADER_TYPE_SUFFIX);
    60 			if (!cd.isLastColumn()) {
    61 				out.print(TerminalColor.Green, " | ");
    62 			}
    63 
    64 			setColumnWidth(cd.getColumnNumber(), cd.getLabel().length() + cd.getTypeName().length() + HEADER_TYPE_PREFIX.length() + HEADER_TYPE_SUFFIX.length());
    65 		}
    66 		out.println();
    67 		out.flush();
    68 	}
    69 
    70 	@Override
    71 	public void writeColumnValue(Object value) {
    72 		super.writeColumnValue(value);
    73 
    74 		if (!isCurrentColumnFirst()) {
    75 			out.print(TerminalColor.Green, " | ");
    76 		}
    77 
    78 		out.print(TerminalColor.Cyan, toString(value));
    79 	}
    80 
    81 	private int getColumnWidth(int columnNumber) {
    82 		return columnWidth[columnNumber - 1];
    83 	}
    84 
    85 	private void setColumnWidth(int columnNumber, int width) {
    86 		columnWidth[columnNumber - 1] = width;
    87 	}
    88 
    89 	private void updateColumnWidth(int columnNumber, int width) {
    90 		int oldWidth = getColumnWidth(columnNumber);
    91 		setColumnWidth(columnNumber, Math.max(width, oldWidth));
    92 
    93 	}
    94 
    95 	@Override
    96 	protected String toString(Object value) {
    97 		final int width = getColumnWidth(getCurrentColumnsCount());
    98 		if (value instanceof Number) {
    99 			return lpad(super.toString(value), width);
   100 		} else {
   101 			return rpad(super.toString(value), width);
   102 		}
   103 	}
   104 
   105 	@Override
   106 	public void writeEndRow() {
   107 		super.writeEndRow();
   108 		out.println();
   109 		out.flush();
   110 	}
   111 
   112 	@Override
   113 	public void writeEndResultSet() {
   114 		super.writeEndResultSet();
   115 		out.print(TerminalColor.Yellow, "Record count: ");
   116 		out.println(getCurrentRowCount());
   117 		out.flush();
   118 	}
   119 
   120 	@Override
   121 	public void writeStartUpdatesResult() {
   122 		super.writeStartUpdatesResult();
   123 		printResultSeparator();
   124 	}
   125 
   126 	@Override
   127 	public void writeUpdatedRowsCount(int updatedRowsCount) {
   128 		super.writeUpdatedRowsCount(updatedRowsCount);
   129 		out.print(TerminalColor.Red, "Updated records: ");
   130 		out.println(updatedRowsCount);
   131 		out.flush();
   132 	}
   133 
   134 	@Override
   135 	public void writeEndDatabase() {
   136 		super.writeEndDatabase();
   137 		out.flush();
   138 	}
   139 
   140 	private void printResultSeparator() {
   141 		if (firstResult) {
   142 			firstResult = false;
   143 		} else {
   144 			out.println();
   145 		}
   146 	}
   147 }