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