TabularFormatter: print colorful tables\! v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Mon, 23 Dec 2013 16:14:03 +0100
branchv_0
changeset 40a9db7fb3ce65
parent 39 be8db46a38c3
child 41 514df5061f59
TabularFormatter: print colorful tables\!
java/sql-dk/src/info/globalcode/sql/dk/ColorfulPrintWriter.java
java/sql-dk/src/info/globalcode/sql/dk/Functions.java
java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/ColorfulPrintWriter.java	Mon Dec 23 12:16:22 2013 +0100
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/ColorfulPrintWriter.java	Mon Dec 23 16:14:03 2013 +0100
     1.3 @@ -92,6 +92,75 @@
     1.4  		return array;
     1.5  	}
     1.6  
     1.7 +	/**
     1.8 +	 * Print (usually audible) bell code (\007, \a, ^G)
     1.9 +	 */
    1.10 +	public void bell() {
    1.11 +		print("\007");
    1.12 +	}
    1.13 +
    1.14 +	/**
    1.15 +	 * Eat the last character
    1.16 +	 */
    1.17 +	public void backspace() {
    1.18 +		print("\b");
    1.19 +	}
    1.20 +
    1.21 +	/**
    1.22 +	 * Eat n last characters
    1.23 +	 *
    1.24 +	 * @param count n
    1.25 +	 */
    1.26 +	public void backspace(int count) {
    1.27 +		for (int i = 0; i < count; i++) {
    1.28 +			backspace();
    1.29 +		}
    1.30 +	}
    1.31 +
    1.32 +	/**
    1.33 +	 * With 100 ms delay and all colors.
    1.34 +	 *
    1.35 +	 * @see #printRainbow(java.lang.String, int,
    1.36 +	 * info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor[])
    1.37 +	 */
    1.38 +	public void printRainbow(String string) {
    1.39 +		printRainbow(string, 100);
    1.40 +	}
    1.41 +
    1.42 +	/**
    1.43 +	 * With all colors.
    1.44 +	 *
    1.45 +	 * @see #printRainbow(java.lang.String, int,
    1.46 +	 * info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor[])
    1.47 +	 */
    1.48 +	public void printRainbow(String string, int delay) {
    1.49 +		printRainbow(string, delay, TerminalColor.values());
    1.50 +	}
    1.51 +
    1.52 +	/**
    1.53 +	 * Prints rainbow text – (re)writes same text subsequently in given colors and then in default
    1.54 +	 * color.
    1.55 +	 *
    1.56 +	 * @param string text to be printed, should not contain \n new line (then rainbow does not work
    1.57 +	 * – use println() after printRainbow() instead)
    1.58 +	 * @param delay delay between rewrites
    1.59 +	 * @param colors list of colors to be used
    1.60 +	 */
    1.61 +	public void printRainbow(String string, int delay, TerminalColor... colors) {
    1.62 +		for (TerminalColor c : colors) {
    1.63 +			print(c, string);
    1.64 +			try {
    1.65 +				Thread.sleep(delay);
    1.66 +			} catch (InterruptedException e) {
    1.67 +				// no time to sleep
    1.68 +				break;
    1.69 +			}
    1.70 +			backspace(string.length());
    1.71 +			flush();
    1.72 +		}
    1.73 +		print(string);
    1.74 +	}
    1.75 +
    1.76  	public void setForegroundColor(TerminalColor color) {
    1.77  		printCodes(color.getForegroundCode());
    1.78  	}
     2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/Functions.java	Mon Dec 23 12:16:22 2013 +0100
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/Functions.java	Mon Dec 23 16:14:03 2013 +0100
     2.3 @@ -24,6 +24,7 @@
     2.4  import java.io.InputStreamReader;
     2.5  import java.io.PrintWriter;
     2.6  import java.util.ArrayList;
     2.7 +import java.util.Arrays;
     2.8  import java.util.Collection;
     2.9  import java.util.Map;
    2.10  
    2.11 @@ -126,4 +127,10 @@
    2.12  	public static String lpad(String s, int n) {
    2.13  		return String.format("%1$" + n + "s", s);
    2.14  	}
    2.15 +
    2.16 +	public static String repeat(char ch, int count) {
    2.17 +		char[] array = new char[count];
    2.18 +		Arrays.fill(array, ch);
    2.19 +		return new String(array);
    2.20 +	}
    2.21  }
     3.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java	Mon Dec 23 12:16:22 2013 +0100
     3.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java	Mon Dec 23 16:14:03 2013 +0100
     3.3 @@ -21,6 +21,7 @@
     3.4  import static info.globalcode.sql.dk.ColorfulPrintWriter.*;
     3.5  import static info.globalcode.sql.dk.Functions.lpad;
     3.6  import static info.globalcode.sql.dk.Functions.rpad;
     3.7 +import static info.globalcode.sql.dk.Functions.repeat;
     3.8  
     3.9  /**
    3.10   *
    3.11 @@ -52,18 +53,48 @@
    3.12  
    3.13  		columnWidth = new int[header.getColumnCount()];
    3.14  
    3.15 +		printTableIndent();
    3.16 +		printTableBorder("╭");
    3.17  		for (ColumnDescriptor cd : header.getColumnDescriptors()) {
    3.18 +			setColumnWidth(cd.getColumnNumber(), cd.getLabel().length() + cd.getTypeName().length() + HEADER_TYPE_PREFIX.length() + HEADER_TYPE_SUFFIX.length());
    3.19 +			if (!cd.isFirstColumn()) {
    3.20 +				printTableBorder("┬");
    3.21 +			}
    3.22 +			printTableBorder(repeat('─', getColumnWidth(cd.getColumnNumber()) + 2));
    3.23 +		}
    3.24 +		printTableBorder("╮");
    3.25 +		out.println();
    3.26 +
    3.27 +		for (ColumnDescriptor cd : header.getColumnDescriptors()) {
    3.28 +			if (cd.isFirstColumn()) {
    3.29 +				printTableIndent();
    3.30 +				printTableBorder("│ ");
    3.31 +			} else {
    3.32 +				printTableBorder(" │ ");
    3.33 +			}
    3.34  			out.print(TerminalStyle.Bright, cd.getLabel());
    3.35  			out.print(HEADER_TYPE_PREFIX);
    3.36  			out.print(cd.getTypeName());
    3.37  			out.print(HEADER_TYPE_SUFFIX);
    3.38 -			if (!cd.isLastColumn()) {
    3.39 -				out.print(TerminalColor.Green, " | ");
    3.40 +			if (cd.isLastColumn()) {
    3.41 +				printTableBorder(" │");
    3.42  			}
    3.43  
    3.44  			setColumnWidth(cd.getColumnNumber(), cd.getLabel().length() + cd.getTypeName().length() + HEADER_TYPE_PREFIX.length() + HEADER_TYPE_SUFFIX.length());
    3.45  		}
    3.46  		out.println();
    3.47 +
    3.48 +		printTableIndent();
    3.49 +		printTableBorder("├");
    3.50 +		for (int i = 1; i <= header.getColumnCount(); i++) {
    3.51 +			if (i > 1) {
    3.52 +				printTableBorder("┼");
    3.53 +			}
    3.54 +			printTableBorder(repeat('─', getColumnWidth(i) + 2));
    3.55 +		}
    3.56 +		printTableBorder("┤");
    3.57 +		out.println();
    3.58 +
    3.59  		out.flush();
    3.60  	}
    3.61  
    3.62 @@ -71,11 +102,19 @@
    3.63  	public void writeColumnValue(Object value) {
    3.64  		super.writeColumnValue(value);
    3.65  
    3.66 -		if (!isCurrentColumnFirst()) {
    3.67 -			out.print(TerminalColor.Green, " | ");
    3.68 +		if (isCurrentColumnFirst()) {
    3.69 +			printTableIndent();
    3.70 +			printTableBorder("│ ");
    3.71 +		} else {
    3.72 +			printTableBorder(" │ ");
    3.73  		}
    3.74  
    3.75  		out.print(TerminalColor.Cyan, toString(value));
    3.76 +
    3.77 +		if (isCurrentColumnLast()) {
    3.78 +			printTableBorder(" │");
    3.79 +		}
    3.80 +
    3.81  	}
    3.82  
    3.83  	private int getColumnWidth(int columnNumber) {
    3.84 @@ -100,6 +139,7 @@
    3.85  		} else {
    3.86  			return rpad(super.toString(value), width);
    3.87  		}
    3.88 +		// ?	value = (boolean) value ? "✔" : "✗";
    3.89  	}
    3.90  
    3.91  	@Override
    3.92 @@ -111,9 +151,24 @@
    3.93  
    3.94  	@Override
    3.95  	public void writeEndResultSet() {
    3.96 +		int columnCount = getCurrentColumnsHeader().getColumnCount();
    3.97  		super.writeEndResultSet();
    3.98 +
    3.99 +		printTableIndent();
   3.100 +		printTableBorder("╰");
   3.101 +		for (int i = 1; i <= columnCount; i++) {
   3.102 +			if (i > 1) {
   3.103 +				printTableBorder("┴");
   3.104 +			}
   3.105 +			printTableBorder(repeat('─', getColumnWidth(i) + 2));
   3.106 +		}
   3.107 +		printTableBorder("╯");
   3.108 +		out.println();
   3.109 +
   3.110 +
   3.111  		out.print(TerminalColor.Yellow, "Record count: ");
   3.112  		out.println(getCurrentRowCount());
   3.113 +		out.bell();
   3.114  		out.flush();
   3.115  	}
   3.116  
   3.117 @@ -128,6 +183,7 @@
   3.118  		super.writeUpdatedRowsCount(updatedRowsCount);
   3.119  		out.print(TerminalColor.Red, "Updated records: ");
   3.120  		out.println(updatedRowsCount);
   3.121 +		out.bell();
   3.122  		out.flush();
   3.123  	}
   3.124  
   3.125 @@ -144,4 +200,12 @@
   3.126  			out.println();
   3.127  		}
   3.128  	}
   3.129 +
   3.130 +	private void printTableBorder(String border) {
   3.131 +		out.print(TerminalColor.Green, border);
   3.132 +	}
   3.133 +
   3.134 +	private void printTableIndent() {
   3.135 +		out.print(" ");
   3.136 +	}
   3.137  }