SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 15 Aug 2015 16:12:06 +0200
branchv_0
changeset 2188e38caf43ca8
parent 217 84bab99dda50
child 219 3b1733fb3793
SingleRecordFormatter: escape whitespace characters in the same way as in TabularFormatter
java/sql-dk/src/info/globalcode/sql/dk/Functions.java
java/sql-dk/src/info/globalcode/sql/dk/formatting/SingleRecordFormatter.java
java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/Functions.java	Sat Aug 15 14:40:48 2015 +0200
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/Functions.java	Sat Aug 15 16:12:06 2015 +0200
     1.3 @@ -24,14 +24,14 @@
     1.4  import java.io.InputStream;
     1.5  import java.io.InputStreamReader;
     1.6  import java.io.PrintWriter;
     1.7 -import java.util.ArrayDeque;
     1.8  import java.util.ArrayList;
     1.9  import java.util.Arrays;
    1.10  import java.util.Collection;
    1.11  import java.util.Collections;
    1.12 -import java.util.Deque;
    1.13  import java.util.List;
    1.14  import java.util.Map;
    1.15 +import java.util.regex.Matcher;
    1.16 +import java.util.regex.Pattern;
    1.17  
    1.18  /**
    1.19   *
    1.20 @@ -39,6 +39,9 @@
    1.21   */
    1.22  public class Functions {
    1.23  
    1.24 +	private static final String NBSP = " ";
    1.25 +	private static final Pattern WHITESPACE_TO_REPLACE = Pattern.compile("\\n|\\r|\\t|" + NBSP);
    1.26 +
    1.27  	private Functions() {
    1.28  	}
    1.29  
    1.30 @@ -185,4 +188,53 @@
    1.31  
    1.32  		return hierarchy;
    1.33  	}
    1.34 +
    1.35 +	/**
    1.36 +	 * TODO: support background or styles and move to ColorfulPrintWriter
    1.37 +	 *
    1.38 +	 * @param out
    1.39 +	 * @param valueString
    1.40 +	 * @param basicColor
    1.41 +	 * @param escapeColor
    1.42 +	 */
    1.43 +	public static void printValueWithWhitespaceReplaced(ColorfulPrintWriter out, String valueString, ColorfulPrintWriter.TerminalColor basicColor, ColorfulPrintWriter.TerminalColor escapeColor) {
    1.44 +
    1.45 +		Matcher m = WHITESPACE_TO_REPLACE.matcher(valueString);
    1.46 +
    1.47 +		int start = 0;
    1.48 +
    1.49 +		while (m.find(start)) {
    1.50 +
    1.51 +			printColorOrNot(out, basicColor, valueString.substring(start, m.start()));
    1.52 +
    1.53 +			switch (m.group()) {
    1.54 +				case "\n":
    1.55 +					out.print(escapeColor, "↲");
    1.56 +					break;
    1.57 +				case "\r":
    1.58 +					out.print(escapeColor, "⏎");
    1.59 +					break;
    1.60 +				case "\t":
    1.61 +					out.print(escapeColor, "↹");
    1.62 +					break;
    1.63 +				case NBSP:
    1.64 +					out.print(escapeColor, "⎵");
    1.65 +					break;
    1.66 +				default:
    1.67 +					throw new IllegalStateException("Unexpected whitespace token: „" + m.group() + "“");
    1.68 +			}
    1.69 +
    1.70 +			start = m.end();
    1.71 +		}
    1.72 +
    1.73 +		printColorOrNot(out, basicColor, valueString.substring(start, valueString.length()));
    1.74 +	}
    1.75 +
    1.76 +	private static void printColorOrNot(ColorfulPrintWriter out, ColorfulPrintWriter.TerminalColor color, String text) {
    1.77 +		if (color == null) {
    1.78 +			out.print(text);
    1.79 +		} else {
    1.80 +			out.print(color, text);
    1.81 +		}
    1.82 +	}
    1.83  }
     2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/SingleRecordFormatter.java	Sat Aug 15 14:40:48 2015 +0200
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/SingleRecordFormatter.java	Sat Aug 15 16:12:06 2015 +0200
     2.3 @@ -18,6 +18,7 @@
     2.4  package info.globalcode.sql.dk.formatting;
     2.5  
     2.6  import info.globalcode.sql.dk.ColorfulPrintWriter;
     2.7 +import info.globalcode.sql.dk.Functions;
     2.8  import info.globalcode.sql.dk.configuration.PropertyDeclaration;
     2.9  import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL;
    2.10  import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL_DESCRIPTION;
    2.11 @@ -61,10 +62,14 @@
    2.12  		super.writeColumnValue(value);
    2.13  		String columnName = getCurrentColumnsHeader().getColumnDescriptors().get(getCurrentColumnsCount() - 1).getLabel();
    2.14  		out.print(ColorfulPrintWriter.TerminalColor.Green, columnName + ": ");
    2.15 -		out.print(String.valueOf(value));
    2.16 +		Functions.printValueWithWhitespaceReplaced(out, toString(value), null, ColorfulPrintWriter.TerminalColor.Red);
    2.17  		println();
    2.18  	}
    2.19  
    2.20 +	private static String toString(Object value) {
    2.21 +		return String.valueOf(value);
    2.22 +	}
    2.23 +
    2.24  	@Override
    2.25  	public void writeUpdatesResult(int updatedRowsCount) {
    2.26  		super.writeUpdatesResult(updatedRowsCount);
     3.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java	Sat Aug 15 14:40:48 2015 +0200
     3.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java	Sat Aug 15 16:12:06 2015 +0200
     3.3 @@ -19,6 +19,7 @@
     3.4  
     3.5  import info.globalcode.sql.dk.ColorfulPrintWriter;
     3.6  import static info.globalcode.sql.dk.ColorfulPrintWriter.*;
     3.7 +import info.globalcode.sql.dk.Functions;
     3.8  import static info.globalcode.sql.dk.Functions.lpad;
     3.9  import static info.globalcode.sql.dk.Functions.rpad;
    3.10  import static info.globalcode.sql.dk.Functions.repeat;
    3.11 @@ -26,8 +27,6 @@
    3.12  import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL;
    3.13  import static info.globalcode.sql.dk.formatting.CommonProperties.COLORFUL_DESCRIPTION;
    3.14  import java.util.List;
    3.15 -import java.util.regex.Matcher;
    3.16 -import java.util.regex.Pattern;
    3.17  
    3.18  /**
    3.19   * <p>
    3.20 @@ -53,8 +52,6 @@
    3.21  	private static final String HEADER_TYPE_SUFFIX = ")";
    3.22  	public static final String PROPERTY_ASCII = "ascii";
    3.23  	public static final String PROPERTY_TRIM = "trim";
    3.24 -	private static final String NBSP = " ";
    3.25 -	private static final Pattern whitespaceToReplace = Pattern.compile("\\n|\\r|\\t|" + NBSP);
    3.26  	protected ColorfulPrintWriter out;
    3.27  	private boolean firstResult = true;
    3.28  	private int[] columnWidth;
    3.29 @@ -165,8 +162,7 @@
    3.30  			printTableBorder(" │ ");
    3.31  		}
    3.32  
    3.33 -		String valueString = toString(value);
    3.34 -		printValueWithWhitespaceReplaced(valueString);
    3.35 +		Functions.printValueWithWhitespaceReplaced(out, toString(value), TerminalColor.Cyan, TerminalColor.Red);
    3.36  
    3.37  		if (isCurrentColumnLast()) {
    3.38  			printTableBorder(" │");
    3.39 @@ -277,36 +273,4 @@
    3.40  	protected void printTableIndent() {
    3.41  		out.print(" ");
    3.42  	}
    3.43 -
    3.44 -	protected void printValueWithWhitespaceReplaced(String valueString) {
    3.45 -
    3.46 -		Matcher m = whitespaceToReplace.matcher(valueString);
    3.47 -
    3.48 -		int start = 0;
    3.49 -		while (m.find(start)) {
    3.50 -
    3.51 -			out.print(TerminalColor.Cyan, valueString.substring(start, m.start()));
    3.52 -
    3.53 -			switch (m.group()) {
    3.54 -				case "\n":
    3.55 -					out.print(TerminalColor.Red, "↲");
    3.56 -					break;
    3.57 -				case "\r":
    3.58 -					out.print(TerminalColor.Red, "⏎");
    3.59 -					break;
    3.60 -				case "\t":
    3.61 -					out.print(TerminalColor.Red, "↹");
    3.62 -					break;
    3.63 -				case NBSP:
    3.64 -					out.print(TerminalColor.Red, "⎵");
    3.65 -					break;
    3.66 -				default:
    3.67 -					throw new IllegalStateException("Unexpected whitespace token: „" + m.group() + "“");
    3.68 -			}
    3.69 -
    3.70 -			start = m.end();
    3.71 -		}
    3.72 -
    3.73 -		out.print(TerminalColor.Cyan, valueString.substring(start, valueString.length()));
    3.74 -	}
    3.75  }