java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java
branchv_0
changeset 169 4e131a0b521a
parent 167 84aaa91642bf
child 170 8f142472270c
equal deleted inserted replaced
168:0e8108da0305 169:4e131a0b521a
    21 import static info.globalcode.sql.dk.ColorfulPrintWriter.*;
    21 import static info.globalcode.sql.dk.ColorfulPrintWriter.*;
    22 import static info.globalcode.sql.dk.Functions.lpad;
    22 import static info.globalcode.sql.dk.Functions.lpad;
    23 import static info.globalcode.sql.dk.Functions.rpad;
    23 import static info.globalcode.sql.dk.Functions.rpad;
    24 import static info.globalcode.sql.dk.Functions.repeat;
    24 import static info.globalcode.sql.dk.Functions.repeat;
    25 import java.util.List;
    25 import java.util.List;
    26 import java.util.Scanner;
    26 import java.util.regex.Matcher;
       
    27 import java.util.regex.Pattern;
    27 
    28 
    28 /**
    29 /**
    29  * <p>Prints human-readable output – tables of result sets and text messages with update counts.</p>
    30  * <p>Prints human-readable output – tables of result sets and text messages with update counts.</p>
    30  *
    31  *
    31  * <p>Longer values might break the table – overflow the cells – see alternative tabular formatters
    32  * <p>Longer values might break the table – overflow the cells – see alternative tabular formatters
    41 	private static final String HEADER_TYPE_PREFIX = " (";
    42 	private static final String HEADER_TYPE_PREFIX = " (";
    42 	private static final String HEADER_TYPE_SUFFIX = ")";
    43 	private static final String HEADER_TYPE_SUFFIX = ")";
    43 	public static final String PROPERTY_ASCII = "ascii";
    44 	public static final String PROPERTY_ASCII = "ascii";
    44 	public static final String PROPERTY_COLORFUL = "color";
    45 	public static final String PROPERTY_COLORFUL = "color";
    45 	public static final String PROPERTY_TRIM = "trim";
    46 	public static final String PROPERTY_TRIM = "trim";
       
    47 	private static final Pattern whitespaceToReplace = Pattern.compile("\\n|\\t");
    46 	protected ColorfulPrintWriter out;
    48 	protected ColorfulPrintWriter out;
    47 	private boolean firstResult = true;
    49 	private boolean firstResult = true;
    48 	private int[] columnWidth;
    50 	private int[] columnWidth;
    49 	/**
    51 	/**
    50 	 * use ASCII borders instead of unicode ones
    52 	 * use ASCII borders instead of unicode ones
   152 		} else {
   154 		} else {
   153 			printTableBorder(" │ ");
   155 			printTableBorder(" │ ");
   154 		}
   156 		}
   155 
   157 
   156 		String valueString = toString(value);
   158 		String valueString = toString(value);
   157 		printValueWithNewLinesReplaced(valueString);
   159 		printValueWithWhitespaceReplaced(valueString);
   158 
   160 
   159 		if (isCurrentColumnLast()) {
   161 		if (isCurrentColumnLast()) {
   160 			printTableBorder(" │");
   162 			printTableBorder(" │");
   161 		}
   163 		}
   162 
   164 
   264 
   266 
   265 	protected void printTableIndent() {
   267 	protected void printTableIndent() {
   266 		out.print(" ");
   268 		out.print(" ");
   267 	}
   269 	}
   268 
   270 
   269 	protected void printValueWithNewLinesReplaced(String valueString) {
   271 	protected void printValueWithWhitespaceReplaced(String valueString) {
   270 		String[] valueParts = valueString.split("\n");
   272 
   271 		for (int i = 0; i < valueParts.length; i++) {
   273 		Matcher m = whitespaceToReplace.matcher(valueString);
   272 			String valuePart = valueParts[i];
   274 
   273 			// TODO: replace also TABs
   275 		int start = 0;
   274 			out.print(TerminalColor.Cyan, valuePart);
   276 		while (m.find(start)) {
   275 			if (i < valueParts.length - 1) {
   277 
   276 				out.print(TerminalColor.Red, "↲");
   278 			out.print(TerminalColor.Cyan, valueString.substring(start, m.start()));
   277 			}
   279 
   278 		}
   280 			switch (m.group()) {
   279 
   281 				case "\n":
   280 		if (valueString.endsWith("\n")) {
   282 					out.print(TerminalColor.Red, "↲");
   281 			out.print(TerminalColor.Red, "↲");
   283 					break;
   282 		}
   284 				case "\t":
       
   285 					out.print(TerminalColor.Red, "↹");
       
   286 					break;
       
   287 				default:
       
   288 					throw new IllegalStateException("Unexpected whitespace token: „" + m.group() + "“");
       
   289 			}
       
   290 
       
   291 			start = m.end();
       
   292 		}
       
   293 
       
   294 		out.print(TerminalColor.Cyan, valueString.substring(start, valueString.length()));
   283 	}
   295 	}
   284 }
   296 }