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 1694e131a0b521a
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
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java	Mon Jan 20 00:05:23 2014 +0100
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java	Mon Jan 20 23:58:57 2014 +0100
     1.3 @@ -23,7 +23,8 @@
     1.4  import static info.globalcode.sql.dk.Functions.rpad;
     1.5  import static info.globalcode.sql.dk.Functions.repeat;
     1.6  import java.util.List;
     1.7 -import java.util.Scanner;
     1.8 +import java.util.regex.Matcher;
     1.9 +import java.util.regex.Pattern;
    1.10  
    1.11  /**
    1.12   * <p>Prints human-readable output – tables of result sets and text messages with update counts.</p>
    1.13 @@ -43,6 +44,7 @@
    1.14  	public static final String PROPERTY_ASCII = "ascii";
    1.15  	public static final String PROPERTY_COLORFUL = "color";
    1.16  	public static final String PROPERTY_TRIM = "trim";
    1.17 +	private static final Pattern whitespaceToReplace = Pattern.compile("\\n|\\t");
    1.18  	protected ColorfulPrintWriter out;
    1.19  	private boolean firstResult = true;
    1.20  	private int[] columnWidth;
    1.21 @@ -154,7 +156,7 @@
    1.22  		}
    1.23  
    1.24  		String valueString = toString(value);
    1.25 -		printValueWithNewLinesReplaced(valueString);
    1.26 +		printValueWithWhitespaceReplaced(valueString);
    1.27  
    1.28  		if (isCurrentColumnLast()) {
    1.29  			printTableBorder(" │");
    1.30 @@ -266,19 +268,29 @@
    1.31  		out.print(" ");
    1.32  	}
    1.33  
    1.34 -	protected void printValueWithNewLinesReplaced(String valueString) {
    1.35 -		String[] valueParts = valueString.split("\n");
    1.36 -		for (int i = 0; i < valueParts.length; i++) {
    1.37 -			String valuePart = valueParts[i];
    1.38 -			// TODO: replace also TABs
    1.39 -			out.print(TerminalColor.Cyan, valuePart);
    1.40 -			if (i < valueParts.length - 1) {
    1.41 -				out.print(TerminalColor.Red, "↲");
    1.42 +	protected void printValueWithWhitespaceReplaced(String valueString) {
    1.43 +
    1.44 +		Matcher m = whitespaceToReplace.matcher(valueString);
    1.45 +
    1.46 +		int start = 0;
    1.47 +		while (m.find(start)) {
    1.48 +
    1.49 +			out.print(TerminalColor.Cyan, valueString.substring(start, m.start()));
    1.50 +
    1.51 +			switch (m.group()) {
    1.52 +				case "\n":
    1.53 +					out.print(TerminalColor.Red, "↲");
    1.54 +					break;
    1.55 +				case "\t":
    1.56 +					out.print(TerminalColor.Red, "↹");
    1.57 +					break;
    1.58 +				default:
    1.59 +					throw new IllegalStateException("Unexpected whitespace token: „" + m.group() + "“");
    1.60  			}
    1.61 +
    1.62 +			start = m.end();
    1.63  		}
    1.64  
    1.65 -		if (valueString.endsWith("\n")) {
    1.66 -			out.print(TerminalColor.Red, "↲");
    1.67 -		}
    1.68 +		out.print(TerminalColor.Cyan, valueString.substring(start, valueString.length()));
    1.69  	}
    1.70  }
     2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularWrappingFormatter.java	Mon Jan 20 00:05:23 2014 +0100
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularWrappingFormatter.java	Mon Jan 20 23:58:57 2014 +0100
     2.3 @@ -71,7 +71,7 @@
     2.4  				}
     2.5  				String[] columnArray = currentRow.get(i);
     2.6  				if (wrappedLine < columnArray.length) {
     2.7 -					printValueWithNewLinesReplaced(columnArray[wrappedLine]);
     2.8 +					printValueWithWhitespaceReplaced(columnArray[wrappedLine]);
     2.9  
    2.10  					if (wrappedLine < columnArray.length - 1) {
    2.11  						out.print(TerminalColor.Red, "↩");