java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java
branchv_0
changeset 123 248a98c13ca4
parent 104 245f1b88a3e6
child 124 9277b02a3b02
equal deleted inserted replaced
122:0c284726a77d 123:248a98c13ca4
    34 	private static final String HEADER_TYPE_PREFIX = " (";
    34 	private static final String HEADER_TYPE_PREFIX = " (";
    35 	private static final String HEADER_TYPE_SUFFIX = ")";
    35 	private static final String HEADER_TYPE_SUFFIX = ")";
    36 	public static final String PROPERTY_ASCII = "ascii";
    36 	public static final String PROPERTY_ASCII = "ascii";
    37 	public static final String PROPERTY_COLORFUL = "color";
    37 	public static final String PROPERTY_COLORFUL = "color";
    38 	public static final String PROPERTY_TRIM = "trim";
    38 	public static final String PROPERTY_TRIM = "trim";
    39 	private ColorfulPrintWriter out;
    39 	protected ColorfulPrintWriter out;
    40 	private boolean firstResult = true;
    40 	private boolean firstResult = true;
    41 	private int[] columnWidth;
    41 	private int[] columnWidth;
    42 	/**
    42 	/**
    43 	 * use ASCII borders instead of unicode ones
    43 	 * use ASCII borders instead of unicode ones
    44 	 */
    44 	 */
   137 	}
   137 	}
   138 
   138 
   139 	@Override
   139 	@Override
   140 	public void writeColumnValue(Object value) {
   140 	public void writeColumnValue(Object value) {
   141 		super.writeColumnValue(value);
   141 		super.writeColumnValue(value);
       
   142 		writeColumnValueInternal(value);
       
   143 	}
       
   144 
       
   145 	protected void writeColumnValueInternal(Object value) {
   142 
   146 
   143 		if (isCurrentColumnFirst()) {
   147 		if (isCurrentColumnFirst()) {
   144 			printTableIndent();
   148 			printTableIndent();
   145 			printTableBorder("│ ");
   149 			printTableBorder("│ ");
   146 		} else {
   150 		} else {
   147 			printTableBorder(" │ ");
   151 			printTableBorder(" │ ");
   148 		}
   152 		}
   149 
   153 
   150 		String[] valueParts = toString(value).split("\n");
   154 		String valueString = toString(value);
   151 		for (int i = 0; i < valueParts.length; i++) {
   155 		printValueWithNewLinesReplaced(valueString);
   152 			String valuePart = valueParts[i];
       
   153 			out.print(TerminalColor.Cyan, valuePart);
       
   154 			if (i < valueParts.length - 1) {
       
   155 				out.print(TerminalColor.Red, "↲");
       
   156 			}
       
   157 		}
       
   158 
   156 
   159 		if (isCurrentColumnLast()) {
   157 		if (isCurrentColumnLast()) {
   160 			printTableBorder(" │");
   158 			printTableBorder(" │");
   161 		}
   159 		}
   162 
   160 
   163 	}
   161 	}
   164 
   162 
   165 	private int getColumnWidth(int columnNumber) {
   163 	protected int getColumnWidth(int columnNumber) {
   166 		return columnWidth[columnNumber - 1];
   164 		return columnWidth[columnNumber - 1];
   167 	}
   165 	}
   168 
   166 
   169 	private void setColumnWidth(int columnNumber, int width) {
   167 	private void setColumnWidth(int columnNumber, int width) {
   170 		columnWidth[columnNumber - 1] = width;
   168 		columnWidth[columnNumber - 1] = width;
   194 	}
   192 	}
   195 
   193 
   196 	@Override
   194 	@Override
   197 	public void writeEndRow() {
   195 	public void writeEndRow() {
   198 		super.writeEndRow();
   196 		super.writeEndRow();
       
   197 		writeEndRowInternal();
       
   198 	}
       
   199 
       
   200 	public void writeEndRowInternal() {
   199 		out.println();
   201 		out.println();
   200 		out.flush();
   202 		out.flush();
   201 	}
   203 	}
   202 
   204 
   203 	@Override
   205 	@Override
   251 		} else {
   253 		} else {
   252 			out.println();
   254 			out.println();
   253 		}
   255 		}
   254 	}
   256 	}
   255 
   257 
   256 	private void printTableBorder(String border) {
   258 	protected void printTableBorder(String border) {
   257 		if (asciiNostalgia) {
   259 		if (asciiNostalgia) {
   258 			border = border.replaceAll("─", "-");
   260 			border = border.replaceAll("─", "-");
   259 			border = border.replaceAll("│", "|");
   261 			border = border.replaceAll("│", "|");
   260 			border = border.replaceAll("[╭┬╮├┼┤╰┴╯]", "+");
   262 			border = border.replaceAll("[╭┬╮├┼┤╰┴╯]", "+");
   261 		}
   263 		}
   262 
   264 
   263 		out.print(TerminalColor.Green, border);
   265 		out.print(TerminalColor.Green, border);
   264 	}
   266 	}
   265 
   267 
   266 	private void printTableIndent() {
   268 	protected void printTableIndent() {
   267 		out.print(" ");
   269 		out.print(" ");
   268 	}
   270 	}
       
   271 
       
   272 	protected void printValueWithNewLinesReplaced(String valueString) {
       
   273 		String[] valueParts = valueString.split("\n");
       
   274 		for (int i = 0; i < valueParts.length; i++) {
       
   275 			String valuePart = valueParts[i];
       
   276 			out.print(TerminalColor.Cyan, valuePart);
       
   277 			if (i < valueParts.length - 1) {
       
   278 				out.print(TerminalColor.Red, "↲");
       
   279 			}
       
   280 		}
       
   281 	}
   269 }
   282 }