java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java
branchv_0
changeset 87 03bf24449c7a
parent 79 e19a13ed19a9
child 88 102ba0fcb07f
equal deleted inserted replaced
86:6b0eb3b22eb8 87:03bf24449c7a
    33 	private static final String HEADER_TYPE_PREFIX = " (";
    33 	private static final String HEADER_TYPE_PREFIX = " (";
    34 	private static final String HEADER_TYPE_SUFFIX = ")";
    34 	private static final String HEADER_TYPE_SUFFIX = ")";
    35 	private ColorfulPrintWriter out;
    35 	private ColorfulPrintWriter out;
    36 	private boolean firstResult = true;
    36 	private boolean firstResult = true;
    37 	private int[] columnWidth;
    37 	private int[] columnWidth;
       
    38 	/**
       
    39 	 * use ASCII borders instead of unicode ones
       
    40 	 */
    38 	private final boolean asciiNostalgia = false;
    41 	private final boolean asciiNostalgia = false;
       
    42 	/**
       
    43 	 * Trim values if they are longer than cell size
       
    44 	 */
       
    45 	private final boolean trimValues = false;
    39 
    46 
    40 	public TabularFormatter(FormatterContext formatterContext) {
    47 	public TabularFormatter(FormatterContext formatterContext) {
    41 		super(formatterContext);
    48 		super(formatterContext);
    42 		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
    49 		out = new ColorfulPrintWriter(formatterContext.getOutputStream());
    43 	}
    50 	}
   133 	}
   140 	}
   134 
   141 
   135 	@Override
   142 	@Override
   136 	protected String toString(Object value) {
   143 	protected String toString(Object value) {
   137 		final int width = getColumnWidth(getCurrentColumnsCount());
   144 		final int width = getColumnWidth(getCurrentColumnsCount());
       
   145 		String result;
   138 		if (value instanceof Number || value instanceof Boolean) {
   146 		if (value instanceof Number || value instanceof Boolean) {
   139 			return lpad(super.toString(value), width);
   147 			result = lpad(super.toString(value), width);
   140 		} else {
   148 		} else {
   141 			return rpad(super.toString(value), width);
   149 			result = rpad(super.toString(value), width);
   142 		}
   150 		}
   143 		// ?	value = (boolean) value ? "✔" : "✗";
   151 		// ?	value = (boolean) value ? "✔" : "✗";
       
   152 
       
   153 		if (trimValues && result.length() > width) {
       
   154 			result = result.substring(0, width - 1) + "…";
       
   155 		}
       
   156 
       
   157 		return result;
   144 	}
   158 	}
   145 
   159 
   146 	@Override
   160 	@Override
   147 	public void writeEndRow() {
   161 	public void writeEndRow() {
   148 		super.writeEndRow();
   162 		super.writeEndRow();
   206 		if (asciiNostalgia) {
   220 		if (asciiNostalgia) {
   207 			border = border.replaceAll("─", "-");
   221 			border = border.replaceAll("─", "-");
   208 			border = border.replaceAll("│", "|");
   222 			border = border.replaceAll("│", "|");
   209 			border = border.replaceAll("[╭┬╮├┼┤╰┴╯]", "+");
   223 			border = border.replaceAll("[╭┬╮├┼┤╰┴╯]", "+");
   210 		}
   224 		}
   211 		
   225 
   212 		out.print(TerminalColor.Green, border);
   226 		out.print(TerminalColor.Green, border);
   213 	}
   227 	}
   214 
   228 
   215 	private void printTableIndent() {
   229 	private void printTableIndent() {
   216 		out.print(" ");
   230 		out.print(" ");