# HG changeset patch # User František Kučera # Date 1388175990 -3600 # Node ID 03bf24449c7a6d60b057a302a8d9a20db857176b # Parent 6b0eb3b22eb883b23d0428b4745d9141ed75a007 TabularFormatter: support for value trim (if longer than cell size) diff -r 6b0eb3b22eb8 -r 03bf24449c7a java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java Fri Dec 27 19:33:46 2013 +0100 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java Fri Dec 27 21:26:30 2013 +0100 @@ -35,7 +35,14 @@ private ColorfulPrintWriter out; private boolean firstResult = true; private int[] columnWidth; + /** + * use ASCII borders instead of unicode ones + */ private final boolean asciiNostalgia = false; + /** + * Trim values if they are longer than cell size + */ + private final boolean trimValues = false; public TabularFormatter(FormatterContext formatterContext) { super(formatterContext); @@ -135,12 +142,19 @@ @Override protected String toString(Object value) { final int width = getColumnWidth(getCurrentColumnsCount()); + String result; if (value instanceof Number || value instanceof Boolean) { - return lpad(super.toString(value), width); + result = lpad(super.toString(value), width); } else { - return rpad(super.toString(value), width); + result = rpad(super.toString(value), width); } // ? value = (boolean) value ? "✔" : "✗"; + + if (trimValues && result.length() > width) { + result = result.substring(0, width - 1) + "…"; + } + + return result; } @Override @@ -208,7 +222,7 @@ border = border.replaceAll("│", "|"); border = border.replaceAll("[╭┬╮├┼┤╰┴╯]", "+"); } - + out.print(TerminalColor.Green, border); }