# HG changeset patch # User František Kučera # Date 1388781360 -3600 # Node ID 248a98c13ca4d24b5dc11707d49b785ea9fba1f9 # Parent 0c284726a77dc4420157c9269d8f6ac80c6a1f4d TabularWrappingFormatter – first version diff -r 0c284726a77d -r 248a98c13ca4 java/sql-dk/src/info/globalcode/sql/dk/configuration/Configuration.java --- a/java/sql-dk/src/info/globalcode/sql/dk/configuration/Configuration.java Fri Jan 03 00:27:16 2014 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/Configuration.java Fri Jan 03 21:36:00 2014 +0100 @@ -23,6 +23,7 @@ import info.globalcode.sql.dk.formatting.SingleValueFormatter; import info.globalcode.sql.dk.formatting.TabularFormatter; import info.globalcode.sql.dk.formatting.TabularPrefetchingFormatter; +import info.globalcode.sql.dk.formatting.TabularWrappingFormatter; import info.globalcode.sql.dk.formatting.XmlFormatter; import java.util.ArrayList; import java.util.Collection; @@ -63,6 +64,7 @@ l.add(new FormatterDefinition(XmlFormatter.NAME, XmlFormatter.class.getName())); l.add(new FormatterDefinition(TabularFormatter.NAME, TabularFormatter.class.getName())); l.add(new FormatterDefinition(TabularPrefetchingFormatter.NAME, TabularPrefetchingFormatter.class.getName())); + l.add(new FormatterDefinition(TabularWrappingFormatter.NAME, TabularWrappingFormatter.class.getName())); buildInFormatters = Collections.unmodifiableCollection(l); } diff -r 0c284726a77d -r 248a98c13ca4 java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java Fri Jan 03 00:27:16 2014 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java Fri Jan 03 21:36:00 2014 +0100 @@ -36,7 +36,7 @@ public static final String PROPERTY_ASCII = "ascii"; public static final String PROPERTY_COLORFUL = "color"; public static final String PROPERTY_TRIM = "trim"; - private ColorfulPrintWriter out; + protected ColorfulPrintWriter out; private boolean firstResult = true; private int[] columnWidth; /** @@ -139,6 +139,10 @@ @Override public void writeColumnValue(Object value) { super.writeColumnValue(value); + writeColumnValueInternal(value); + } + + protected void writeColumnValueInternal(Object value) { if (isCurrentColumnFirst()) { printTableIndent(); @@ -147,14 +151,8 @@ printTableBorder(" │ "); } - String[] valueParts = toString(value).split("\n"); - for (int i = 0; i < valueParts.length; i++) { - String valuePart = valueParts[i]; - out.print(TerminalColor.Cyan, valuePart); - if (i < valueParts.length - 1) { - out.print(TerminalColor.Red, "↲"); - } - } + String valueString = toString(value); + printValueWithNewLinesReplaced(valueString); if (isCurrentColumnLast()) { printTableBorder(" │"); @@ -162,7 +160,7 @@ } - private int getColumnWidth(int columnNumber) { + protected int getColumnWidth(int columnNumber) { return columnWidth[columnNumber - 1]; } @@ -196,6 +194,10 @@ @Override public void writeEndRow() { super.writeEndRow(); + writeEndRowInternal(); + } + + public void writeEndRowInternal() { out.println(); out.flush(); } @@ -253,7 +255,7 @@ } } - private void printTableBorder(String border) { + protected void printTableBorder(String border) { if (asciiNostalgia) { border = border.replaceAll("─", "-"); border = border.replaceAll("│", "|"); @@ -263,7 +265,18 @@ out.print(TerminalColor.Green, border); } - private void printTableIndent() { + protected void printTableIndent() { out.print(" "); } + + protected void printValueWithNewLinesReplaced(String valueString) { + String[] valueParts = valueString.split("\n"); + for (int i = 0; i < valueParts.length; i++) { + String valuePart = valueParts[i]; + out.print(TerminalColor.Cyan, valuePart); + if (i < valueParts.length - 1) { + out.print(TerminalColor.Red, "↲"); + } + } + } } diff -r 0c284726a77d -r 248a98c13ca4 java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularWrappingFormatter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularWrappingFormatter.java Fri Jan 03 21:36:00 2014 +0100 @@ -0,0 +1,114 @@ +/** + * SQL-DK + * Copyright © 2014 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package info.globalcode.sql.dk.formatting; + +import info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor; +import java.util.ArrayList; +import java.util.List; +import static info.globalcode.sql.dk.Functions.lpad; +import static info.globalcode.sql.dk.Functions.rpad; +import static info.globalcode.sql.dk.Functions.repeat; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class TabularWrappingFormatter extends TabularFormatter { + + public static final String NAME = "tabular-wrapping"; // bash-completion:formatter + private List currentRow; + + public TabularWrappingFormatter(FormatterContext formatterContext) { + super(formatterContext); + } + + @Override + public void writeColumnsHeader(ColumnsHeader header) { + super.writeColumnsHeader(header); + currentRow = new ArrayList<>(header.getColumnCount()); + } + + @Override + protected void writeColumnValueInternal(Object value) { + boolean rightAlign = value instanceof Number || value instanceof Boolean; + String valueString = String.valueOf(value); + int columnWidth = getColumnWidth(getCurrentColumnsCount()) - 1; // -1 = space for new line symbol + currentRow.add(split(valueString, columnWidth, rightAlign)); + } + + @Override + public void writeEndRow() { + super.writeEndRow(); + + int wrappedLine = 0; + boolean hasMoreWrappedLines; + + do { + hasMoreWrappedLines = false; + for (int i = 0; i < currentRow.size(); i++) { + if (i == 0) { + printTableIndent(); + printTableBorder("│ "); + } else { + printTableBorder(" │ "); + } + String[] columnArray = currentRow.get(i); + if (wrappedLine < columnArray.length) { + printValueWithNewLinesReplaced(columnArray[wrappedLine]); + + if (wrappedLine < columnArray.length - 1) { + out.print(TerminalColor.Red, "↩"); + hasMoreWrappedLines = true; + } else { + out.print(" "); + } + + } else { + out.print(repeat(' ', getColumnWidth(i + 1))); + } + + if (i == (currentRow.size() - 1)) { + printTableBorder(" │"); + } + } + out.println(); + out.flush(); + wrappedLine++; + } while (hasMoreWrappedLines); + + currentRow.clear(); + } + + @Override + public void writeEndRowInternal() { + // already done – wrapped row ends + } + + private static String[] split(String s, int width, boolean rightAlign) { + String[] array = new String[(s.length() - 1) / width + 1]; + for (int i = 0; i < array.length; i++) { + if (i == array.length - 1) { + String part = s.substring(i * width, s.length()); + array[i] = rightAlign ? lpad(part, width) : rpad(part, width); + } else { + array[i] = s.substring(i * width, (i + 1) * width); + } + } + return array; + } +} \ No newline at end of file