Tabular: replace also TABs with colored symbol (like newlines) v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Mon, 20 Jan 2014 23:58:57 +0100
branchv_0
changeset 169 4e131a0b521a
parent 168 0e8108da0305
child 170 8f142472270c
Tabular: replace also TABs with colored symbol (like newlines)
java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java
java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularWrappingFormatter.java
--- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java	Mon Jan 20 00:05:23 2014 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java	Mon Jan 20 23:58:57 2014 +0100
@@ -23,7 +23,8 @@
 import static info.globalcode.sql.dk.Functions.rpad;
 import static info.globalcode.sql.dk.Functions.repeat;
 import java.util.List;
-import java.util.Scanner;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
  * <p>Prints human-readable output – tables of result sets and text messages with update counts.</p>
@@ -43,6 +44,7 @@
 	public static final String PROPERTY_ASCII = "ascii";
 	public static final String PROPERTY_COLORFUL = "color";
 	public static final String PROPERTY_TRIM = "trim";
+	private static final Pattern whitespaceToReplace = Pattern.compile("\\n|\\t");
 	protected ColorfulPrintWriter out;
 	private boolean firstResult = true;
 	private int[] columnWidth;
@@ -154,7 +156,7 @@
 		}
 
 		String valueString = toString(value);
-		printValueWithNewLinesReplaced(valueString);
+		printValueWithWhitespaceReplaced(valueString);
 
 		if (isCurrentColumnLast()) {
 			printTableBorder(" │");
@@ -266,19 +268,29 @@
 		out.print(" ");
 	}
 
-	protected void printValueWithNewLinesReplaced(String valueString) {
-		String[] valueParts = valueString.split("\n");
-		for (int i = 0; i < valueParts.length; i++) {
-			String valuePart = valueParts[i];
-			// TODO: replace also TABs
-			out.print(TerminalColor.Cyan, valuePart);
-			if (i < valueParts.length - 1) {
-				out.print(TerminalColor.Red, "↲");
+	protected void printValueWithWhitespaceReplaced(String valueString) {
+
+		Matcher m = whitespaceToReplace.matcher(valueString);
+
+		int start = 0;
+		while (m.find(start)) {
+
+			out.print(TerminalColor.Cyan, valueString.substring(start, m.start()));
+
+			switch (m.group()) {
+				case "\n":
+					out.print(TerminalColor.Red, "↲");
+					break;
+				case "\t":
+					out.print(TerminalColor.Red, "↹");
+					break;
+				default:
+					throw new IllegalStateException("Unexpected whitespace token: „" + m.group() + "“");
 			}
+
+			start = m.end();
 		}
 
-		if (valueString.endsWith("\n")) {
-			out.print(TerminalColor.Red, "↲");
-		}
+		out.print(TerminalColor.Cyan, valueString.substring(start, valueString.length()));
 	}
 }
--- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularWrappingFormatter.java	Mon Jan 20 00:05:23 2014 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularWrappingFormatter.java	Mon Jan 20 23:58:57 2014 +0100
@@ -71,7 +71,7 @@
 				}
 				String[] columnArray = currentRow.get(i);
 				if (wrappedLine < columnArray.length) {
-					printValueWithNewLinesReplaced(columnArray[wrappedLine]);
+					printValueWithWhitespaceReplaced(columnArray[wrappedLine]);
 
 					if (wrappedLine < columnArray.length - 1) {
 						out.print(TerminalColor.Red, "↩");