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

/**
 * 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, version 3 of the License.
 *
 * 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 <http://www.gnu.org/licenses/>.
 */
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;

/**
 * Longer values are line-wrapped – the cell then contains multiple lines. Marks are added to
 * signalize forced line ends (not present in original data).
 *
 * @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<String[]> currentRow;

	public TabularWrappingFormatter(FormatterContext formatterContext) {
		super(formatterContext);
	}

	@Override
	public void writeStartResultSet(ColumnsHeader header) {
		super.writeStartResultSet(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(wrapLines(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) {
					printValueWithWhitespaceReplaced(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[] wrapLines(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;
	}
}