java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/TabularWrappingFormatter.java
branchv_0
changeset 238 4a1864c3e867
parent 219 3b1733fb3793
child 250 aae5009bd0af
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/java/sql-dk/src/main/java/info/globalcode/sql/dk/formatting/TabularWrappingFormatter.java	Mon Mar 04 20:15:24 2019 +0100
@@ -0,0 +1,116 @@
+/**
+ * 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 <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;
+	}
+}