1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularWrappingFormatter.java Fri Jan 03 21:36:00 2014 +0100
1.3 @@ -0,0 +1,114 @@
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 + *
1.32 + * @author Ing. František Kučera (frantovo.cz)
1.33 + */
1.34 +public class TabularWrappingFormatter extends TabularFormatter {
1.35 +
1.36 + public static final String NAME = "tabular-wrapping"; // bash-completion:formatter
1.37 + private List<String[]> currentRow;
1.38 +
1.39 + public TabularWrappingFormatter(FormatterContext formatterContext) {
1.40 + super(formatterContext);
1.41 + }
1.42 +
1.43 + @Override
1.44 + public void writeColumnsHeader(ColumnsHeader header) {
1.45 + super.writeColumnsHeader(header);
1.46 + currentRow = new ArrayList<>(header.getColumnCount());
1.47 + }
1.48 +
1.49 + @Override
1.50 + protected void writeColumnValueInternal(Object value) {
1.51 + boolean rightAlign = value instanceof Number || value instanceof Boolean;
1.52 + String valueString = String.valueOf(value);
1.53 + int columnWidth = getColumnWidth(getCurrentColumnsCount()) - 1; // -1 = space for new line symbol
1.54 + currentRow.add(split(valueString, columnWidth, rightAlign));
1.55 + }
1.56 +
1.57 + @Override
1.58 + public void writeEndRow() {
1.59 + super.writeEndRow();
1.60 +
1.61 + int wrappedLine = 0;
1.62 + boolean hasMoreWrappedLines;
1.63 +
1.64 + do {
1.65 + hasMoreWrappedLines = false;
1.66 + for (int i = 0; i < currentRow.size(); i++) {
1.67 + if (i == 0) {
1.68 + printTableIndent();
1.69 + printTableBorder("│ ");
1.70 + } else {
1.71 + printTableBorder(" │ ");
1.72 + }
1.73 + String[] columnArray = currentRow.get(i);
1.74 + if (wrappedLine < columnArray.length) {
1.75 + printValueWithNewLinesReplaced(columnArray[wrappedLine]);
1.76 +
1.77 + if (wrappedLine < columnArray.length - 1) {
1.78 + out.print(TerminalColor.Red, "↩");
1.79 + hasMoreWrappedLines = true;
1.80 + } else {
1.81 + out.print(" ");
1.82 + }
1.83 +
1.84 + } else {
1.85 + out.print(repeat(' ', getColumnWidth(i + 1)));
1.86 + }
1.87 +
1.88 + if (i == (currentRow.size() - 1)) {
1.89 + printTableBorder(" │");
1.90 + }
1.91 + }
1.92 + out.println();
1.93 + out.flush();
1.94 + wrappedLine++;
1.95 + } while (hasMoreWrappedLines);
1.96 +
1.97 + currentRow.clear();
1.98 + }
1.99 +
1.100 + @Override
1.101 + public void writeEndRowInternal() {
1.102 + // already done – wrapped row ends
1.103 + }
1.104 +
1.105 + private static String[] split(String s, int width, boolean rightAlign) {
1.106 + String[] array = new String[(s.length() - 1) / width + 1];
1.107 + for (int i = 0; i < array.length; i++) {
1.108 + if (i == array.length - 1) {
1.109 + String part = s.substring(i * width, s.length());
1.110 + array[i] = rightAlign ? lpad(part, width) : rpad(part, width);
1.111 + } else {
1.112 + array[i] = s.substring(i * width, (i + 1) * width);
1.113 + }
1.114 + }
1.115 + return array;
1.116 + }
1.117 +}
1.118 \ No newline at end of file