headerTypes: new option to hide column types in tabular headers v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 13 Sep 2015 19:25:36 +0200
branchv_0
changeset 227 0094319a274a
parent 226 b40153eb7716
child 228 3787c999d12c
headerTypes: new option to hide column types in tabular headers
java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java
--- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java	Sun Sep 06 21:48:54 2015 +0200
+++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java	Sun Sep 13 19:25:36 2015 +0200
@@ -45,6 +45,7 @@
 @PropertyDeclaration(name = COLORFUL, defaultValue = "true", type = Boolean.class, description = COLORFUL_DESCRIPTION)
 @PropertyDeclaration(name = TabularFormatter.PROPERTY_ASCII, defaultValue = "false", type = Boolean.class, description = "whether to use ASCII table borders instead of unicode ones")
 @PropertyDeclaration(name = TabularFormatter.PROPERTY_TRIM, defaultValue = "false", type = Boolean.class, description = "whether to trim the values to fit the column width")
+@PropertyDeclaration(name = TabularFormatter.PROPERTY_HEADER_TYPE, defaultValue = "true", type = Boolean.class, description = "whether to print data types in column headers")
 public class TabularFormatter extends AbstractFormatter {
 
 	public static final String NAME = "tabular"; // bash-completion:formatter
@@ -52,6 +53,7 @@
 	private static final String HEADER_TYPE_SUFFIX = ")";
 	public static final String PROPERTY_ASCII = "ascii";
 	public static final String PROPERTY_TRIM = "trim";
+	public static final String PROPERTY_HEADER_TYPE = "headerTypes";
 	protected ColorfulPrintWriter out;
 	private boolean firstResult = true;
 	private int[] columnWidth;
@@ -63,12 +65,17 @@
 	 * Trim values if they are longer than cell size
 	 */
 	private final boolean trimValues;
+	/**
+	 * Print data type of each column in the header
+	 */
+	private final boolean printHeaderTypes;
 
 	public TabularFormatter(FormatterContext formatterContext) {
 		super(formatterContext);
 		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
 		asciiNostalgia = formatterContext.getProperties().getBoolean(PROPERTY_ASCII, false);
 		trimValues = formatterContext.getProperties().getBoolean(PROPERTY_TRIM, false);
+		printHeaderTypes = formatterContext.getProperties().getBoolean(PROPERTY_HEADER_TYPE, true);
 		out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, true));
 	}
 
@@ -86,7 +93,7 @@
 
 		for (ColumnDescriptor cd : columnDescriptors) {
 			// padding: make header cell at least same width as data cells in this column
-			int typeWidth = cd.getTypeName().length() + HEADER_TYPE_PREFIX.length() + HEADER_TYPE_SUFFIX.length();
+			int typeWidth = printHeaderTypes ? cd.getTypeName().length() + HEADER_TYPE_PREFIX.length() + HEADER_TYPE_SUFFIX.length() : 0;
 			cd.setLabel(rpad(cd.getLabel(), getColumnWidth(cd.getColumnNumber()) - typeWidth));
 			updateColumnWidth(cd.getColumnNumber(), cd.getLabel().length() + typeWidth);
 
@@ -106,9 +113,11 @@
 				printTableBorder(" │ ");
 			}
 			out.print(TerminalStyle.Bright, cd.getLabel());
-			out.print(HEADER_TYPE_PREFIX);
-			out.print(cd.getTypeName());
-			out.print(HEADER_TYPE_SUFFIX);
+			if (printHeaderTypes) {
+				out.print(HEADER_TYPE_PREFIX);
+				out.print(cd.getTypeName());
+				out.print(HEADER_TYPE_SUFFIX);
+			}
 			if (cd.isLastColumn()) {
 				printTableBorder(" │");
 			}