TabularFormatter: support for value trim (if longer than cell size) v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Fri, 27 Dec 2013 21:26:30 +0100
branchv_0
changeset 8703bf24449c7a
parent 86 6b0eb3b22eb8
child 88 102ba0fcb07f
TabularFormatter: support for value trim (if longer than cell size)
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	Fri Dec 27 19:33:46 2013 +0100
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java	Fri Dec 27 21:26:30 2013 +0100
     1.3 @@ -35,7 +35,14 @@
     1.4  	private ColorfulPrintWriter out;
     1.5  	private boolean firstResult = true;
     1.6  	private int[] columnWidth;
     1.7 +	/**
     1.8 +	 * use ASCII borders instead of unicode ones
     1.9 +	 */
    1.10  	private final boolean asciiNostalgia = false;
    1.11 +	/**
    1.12 +	 * Trim values if they are longer than cell size
    1.13 +	 */
    1.14 +	private final boolean trimValues = false;
    1.15  
    1.16  	public TabularFormatter(FormatterContext formatterContext) {
    1.17  		super(formatterContext);
    1.18 @@ -135,12 +142,19 @@
    1.19  	@Override
    1.20  	protected String toString(Object value) {
    1.21  		final int width = getColumnWidth(getCurrentColumnsCount());
    1.22 +		String result;
    1.23  		if (value instanceof Number || value instanceof Boolean) {
    1.24 -			return lpad(super.toString(value), width);
    1.25 +			result = lpad(super.toString(value), width);
    1.26  		} else {
    1.27 -			return rpad(super.toString(value), width);
    1.28 +			result = rpad(super.toString(value), width);
    1.29  		}
    1.30  		// ?	value = (boolean) value ? "✔" : "✗";
    1.31 +
    1.32 +		if (trimValues && result.length() > width) {
    1.33 +			result = result.substring(0, width - 1) + "…";
    1.34 +		}
    1.35 +
    1.36 +		return result;
    1.37  	}
    1.38  
    1.39  	@Override
    1.40 @@ -208,7 +222,7 @@
    1.41  			border = border.replaceAll("│", "|");
    1.42  			border = border.replaceAll("[╭┬╮├┼┤╰┴╯]", "+");
    1.43  		}
    1.44 -		
    1.45 +
    1.46  		out.print(TerminalColor.Green, border);
    1.47  	}
    1.48