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 2270094319a274a
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
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java	Sun Sep 06 21:48:54 2015 +0200
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java	Sun Sep 13 19:25:36 2015 +0200
     1.3 @@ -45,6 +45,7 @@
     1.4  @PropertyDeclaration(name = COLORFUL, defaultValue = "true", type = Boolean.class, description = COLORFUL_DESCRIPTION)
     1.5  @PropertyDeclaration(name = TabularFormatter.PROPERTY_ASCII, defaultValue = "false", type = Boolean.class, description = "whether to use ASCII table borders instead of unicode ones")
     1.6  @PropertyDeclaration(name = TabularFormatter.PROPERTY_TRIM, defaultValue = "false", type = Boolean.class, description = "whether to trim the values to fit the column width")
     1.7 +@PropertyDeclaration(name = TabularFormatter.PROPERTY_HEADER_TYPE, defaultValue = "true", type = Boolean.class, description = "whether to print data types in column headers")
     1.8  public class TabularFormatter extends AbstractFormatter {
     1.9  
    1.10  	public static final String NAME = "tabular"; // bash-completion:formatter
    1.11 @@ -52,6 +53,7 @@
    1.12  	private static final String HEADER_TYPE_SUFFIX = ")";
    1.13  	public static final String PROPERTY_ASCII = "ascii";
    1.14  	public static final String PROPERTY_TRIM = "trim";
    1.15 +	public static final String PROPERTY_HEADER_TYPE = "headerTypes";
    1.16  	protected ColorfulPrintWriter out;
    1.17  	private boolean firstResult = true;
    1.18  	private int[] columnWidth;
    1.19 @@ -63,12 +65,17 @@
    1.20  	 * Trim values if they are longer than cell size
    1.21  	 */
    1.22  	private final boolean trimValues;
    1.23 +	/**
    1.24 +	 * Print data type of each column in the header
    1.25 +	 */
    1.26 +	private final boolean printHeaderTypes;
    1.27  
    1.28  	public TabularFormatter(FormatterContext formatterContext) {
    1.29  		super(formatterContext);
    1.30  		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
    1.31  		asciiNostalgia = formatterContext.getProperties().getBoolean(PROPERTY_ASCII, false);
    1.32  		trimValues = formatterContext.getProperties().getBoolean(PROPERTY_TRIM, false);
    1.33 +		printHeaderTypes = formatterContext.getProperties().getBoolean(PROPERTY_HEADER_TYPE, true);
    1.34  		out.setColorful(formatterContext.getProperties().getBoolean(COLORFUL, true));
    1.35  	}
    1.36  
    1.37 @@ -86,7 +93,7 @@
    1.38  
    1.39  		for (ColumnDescriptor cd : columnDescriptors) {
    1.40  			// padding: make header cell at least same width as data cells in this column
    1.41 -			int typeWidth = cd.getTypeName().length() + HEADER_TYPE_PREFIX.length() + HEADER_TYPE_SUFFIX.length();
    1.42 +			int typeWidth = printHeaderTypes ? cd.getTypeName().length() + HEADER_TYPE_PREFIX.length() + HEADER_TYPE_SUFFIX.length() : 0;
    1.43  			cd.setLabel(rpad(cd.getLabel(), getColumnWidth(cd.getColumnNumber()) - typeWidth));
    1.44  			updateColumnWidth(cd.getColumnNumber(), cd.getLabel().length() + typeWidth);
    1.45  
    1.46 @@ -106,9 +113,11 @@
    1.47  				printTableBorder(" │ ");
    1.48  			}
    1.49  			out.print(TerminalStyle.Bright, cd.getLabel());
    1.50 -			out.print(HEADER_TYPE_PREFIX);
    1.51 -			out.print(cd.getTypeName());
    1.52 -			out.print(HEADER_TYPE_SUFFIX);
    1.53 +			if (printHeaderTypes) {
    1.54 +				out.print(HEADER_TYPE_PREFIX);
    1.55 +				out.print(cd.getTypeName());
    1.56 +				out.print(HEADER_TYPE_SUFFIX);
    1.57 +			}
    1.58  			if (cd.isLastColumn()) {
    1.59  				printTableBorder(" │");
    1.60  			}