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;
25 import java.util.List;
29 * @author Ing. František Kučera (frantovo.cz)
31 public class TabularFormatter extends AbstractFormatter {
33 public static final String NAME = "tabular"; // bash-completion:formatter
34 private static final String HEADER_TYPE_PREFIX = " (";
35 private static final String HEADER_TYPE_SUFFIX = ")";
36 public static final String PROPERTY_ASCII = "ascii";
37 public static final String PROPERTY_COLORFUL = "color";
38 public static final String PROPERTY_TRIM = "trim";
39 private ColorfulPrintWriter out;
40 private boolean firstResult = true;
41 private int[] columnWidth;
43 * use ASCII borders instead of unicode ones
45 private final boolean asciiNostalgia;
47 * Trim values if they are longer than cell size
49 private final boolean trimValues;
51 public TabularFormatter(FormatterContext formatterContext) {
52 super(formatterContext);
53 out = new ColorfulPrintWriter(formatterContext.getOutputStream());
54 asciiNostalgia = formatterContext.getProperties().getBoolean(PROPERTY_ASCII, false);
55 trimValues = formatterContext.getProperties().getBoolean(PROPERTY_TRIM, false);
56 out.setColorful(formatterContext.getProperties().getBoolean(PROPERTY_COLORFUL, true));
60 public void writeStartResultSet() {
61 super.writeStartResultSet();
62 printResultSeparator();
66 public void writeColumnsHeader(ColumnsHeader header) {
67 super.writeColumnsHeader(header);
69 initColumnWidths(header.getColumnCount());
72 printTableBorder("╭");
74 List<ColumnDescriptor> columnDescriptors = header.getColumnDescriptors();
76 for (ColumnDescriptor cd : columnDescriptors) {
77 // padding: make header cell at least same width as data cells in this column
78 int typeWidth = cd.getTypeName().length() + HEADER_TYPE_PREFIX.length() + HEADER_TYPE_SUFFIX.length();
79 cd.setLabel(rpad(cd.getLabel(), getColumnWidth(cd.getColumnNumber()) - typeWidth));
80 updateColumnWidth(cd.getColumnNumber(), cd.getLabel().length() + typeWidth);
82 if (!cd.isFirstColumn()) {
83 printTableBorder("┬");
85 printTableBorder(repeat('─', getColumnWidth(cd.getColumnNumber()) + 2));
87 printTableBorder("╮");
90 for (ColumnDescriptor cd : columnDescriptors) {
91 if (cd.isFirstColumn()) {
93 printTableBorder("│ ");
95 printTableBorder(" │ ");
97 out.print(TerminalStyle.Bright, cd.getLabel());
98 out.print(HEADER_TYPE_PREFIX);
99 out.print(cd.getTypeName());
100 out.print(HEADER_TYPE_SUFFIX);
101 if (cd.isLastColumn()) {
102 printTableBorder(" │");
108 printTableBorder("├");
109 for (int i = 1; i <= header.getColumnCount(); i++) {
111 printTableBorder("┼");
113 printTableBorder(repeat('─', getColumnWidth(i) + 2));
115 printTableBorder("┤");
122 * Must be called before
123 * {@linkplain #updateColumnWidth(int, int)}
124 * and {@linkplain #getColumnWidth(int)}
125 * for each result set.
127 * @param columnCount number of columns in current result set
129 protected void initColumnWidths(int columnCount) {
130 if (columnWidth == null) {
131 columnWidth = new int[columnCount];
135 protected void cleanColumnWidths() {
140 public void writeColumnValue(Object value) {
141 super.writeColumnValue(value);
143 if (isCurrentColumnFirst()) {
145 printTableBorder("│ ");
147 printTableBorder(" │ ");
150 String[] valueParts = toString(value).split("\n");
151 for (int i = 0; i < valueParts.length; i++) {
152 String valuePart = valueParts[i];
153 out.print(TerminalColor.Cyan, valuePart);
154 if (i < valueParts.length - 1) {
155 out.print(TerminalColor.Red, "↲");
159 if (isCurrentColumnLast()) {
160 printTableBorder(" │");
165 private int getColumnWidth(int columnNumber) {
166 return columnWidth[columnNumber - 1];
169 private void setColumnWidth(int columnNumber, int width) {
170 columnWidth[columnNumber - 1] = width;
173 protected void updateColumnWidth(int columnNumber, int width) {
174 int oldWidth = getColumnWidth(columnNumber);
175 setColumnWidth(columnNumber, Math.max(width, oldWidth));
179 protected String toString(Object value) {
180 final int width = getColumnWidth(getCurrentColumnsCount());
182 if (value instanceof Number || value instanceof Boolean) {
183 result = lpad(String.valueOf(value), width);
185 result = rpad(String.valueOf(value), width);
187 // ? value = (boolean) value ? "✔" : "✗";
189 if (trimValues && result.length() > width) {
190 result = result.substring(0, width - 1) + "…";
197 public void writeEndRow() {
204 public void writeEndResultSet() {
205 int columnCount = getCurrentColumnsHeader().getColumnCount();
206 super.writeEndResultSet();
209 printTableBorder("╰");
210 for (int i = 1; i <= columnCount; i++) {
212 printTableBorder("┴");
214 printTableBorder(repeat('─', getColumnWidth(i) + 2));
216 printTableBorder("╯");
221 out.print(TerminalColor.Yellow, "Record count: ");
222 out.println(getCurrentRowCount());
228 public void writeStartUpdatesResult() {
229 super.writeStartUpdatesResult();
230 printResultSeparator();
234 public void writeUpdatedRowsCount(int updatedRowsCount) {
235 super.writeUpdatedRowsCount(updatedRowsCount);
236 out.print(TerminalColor.Red, "Updated records: ");
237 out.println(updatedRowsCount);
243 public void writeEndDatabase() {
244 super.writeEndDatabase();
248 private void printResultSeparator() {
256 private void printTableBorder(String border) {
257 if (asciiNostalgia) {
258 border = border.replaceAll("─", "-");
259 border = border.replaceAll("│", "|");
260 border = border.replaceAll("[╭┬╮├┼┤╰┴╯]", "+");
263 out.print(TerminalColor.Green, border);
266 private void printTableIndent() {