1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java Fri Dec 27 21:26:30 2013 +0100
1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java Sat Dec 28 12:19:39 2013 +0100
1.3 @@ -22,6 +22,8 @@
1.4 import static info.globalcode.sql.dk.Functions.lpad;
1.5 import static info.globalcode.sql.dk.Functions.rpad;
1.6 import static info.globalcode.sql.dk.Functions.repeat;
1.7 +import java.util.Arrays;
1.8 +import java.util.List;
1.9
1.10 /**
1.11 *
1.12 @@ -59,12 +61,19 @@
1.13 public void writeColumnsHeader(ColumnsHeader header) {
1.14 super.writeColumnsHeader(header);
1.15
1.16 - columnWidth = new int[header.getColumnCount()];
1.17 + initColumnWidths(header.getColumnCount());
1.18
1.19 printTableIndent();
1.20 printTableBorder("╭");
1.21 - for (ColumnDescriptor cd : header.getColumnDescriptors()) {
1.22 - setColumnWidth(cd.getColumnNumber(), cd.getLabel().length() + cd.getTypeName().length() + HEADER_TYPE_PREFIX.length() + HEADER_TYPE_SUFFIX.length());
1.23 +
1.24 + List<ColumnDescriptor> columnDescriptors = header.getColumnDescriptors();
1.25 +
1.26 + for (ColumnDescriptor cd : columnDescriptors) {
1.27 + // padding: make header cell at least same width as data cells in this column
1.28 + int typeWidth = cd.getTypeName().length() + HEADER_TYPE_PREFIX.length() + HEADER_TYPE_SUFFIX.length();
1.29 + cd.setLabel(rpad(cd.getLabel(), getColumnWidth(cd.getColumnNumber()) - typeWidth));
1.30 + updateColumnWidth(cd.getColumnNumber(), cd.getLabel().length() + typeWidth);
1.31 +
1.32 if (!cd.isFirstColumn()) {
1.33 printTableBorder("┬");
1.34 }
1.35 @@ -73,7 +82,7 @@
1.36 printTableBorder("╮");
1.37 out.println();
1.38
1.39 - for (ColumnDescriptor cd : header.getColumnDescriptors()) {
1.40 + for (ColumnDescriptor cd : columnDescriptors) {
1.41 if (cd.isFirstColumn()) {
1.42 printTableIndent();
1.43 printTableBorder("│ ");
1.44 @@ -87,8 +96,6 @@
1.45 if (cd.isLastColumn()) {
1.46 printTableBorder(" │");
1.47 }
1.48 -
1.49 - setColumnWidth(cd.getColumnNumber(), cd.getLabel().length() + cd.getTypeName().length() + HEADER_TYPE_PREFIX.length() + HEADER_TYPE_SUFFIX.length());
1.50 }
1.51 out.println();
1.52
1.53 @@ -106,6 +113,24 @@
1.54 out.flush();
1.55 }
1.56
1.57 + /**
1.58 + * Must be called before
1.59 + * {@linkplain #updateColumnWidth(int, int)}
1.60 + * and {@linkplain #getColumnWidth(int)}
1.61 + * for each result set.
1.62 + *
1.63 + * @param columnCount number of columns in current result set
1.64 + */
1.65 + protected void initColumnWidths(int columnCount) {
1.66 + if (columnWidth == null) {
1.67 + columnWidth = new int[columnCount];
1.68 + }
1.69 + }
1.70 +
1.71 + protected void cleanColumnWidths() {
1.72 + columnWidth = null;
1.73 + }
1.74 +
1.75 @Override
1.76 public void writeColumnValue(Object value) {
1.77 super.writeColumnValue(value);
1.78 @@ -133,7 +158,7 @@
1.79 columnWidth[columnNumber - 1] = width;
1.80 }
1.81
1.82 - private void updateColumnWidth(int columnNumber, int width) {
1.83 + protected void updateColumnWidth(int columnNumber, int width) {
1.84 int oldWidth = getColumnWidth(columnNumber);
1.85 setColumnWidth(columnNumber, Math.max(width, oldWidth));
1.86
1.87 @@ -180,6 +205,7 @@
1.88 printTableBorder("╯");
1.89 out.println();
1.90
1.91 + cleanColumnWidths();
1.92
1.93 out.print(TerminalColor.Yellow, "Record count: ");
1.94 out.println(getCurrentRowCount());