3 * Copyright © 2013 František Kučera (frantovo.cz)
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.
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.
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/>.
18 package info.globalcode.sql.dk.formatting;
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;
28 * @author Ing. František Kučera (frantovo.cz)
30 public class TabularFormatter extends AbstractFormatter {
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;
39 * use ASCII borders instead of unicode ones
41 private final boolean asciiNostalgia = false;
43 * Trim values if they are longer than cell size
45 private final boolean trimValues = false;
47 public TabularFormatter(FormatterContext formatterContext) {
48 super(formatterContext);
49 out = new ColorfulPrintWriter(formatterContext.getOutputStream());
53 public void writeStartResultSet() {
54 super.writeStartResultSet();
55 printResultSeparator();
59 public void writeColumnsHeader(ColumnsHeader header) {
60 super.writeColumnsHeader(header);
62 columnWidth = new int[header.getColumnCount()];
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("┬");
71 printTableBorder(repeat('─', getColumnWidth(cd.getColumnNumber()) + 2));
73 printTableBorder("╮");
76 for (ColumnDescriptor cd : header.getColumnDescriptors()) {
77 if (cd.isFirstColumn()) {
79 printTableBorder("│ ");
81 printTableBorder(" │ ");
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(" │");
91 setColumnWidth(cd.getColumnNumber(), cd.getLabel().length() + cd.getTypeName().length() + HEADER_TYPE_PREFIX.length() + HEADER_TYPE_SUFFIX.length());
96 printTableBorder("├");
97 for (int i = 1; i <= header.getColumnCount(); i++) {
99 printTableBorder("┼");
101 printTableBorder(repeat('─', getColumnWidth(i) + 2));
103 printTableBorder("┤");
110 public void writeColumnValue(Object value) {
111 super.writeColumnValue(value);
113 if (isCurrentColumnFirst()) {
115 printTableBorder("│ ");
117 printTableBorder(" │ ");
120 out.print(TerminalColor.Cyan, toString(value));
122 if (isCurrentColumnLast()) {
123 printTableBorder(" │");
128 private int getColumnWidth(int columnNumber) {
129 return columnWidth[columnNumber - 1];
132 private void setColumnWidth(int columnNumber, int width) {
133 columnWidth[columnNumber - 1] = width;
136 private void updateColumnWidth(int columnNumber, int width) {
137 int oldWidth = getColumnWidth(columnNumber);
138 setColumnWidth(columnNumber, Math.max(width, oldWidth));
143 protected String toString(Object value) {
144 final int width = getColumnWidth(getCurrentColumnsCount());
146 if (value instanceof Number || value instanceof Boolean) {
147 result = lpad(super.toString(value), width);
149 result = rpad(super.toString(value), width);
151 // ? value = (boolean) value ? "✔" : "✗";
153 if (trimValues && result.length() > width) {
154 result = result.substring(0, width - 1) + "…";
161 public void writeEndRow() {
168 public void writeEndResultSet() {
169 int columnCount = getCurrentColumnsHeader().getColumnCount();
170 super.writeEndResultSet();
173 printTableBorder("╰");
174 for (int i = 1; i <= columnCount; i++) {
176 printTableBorder("┴");
178 printTableBorder(repeat('─', getColumnWidth(i) + 2));
180 printTableBorder("╯");
184 out.print(TerminalColor.Yellow, "Record count: ");
185 out.println(getCurrentRowCount());
191 public void writeStartUpdatesResult() {
192 super.writeStartUpdatesResult();
193 printResultSeparator();
197 public void writeUpdatedRowsCount(int updatedRowsCount) {
198 super.writeUpdatedRowsCount(updatedRowsCount);
199 out.print(TerminalColor.Red, "Updated records: ");
200 out.println(updatedRowsCount);
206 public void writeEndDatabase() {
207 super.writeEndDatabase();
211 private void printResultSeparator() {
219 private void printTableBorder(String border) {
220 if (asciiNostalgia) {
221 border = border.replaceAll("─", "-");
222 border = border.replaceAll("│", "|");
223 border = border.replaceAll("[╭┬╮├┼┤╰┴╯]", "+");
226 out.print(TerminalColor.Green, border);
229 private void printTableIndent() {