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 protected 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);
142 writeColumnValueInternal(value);
145 protected void writeColumnValueInternal(Object value) {
147 if (isCurrentColumnFirst()) {
149 printTableBorder("│ ");
151 printTableBorder(" │ ");
154 String valueString = toString(value);
155 printValueWithNewLinesReplaced(valueString);
157 if (isCurrentColumnLast()) {
158 printTableBorder(" │");
163 protected int getColumnWidth(int columnNumber) {
164 return columnWidth[columnNumber - 1];
167 private void setColumnWidth(int columnNumber, int width) {
168 columnWidth[columnNumber - 1] = width;
171 protected void updateColumnWidth(int columnNumber, int width) {
172 int oldWidth = getColumnWidth(columnNumber);
173 setColumnWidth(columnNumber, Math.max(width, oldWidth));
177 protected String toString(Object value) {
178 final int width = getColumnWidth(getCurrentColumnsCount());
180 if (value instanceof Number || value instanceof Boolean) {
181 result = lpad(String.valueOf(value), width);
183 result = rpad(String.valueOf(value), width);
185 // ? value = (boolean) value ? "✔" : "✗";
187 if (trimValues && result.length() > width) {
188 result = result.substring(0, width - 1) + "…";
195 public void writeEndRow() {
197 writeEndRowInternal();
200 public void writeEndRowInternal() {
206 public void writeEndResultSet() {
207 int columnCount = getCurrentColumnsHeader().getColumnCount();
208 super.writeEndResultSet();
211 printTableBorder("╰");
212 for (int i = 1; i <= columnCount; i++) {
214 printTableBorder("┴");
216 printTableBorder(repeat('─', getColumnWidth(i) + 2));
218 printTableBorder("╯");
223 out.print(TerminalColor.Yellow, "Record count: ");
224 out.println(getCurrentRowCount());
230 public void writeStartUpdatesResult() {
231 super.writeStartUpdatesResult();
232 printResultSeparator();
236 public void writeUpdatedRowsCount(int updatedRowsCount) {
237 super.writeUpdatedRowsCount(updatedRowsCount);
238 out.print(TerminalColor.Red, "Updated records: ");
239 out.println(updatedRowsCount);
245 public void writeEndDatabase() {
246 super.writeEndDatabase();
250 private void printResultSeparator() {
258 protected void printTableBorder(String border) {
259 if (asciiNostalgia) {
260 border = border.replaceAll("─", "-");
261 border = border.replaceAll("│", "|");
262 border = border.replaceAll("[╭┬╮├┼┤╰┴╯]", "+");
265 out.print(TerminalColor.Green, border);
268 protected void printTableIndent() {
272 protected void printValueWithNewLinesReplaced(String valueString) {
273 String[] valueParts = valueString.split("\n");
274 for (int i = 0; i < valueParts.length; i++) {
275 String valuePart = valueParts[i];
276 // TODO: replace also TABs
277 out.print(TerminalColor.Cyan, valuePart);
278 if (i < valueParts.length - 1) {
279 out.print(TerminalColor.Red, "↲");