1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularWrappingFormatter.java Mon Mar 04 17:06:42 2019 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,116 +0,0 @@
1.4 -/**
1.5 - * SQL-DK
1.6 - * Copyright © 2014 František Kučera (frantovo.cz)
1.7 - *
1.8 - * This program is free software: you can redistribute it and/or modify
1.9 - * it under the terms of the GNU General Public License as published by
1.10 - * the Free Software Foundation, either version 3 of the License, or
1.11 - * (at your option) any later version.
1.12 - *
1.13 - * This program is distributed in the hope that it will be useful,
1.14 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.15 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.16 - * GNU General Public License for more details.
1.17 - *
1.18 - * You should have received a copy of the GNU General Public License
1.19 - * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.20 - */
1.21 -package info.globalcode.sql.dk.formatting;
1.22 -
1.23 -import info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor;
1.24 -import java.util.ArrayList;
1.25 -import java.util.List;
1.26 -import static info.globalcode.sql.dk.Functions.lpad;
1.27 -import static info.globalcode.sql.dk.Functions.rpad;
1.28 -import static info.globalcode.sql.dk.Functions.repeat;
1.29 -
1.30 -/**
1.31 - * Longer values are line-wrapped – the cell then contains multiple lines. Marks are added to
1.32 - * signalize forced line ends (not present in original data).
1.33 - *
1.34 - * @author Ing. František Kučera (frantovo.cz)
1.35 - */
1.36 -public class TabularWrappingFormatter extends TabularFormatter {
1.37 -
1.38 - public static final String NAME = "tabular-wrapping"; // bash-completion:formatter
1.39 - private List<String[]> currentRow;
1.40 -
1.41 - public TabularWrappingFormatter(FormatterContext formatterContext) {
1.42 - super(formatterContext);
1.43 - }
1.44 -
1.45 - @Override
1.46 - public void writeStartResultSet(ColumnsHeader header) {
1.47 - super.writeStartResultSet(header);
1.48 - currentRow = new ArrayList<>(header.getColumnCount());
1.49 - }
1.50 -
1.51 - @Override
1.52 - protected void writeColumnValueInternal(Object value) {
1.53 - boolean rightAlign = value instanceof Number || value instanceof Boolean;
1.54 - String valueString = String.valueOf(value);
1.55 - int columnWidth = getColumnWidth(getCurrentColumnsCount()) - 1; // -1 = space for new line symbol
1.56 - currentRow.add(wrapLines(valueString, columnWidth, rightAlign));
1.57 - }
1.58 -
1.59 - @Override
1.60 - public void writeEndRow() {
1.61 - super.writeEndRow();
1.62 -
1.63 - int wrappedLine = 0;
1.64 - boolean hasMoreWrappedLines;
1.65 -
1.66 - do {
1.67 - hasMoreWrappedLines = false;
1.68 - for (int i = 0; i < currentRow.size(); i++) {
1.69 - if (i == 0) {
1.70 - printTableIndent();
1.71 - printTableBorder("│ ");
1.72 - } else {
1.73 - printTableBorder(" │ ");
1.74 - }
1.75 - String[] columnArray = currentRow.get(i);
1.76 - if (wrappedLine < columnArray.length) {
1.77 - printValueWithWhitespaceReplaced(columnArray[wrappedLine]);
1.78 -
1.79 - if (wrappedLine < columnArray.length - 1) {
1.80 - out.print(TerminalColor.Red, "↩");
1.81 - hasMoreWrappedLines = true;
1.82 - } else {
1.83 - out.print(" ");
1.84 - }
1.85 -
1.86 - } else {
1.87 - out.print(repeat(' ', getColumnWidth(i + 1)));
1.88 - }
1.89 -
1.90 - if (i == (currentRow.size() - 1)) {
1.91 - printTableBorder(" │");
1.92 - }
1.93 - }
1.94 - out.println();
1.95 - out.flush();
1.96 - wrappedLine++;
1.97 - } while (hasMoreWrappedLines);
1.98 -
1.99 - currentRow.clear();
1.100 - }
1.101 -
1.102 - @Override
1.103 - public void writeEndRowInternal() {
1.104 - // already done – wrapped row ends
1.105 - }
1.106 -
1.107 - private static String[] wrapLines(String s, int width, boolean rightAlign) {
1.108 - String[] array = new String[(s.length() - 1) / width + 1];
1.109 - for (int i = 0; i < array.length; i++) {
1.110 - if (i == array.length - 1) {
1.111 - String part = s.substring(i * width, s.length());
1.112 - array[i] = rightAlign ? lpad(part, width) : rpad(part, width);
1.113 - } else {
1.114 - array[i] = s.substring(i * width, (i + 1) * width);
1.115 - }
1.116 - }
1.117 - return array;
1.118 - }
1.119 -}