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