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 40 a9db7fb3ce65
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
--- a/java/sql-dk/src/info/globalcode/sql/dk/ColorfulPrintWriter.java	Mon Dec 23 12:16:22 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/ColorfulPrintWriter.java	Mon Dec 23 16:14:03 2013 +0100
@@ -92,6 +92,75 @@
 		return array;
 	}
 
+	/**
+	 * Print (usually audible) bell code (\007, \a, ^G)
+	 */
+	public void bell() {
+		print("\007");
+	}
+
+	/**
+	 * Eat the last character
+	 */
+	public void backspace() {
+		print("\b");
+	}
+
+	/**
+	 * Eat n last characters
+	 *
+	 * @param count n
+	 */
+	public void backspace(int count) {
+		for (int i = 0; i < count; i++) {
+			backspace();
+		}
+	}
+
+	/**
+	 * With 100 ms delay and all colors.
+	 *
+	 * @see #printRainbow(java.lang.String, int,
+	 * info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor[])
+	 */
+	public void printRainbow(String string) {
+		printRainbow(string, 100);
+	}
+
+	/**
+	 * With all colors.
+	 *
+	 * @see #printRainbow(java.lang.String, int,
+	 * info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor[])
+	 */
+	public void printRainbow(String string, int delay) {
+		printRainbow(string, delay, TerminalColor.values());
+	}
+
+	/**
+	 * Prints rainbow text – (re)writes same text subsequently in given colors and then in default
+	 * color.
+	 *
+	 * @param string text to be printed, should not contain \n new line (then rainbow does not work
+	 * – use println() after printRainbow() instead)
+	 * @param delay delay between rewrites
+	 * @param colors list of colors to be used
+	 */
+	public void printRainbow(String string, int delay, TerminalColor... colors) {
+		for (TerminalColor c : colors) {
+			print(c, string);
+			try {
+				Thread.sleep(delay);
+			} catch (InterruptedException e) {
+				// no time to sleep
+				break;
+			}
+			backspace(string.length());
+			flush();
+		}
+		print(string);
+	}
+
 	public void setForegroundColor(TerminalColor color) {
 		printCodes(color.getForegroundCode());
 	}
--- a/java/sql-dk/src/info/globalcode/sql/dk/Functions.java	Mon Dec 23 12:16:22 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/Functions.java	Mon Dec 23 16:14:03 2013 +0100
@@ -24,6 +24,7 @@
 import java.io.InputStreamReader;
 import java.io.PrintWriter;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Map;
 
@@ -126,4 +127,10 @@
 	public static String lpad(String s, int n) {
 		return String.format("%1$" + n + "s", s);
 	}
+
+	public static String repeat(char ch, int count) {
+		char[] array = new char[count];
+		Arrays.fill(array, ch);
+		return new String(array);
+	}
 }
--- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java	Mon Dec 23 12:16:22 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java	Mon Dec 23 16:14:03 2013 +0100
@@ -21,6 +21,7 @@
 import static info.globalcode.sql.dk.ColorfulPrintWriter.*;
 import static info.globalcode.sql.dk.Functions.lpad;
 import static info.globalcode.sql.dk.Functions.rpad;
+import static info.globalcode.sql.dk.Functions.repeat;
 
 /**
  *
@@ -52,18 +53,48 @@
 
 		columnWidth = new int[header.getColumnCount()];
 
+		printTableIndent();
+		printTableBorder("╭");
 		for (ColumnDescriptor cd : header.getColumnDescriptors()) {
+			setColumnWidth(cd.getColumnNumber(), cd.getLabel().length() + cd.getTypeName().length() + HEADER_TYPE_PREFIX.length() + HEADER_TYPE_SUFFIX.length());
+			if (!cd.isFirstColumn()) {
+				printTableBorder("┬");
+			}
+			printTableBorder(repeat('─', getColumnWidth(cd.getColumnNumber()) + 2));
+		}
+		printTableBorder("╮");
+		out.println();
+
+		for (ColumnDescriptor cd : header.getColumnDescriptors()) {
+			if (cd.isFirstColumn()) {
+				printTableIndent();
+				printTableBorder("│ ");
+			} else {
+				printTableBorder(" │ ");
+			}
 			out.print(TerminalStyle.Bright, cd.getLabel());
 			out.print(HEADER_TYPE_PREFIX);
 			out.print(cd.getTypeName());
 			out.print(HEADER_TYPE_SUFFIX);
-			if (!cd.isLastColumn()) {
-				out.print(TerminalColor.Green, " | ");
+			if (cd.isLastColumn()) {
+				printTableBorder(" │");
 			}
 
 			setColumnWidth(cd.getColumnNumber(), cd.getLabel().length() + cd.getTypeName().length() + HEADER_TYPE_PREFIX.length() + HEADER_TYPE_SUFFIX.length());
 		}
 		out.println();
+
+		printTableIndent();
+		printTableBorder("├");
+		for (int i = 1; i <= header.getColumnCount(); i++) {
+			if (i > 1) {
+				printTableBorder("┼");
+			}
+			printTableBorder(repeat('─', getColumnWidth(i) + 2));
+		}
+		printTableBorder("┤");
+		out.println();
+
 		out.flush();
 	}
 
@@ -71,11 +102,19 @@
 	public void writeColumnValue(Object value) {
 		super.writeColumnValue(value);
 
-		if (!isCurrentColumnFirst()) {
-			out.print(TerminalColor.Green, " | ");
+		if (isCurrentColumnFirst()) {
+			printTableIndent();
+			printTableBorder("│ ");
+		} else {
+			printTableBorder(" │ ");
 		}
 
 		out.print(TerminalColor.Cyan, toString(value));
+
+		if (isCurrentColumnLast()) {
+			printTableBorder(" │");
+		}
+
 	}
 
 	private int getColumnWidth(int columnNumber) {
@@ -100,6 +139,7 @@
 		} else {
 			return rpad(super.toString(value), width);
 		}
+		// ?	value = (boolean) value ? "✔" : "✗";
 	}
 
 	@Override
@@ -111,9 +151,24 @@
 
 	@Override
 	public void writeEndResultSet() {
+		int columnCount = getCurrentColumnsHeader().getColumnCount();
 		super.writeEndResultSet();
+
+		printTableIndent();
+		printTableBorder("╰");
+		for (int i = 1; i <= columnCount; i++) {
+			if (i > 1) {
+				printTableBorder("┴");
+			}
+			printTableBorder(repeat('─', getColumnWidth(i) + 2));
+		}
+		printTableBorder("╯");
+		out.println();
+
+
 		out.print(TerminalColor.Yellow, "Record count: ");
 		out.println(getCurrentRowCount());
+		out.bell();
 		out.flush();
 	}
 
@@ -128,6 +183,7 @@
 		super.writeUpdatedRowsCount(updatedRowsCount);
 		out.print(TerminalColor.Red, "Updated records: ");
 		out.println(updatedRowsCount);
+		out.bell();
 		out.flush();
 	}
 
@@ -144,4 +200,12 @@
 			out.println();
 		}
 	}
+
+	private void printTableBorder(String border) {
+		out.print(TerminalColor.Green, border);
+	}
+
+	private void printTableIndent() {
+		out.print(" ");
+	}
 }