java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularWrappingFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 20 Jan 2014 23:58:57 +0100
branchv_0
changeset 169 4e131a0b521a
parent 155 eb3676c6929b
child 219 3b1733fb3793
permissions -rw-r--r--
Tabular: replace also TABs with colored symbol (like newlines)
franta-hg@123
     1
/**
franta-hg@123
     2
 * SQL-DK
franta-hg@123
     3
 * Copyright © 2014 František Kučera (frantovo.cz)
franta-hg@123
     4
 *
franta-hg@123
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@123
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@123
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@123
     8
 * (at your option) any later version.
franta-hg@123
     9
 *
franta-hg@123
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@123
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@123
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@123
    13
 * GNU General Public License for more details.
franta-hg@123
    14
 *
franta-hg@123
    15
 * You should have received a copy of the GNU General Public License
franta-hg@123
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@123
    17
 */
franta-hg@123
    18
package info.globalcode.sql.dk.formatting;
franta-hg@123
    19
franta-hg@123
    20
import info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor;
franta-hg@123
    21
import java.util.ArrayList;
franta-hg@123
    22
import java.util.List;
franta-hg@123
    23
import static info.globalcode.sql.dk.Functions.lpad;
franta-hg@123
    24
import static info.globalcode.sql.dk.Functions.rpad;
franta-hg@123
    25
import static info.globalcode.sql.dk.Functions.repeat;
franta-hg@123
    26
franta-hg@123
    27
/**
franta-hg@155
    28
 * Longer values are line-wrapped – the cell then contains multiple lines. Marks are added to
franta-hg@155
    29
 * signalize forced line ends (not present in original data).
franta-hg@123
    30
 *
franta-hg@123
    31
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@123
    32
 */
franta-hg@123
    33
public class TabularWrappingFormatter extends TabularFormatter {
franta-hg@123
    34
franta-hg@123
    35
	public static final String NAME = "tabular-wrapping"; // bash-completion:formatter
franta-hg@123
    36
	private List<String[]> currentRow;
franta-hg@123
    37
franta-hg@123
    38
	public TabularWrappingFormatter(FormatterContext formatterContext) {
franta-hg@123
    39
		super(formatterContext);
franta-hg@123
    40
	}
franta-hg@123
    41
franta-hg@123
    42
	@Override
franta-hg@142
    43
	public void writeStartResultSet(ColumnsHeader header) {
franta-hg@142
    44
		super.writeStartResultSet(header);
franta-hg@123
    45
		currentRow = new ArrayList<>(header.getColumnCount());
franta-hg@123
    46
	}
franta-hg@123
    47
franta-hg@123
    48
	@Override
franta-hg@123
    49
	protected void writeColumnValueInternal(Object value) {
franta-hg@123
    50
		boolean rightAlign = value instanceof Number || value instanceof Boolean;
franta-hg@123
    51
		String valueString = String.valueOf(value);
franta-hg@123
    52
		int columnWidth = getColumnWidth(getCurrentColumnsCount()) - 1;  // -1 = space for new line symbol
franta-hg@142
    53
		currentRow.add(wrapLines(valueString, columnWidth, rightAlign));
franta-hg@123
    54
	}
franta-hg@123
    55
franta-hg@123
    56
	@Override
franta-hg@123
    57
	public void writeEndRow() {
franta-hg@123
    58
		super.writeEndRow();
franta-hg@123
    59
franta-hg@123
    60
		int wrappedLine = 0;
franta-hg@123
    61
		boolean hasMoreWrappedLines;
franta-hg@123
    62
franta-hg@123
    63
		do {
franta-hg@123
    64
			hasMoreWrappedLines = false;
franta-hg@123
    65
			for (int i = 0; i < currentRow.size(); i++) {
franta-hg@123
    66
				if (i == 0) {
franta-hg@123
    67
					printTableIndent();
franta-hg@123
    68
					printTableBorder("│ ");
franta-hg@123
    69
				} else {
franta-hg@123
    70
					printTableBorder(" │ ");
franta-hg@123
    71
				}
franta-hg@123
    72
				String[] columnArray = currentRow.get(i);
franta-hg@123
    73
				if (wrappedLine < columnArray.length) {
franta-hg@169
    74
					printValueWithWhitespaceReplaced(columnArray[wrappedLine]);
franta-hg@123
    75
franta-hg@123
    76
					if (wrappedLine < columnArray.length - 1) {
franta-hg@123
    77
						out.print(TerminalColor.Red, "↩");
franta-hg@123
    78
						hasMoreWrappedLines = true;
franta-hg@123
    79
					} else {
franta-hg@123
    80
						out.print(" ");
franta-hg@123
    81
					}
franta-hg@123
    82
franta-hg@123
    83
				} else {
franta-hg@123
    84
					out.print(repeat(' ', getColumnWidth(i + 1)));
franta-hg@123
    85
				}
franta-hg@123
    86
franta-hg@123
    87
				if (i == (currentRow.size() - 1)) {
franta-hg@123
    88
					printTableBorder(" │");
franta-hg@123
    89
				}
franta-hg@123
    90
			}
franta-hg@123
    91
			out.println();
franta-hg@123
    92
			out.flush();
franta-hg@123
    93
			wrappedLine++;
franta-hg@123
    94
		} while (hasMoreWrappedLines);
franta-hg@123
    95
franta-hg@123
    96
		currentRow.clear();
franta-hg@123
    97
	}
franta-hg@123
    98
franta-hg@123
    99
	@Override
franta-hg@123
   100
	public void writeEndRowInternal() {
franta-hg@123
   101
		// already done – wrapped row ends
franta-hg@123
   102
	}
franta-hg@123
   103
franta-hg@142
   104
	private static String[] wrapLines(String s, int width, boolean rightAlign) {
franta-hg@123
   105
		String[] array = new String[(s.length() - 1) / width + 1];
franta-hg@123
   106
		for (int i = 0; i < array.length; i++) {
franta-hg@123
   107
			if (i == array.length - 1) {
franta-hg@123
   108
				String part = s.substring(i * width, s.length());
franta-hg@123
   109
				array[i] = rightAlign ? lpad(part, width) : rpad(part, width);
franta-hg@123
   110
			} else {
franta-hg@123
   111
				array[i] = s.substring(i * width, (i + 1) * width);
franta-hg@123
   112
			}
franta-hg@123
   113
		}
franta-hg@123
   114
		return array;
franta-hg@123
   115
	}
franta-hg@123
   116
}