franta-hg@32: /** franta-hg@32: * SQL-DK franta-hg@32: * Copyright © 2013 František Kučera (frantovo.cz) franta-hg@32: * franta-hg@32: * This program is free software: you can redistribute it and/or modify franta-hg@32: * it under the terms of the GNU General Public License as published by franta-hg@32: * the Free Software Foundation, either version 3 of the License, or franta-hg@32: * (at your option) any later version. franta-hg@32: * franta-hg@32: * This program is distributed in the hope that it will be useful, franta-hg@32: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@32: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@32: * GNU General Public License for more details. franta-hg@32: * franta-hg@32: * You should have received a copy of the GNU General Public License franta-hg@32: * along with this program. If not, see . franta-hg@32: */ franta-hg@32: package info.globalcode.sql.dk.formatting; franta-hg@32: franta-hg@34: import info.globalcode.sql.dk.ColorfulPrintWriter; franta-hg@37: import static info.globalcode.sql.dk.ColorfulPrintWriter.*; franta-hg@39: import static info.globalcode.sql.dk.Functions.lpad; franta-hg@39: import static info.globalcode.sql.dk.Functions.rpad; franta-hg@34: franta-hg@32: /** franta-hg@32: * franta-hg@32: * @author Ing. František Kučera (frantovo.cz) franta-hg@32: */ franta-hg@32: public class TabularFormatter extends AbstractFormatter { franta-hg@32: franta-hg@32: public static final String NAME = "tabular"; franta-hg@39: private static final String HEADER_TYPE_PREFIX = " ("; franta-hg@39: private static final String HEADER_TYPE_SUFFIX = ")"; franta-hg@34: private ColorfulPrintWriter out; franta-hg@37: private boolean firstResult = true; franta-hg@39: private int[] columnWidth; franta-hg@32: franta-hg@32: public TabularFormatter(FormatterContext formatterContext) { franta-hg@32: super(formatterContext); franta-hg@34: out = new ColorfulPrintWriter(formatterContext.getOutputStream()); franta-hg@34: } franta-hg@34: franta-hg@34: @Override franta-hg@37: public void writeStartResultSet() { franta-hg@37: super.writeStartResultSet(); franta-hg@37: printResultSeparator(); franta-hg@37: } franta-hg@37: franta-hg@37: @Override franta-hg@37: public void writeColumnsHeader(ColumnsHeader header) { franta-hg@37: super.writeColumnsHeader(header); franta-hg@37: franta-hg@39: columnWidth = new int[header.getColumnCount()]; franta-hg@39: franta-hg@37: for (ColumnDescriptor cd : header.getColumnDescriptors()) { franta-hg@37: out.print(TerminalStyle.Bright, cd.getLabel()); franta-hg@39: out.print(HEADER_TYPE_PREFIX); franta-hg@37: out.print(cd.getTypeName()); franta-hg@39: out.print(HEADER_TYPE_SUFFIX); franta-hg@37: if (!cd.isLastColumn()) { franta-hg@37: out.print(TerminalColor.Green, " | "); franta-hg@37: } franta-hg@39: franta-hg@39: setColumnWidth(cd.getColumnNumber(), cd.getLabel().length() + cd.getTypeName().length() + HEADER_TYPE_PREFIX.length() + HEADER_TYPE_SUFFIX.length()); franta-hg@37: } franta-hg@37: out.println(); franta-hg@37: out.flush(); franta-hg@37: } franta-hg@37: franta-hg@37: @Override franta-hg@34: public void writeColumnValue(Object value) { franta-hg@34: super.writeColumnValue(value); franta-hg@34: franta-hg@34: if (!isCurrentColumnFirst()) { franta-hg@37: out.print(TerminalColor.Green, " | "); franta-hg@34: } franta-hg@37: franta-hg@39: out.print(TerminalColor.Cyan, toString(value)); franta-hg@39: } franta-hg@39: franta-hg@39: private int getColumnWidth(int columnNumber) { franta-hg@39: return columnWidth[columnNumber - 1]; franta-hg@39: } franta-hg@39: franta-hg@39: private void setColumnWidth(int columnNumber, int width) { franta-hg@39: columnWidth[columnNumber - 1] = width; franta-hg@39: } franta-hg@39: franta-hg@39: private void updateColumnWidth(int columnNumber, int width) { franta-hg@39: int oldWidth = getColumnWidth(columnNumber); franta-hg@39: setColumnWidth(columnNumber, Math.max(width, oldWidth)); franta-hg@39: franta-hg@39: } franta-hg@39: franta-hg@39: @Override franta-hg@39: protected String toString(Object value) { franta-hg@39: final int width = getColumnWidth(getCurrentColumnsCount()); franta-hg@39: if (value instanceof Number) { franta-hg@39: return lpad(super.toString(value), width); franta-hg@39: } else { franta-hg@39: return rpad(super.toString(value), width); franta-hg@39: } franta-hg@34: } franta-hg@34: franta-hg@34: @Override franta-hg@34: public void writeEndRow() { franta-hg@34: super.writeEndRow(); franta-hg@34: out.println(); franta-hg@34: out.flush(); franta-hg@32: } franta-hg@37: franta-hg@37: @Override franta-hg@37: public void writeEndResultSet() { franta-hg@37: super.writeEndResultSet(); franta-hg@37: out.print(TerminalColor.Yellow, "Record count: "); franta-hg@37: out.println(getCurrentRowCount()); franta-hg@37: out.flush(); franta-hg@37: } franta-hg@37: franta-hg@37: @Override franta-hg@37: public void writeStartUpdatesResult() { franta-hg@37: super.writeStartUpdatesResult(); franta-hg@37: printResultSeparator(); franta-hg@37: } franta-hg@37: franta-hg@37: @Override franta-hg@37: public void writeUpdatedRowsCount(int updatedRowsCount) { franta-hg@37: super.writeUpdatedRowsCount(updatedRowsCount); franta-hg@37: out.print(TerminalColor.Red, "Updated records: "); franta-hg@37: out.println(updatedRowsCount); franta-hg@37: out.flush(); franta-hg@37: } franta-hg@37: franta-hg@37: @Override franta-hg@37: public void writeEndDatabase() { franta-hg@37: super.writeEndDatabase(); franta-hg@37: out.flush(); franta-hg@37: } franta-hg@37: franta-hg@37: private void printResultSeparator() { franta-hg@37: if (firstResult) { franta-hg@37: firstResult = false; franta-hg@37: } else { franta-hg@37: out.println(); franta-hg@37: } franta-hg@37: } franta-hg@32: }