TabularWrappingFormatter – first version v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Fri, 03 Jan 2014 21:36:00 +0100
branchv_0
changeset 123248a98c13ca4
parent 122 0c284726a77d
child 124 9277b02a3b02
TabularWrappingFormatter – first version
java/sql-dk/src/info/globalcode/sql/dk/configuration/Configuration.java
java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java
java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularWrappingFormatter.java
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/configuration/Configuration.java	Fri Jan 03 00:27:16 2014 +0100
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/configuration/Configuration.java	Fri Jan 03 21:36:00 2014 +0100
     1.3 @@ -23,6 +23,7 @@
     1.4  import info.globalcode.sql.dk.formatting.SingleValueFormatter;
     1.5  import info.globalcode.sql.dk.formatting.TabularFormatter;
     1.6  import info.globalcode.sql.dk.formatting.TabularPrefetchingFormatter;
     1.7 +import info.globalcode.sql.dk.formatting.TabularWrappingFormatter;
     1.8  import info.globalcode.sql.dk.formatting.XmlFormatter;
     1.9  import java.util.ArrayList;
    1.10  import java.util.Collection;
    1.11 @@ -63,6 +64,7 @@
    1.12  		l.add(new FormatterDefinition(XmlFormatter.NAME, XmlFormatter.class.getName()));
    1.13  		l.add(new FormatterDefinition(TabularFormatter.NAME, TabularFormatter.class.getName()));
    1.14  		l.add(new FormatterDefinition(TabularPrefetchingFormatter.NAME, TabularPrefetchingFormatter.class.getName()));
    1.15 +		l.add(new FormatterDefinition(TabularWrappingFormatter.NAME, TabularWrappingFormatter.class.getName()));
    1.16  		buildInFormatters = Collections.unmodifiableCollection(l);
    1.17  	}
    1.18  
     2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java	Fri Jan 03 00:27:16 2014 +0100
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java	Fri Jan 03 21:36:00 2014 +0100
     2.3 @@ -36,7 +36,7 @@
     2.4  	public static final String PROPERTY_ASCII = "ascii";
     2.5  	public static final String PROPERTY_COLORFUL = "color";
     2.6  	public static final String PROPERTY_TRIM = "trim";
     2.7 -	private ColorfulPrintWriter out;
     2.8 +	protected ColorfulPrintWriter out;
     2.9  	private boolean firstResult = true;
    2.10  	private int[] columnWidth;
    2.11  	/**
    2.12 @@ -139,6 +139,10 @@
    2.13  	@Override
    2.14  	public void writeColumnValue(Object value) {
    2.15  		super.writeColumnValue(value);
    2.16 +		writeColumnValueInternal(value);
    2.17 +	}
    2.18 +
    2.19 +	protected void writeColumnValueInternal(Object value) {
    2.20  
    2.21  		if (isCurrentColumnFirst()) {
    2.22  			printTableIndent();
    2.23 @@ -147,14 +151,8 @@
    2.24  			printTableBorder(" │ ");
    2.25  		}
    2.26  
    2.27 -		String[] valueParts = toString(value).split("\n");
    2.28 -		for (int i = 0; i < valueParts.length; i++) {
    2.29 -			String valuePart = valueParts[i];
    2.30 -			out.print(TerminalColor.Cyan, valuePart);
    2.31 -			if (i < valueParts.length - 1) {
    2.32 -				out.print(TerminalColor.Red, "↲");
    2.33 -			}
    2.34 -		}
    2.35 +		String valueString = toString(value);
    2.36 +		printValueWithNewLinesReplaced(valueString);
    2.37  
    2.38  		if (isCurrentColumnLast()) {
    2.39  			printTableBorder(" │");
    2.40 @@ -162,7 +160,7 @@
    2.41  
    2.42  	}
    2.43  
    2.44 -	private int getColumnWidth(int columnNumber) {
    2.45 +	protected int getColumnWidth(int columnNumber) {
    2.46  		return columnWidth[columnNumber - 1];
    2.47  	}
    2.48  
    2.49 @@ -196,6 +194,10 @@
    2.50  	@Override
    2.51  	public void writeEndRow() {
    2.52  		super.writeEndRow();
    2.53 +		writeEndRowInternal();
    2.54 +	}
    2.55 +
    2.56 +	public void writeEndRowInternal() {
    2.57  		out.println();
    2.58  		out.flush();
    2.59  	}
    2.60 @@ -253,7 +255,7 @@
    2.61  		}
    2.62  	}
    2.63  
    2.64 -	private void printTableBorder(String border) {
    2.65 +	protected void printTableBorder(String border) {
    2.66  		if (asciiNostalgia) {
    2.67  			border = border.replaceAll("─", "-");
    2.68  			border = border.replaceAll("│", "|");
    2.69 @@ -263,7 +265,18 @@
    2.70  		out.print(TerminalColor.Green, border);
    2.71  	}
    2.72  
    2.73 -	private void printTableIndent() {
    2.74 +	protected void printTableIndent() {
    2.75  		out.print(" ");
    2.76  	}
    2.77 +
    2.78 +	protected void printValueWithNewLinesReplaced(String valueString) {
    2.79 +		String[] valueParts = valueString.split("\n");
    2.80 +		for (int i = 0; i < valueParts.length; i++) {
    2.81 +			String valuePart = valueParts[i];
    2.82 +			out.print(TerminalColor.Cyan, valuePart);
    2.83 +			if (i < valueParts.length - 1) {
    2.84 +				out.print(TerminalColor.Red, "↲");
    2.85 +			}
    2.86 +		}
    2.87 +	}
    2.88  }
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularWrappingFormatter.java	Fri Jan 03 21:36:00 2014 +0100
     3.3 @@ -0,0 +1,114 @@
     3.4 +/**
     3.5 + * SQL-DK
     3.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, either version 3 of the License, or
    3.11 + * (at your option) any later version.
    3.12 + *
    3.13 + * This program is distributed in the hope that it will be useful,
    3.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    3.16 + * GNU General Public License for more details.
    3.17 + *
    3.18 + * You should have received a copy of the GNU General Public License
    3.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    3.20 + */
    3.21 +package info.globalcode.sql.dk.formatting;
    3.22 +
    3.23 +import info.globalcode.sql.dk.ColorfulPrintWriter.TerminalColor;
    3.24 +import java.util.ArrayList;
    3.25 +import java.util.List;
    3.26 +import static info.globalcode.sql.dk.Functions.lpad;
    3.27 +import static info.globalcode.sql.dk.Functions.rpad;
    3.28 +import static info.globalcode.sql.dk.Functions.repeat;
    3.29 +
    3.30 +/**
    3.31 + *
    3.32 + * @author Ing. František Kučera (frantovo.cz)
    3.33 + */
    3.34 +public class TabularWrappingFormatter extends TabularFormatter {
    3.35 +
    3.36 +	public static final String NAME = "tabular-wrapping"; // bash-completion:formatter
    3.37 +	private List<String[]> currentRow;
    3.38 +
    3.39 +	public TabularWrappingFormatter(FormatterContext formatterContext) {
    3.40 +		super(formatterContext);
    3.41 +	}
    3.42 +
    3.43 +	@Override
    3.44 +	public void writeColumnsHeader(ColumnsHeader header) {
    3.45 +		super.writeColumnsHeader(header);
    3.46 +		currentRow = new ArrayList<>(header.getColumnCount());
    3.47 +	}
    3.48 +
    3.49 +	@Override
    3.50 +	protected void writeColumnValueInternal(Object value) {
    3.51 +		boolean rightAlign = value instanceof Number || value instanceof Boolean;
    3.52 +		String valueString = String.valueOf(value);
    3.53 +		int columnWidth = getColumnWidth(getCurrentColumnsCount()) - 1;  // -1 = space for new line symbol
    3.54 +		currentRow.add(split(valueString, columnWidth, rightAlign));
    3.55 +	}
    3.56 +
    3.57 +	@Override
    3.58 +	public void writeEndRow() {
    3.59 +		super.writeEndRow();
    3.60 +
    3.61 +		int wrappedLine = 0;
    3.62 +		boolean hasMoreWrappedLines;
    3.63 +
    3.64 +		do {
    3.65 +			hasMoreWrappedLines = false;
    3.66 +			for (int i = 0; i < currentRow.size(); i++) {
    3.67 +				if (i == 0) {
    3.68 +					printTableIndent();
    3.69 +					printTableBorder("│ ");
    3.70 +				} else {
    3.71 +					printTableBorder(" │ ");
    3.72 +				}
    3.73 +				String[] columnArray = currentRow.get(i);
    3.74 +				if (wrappedLine < columnArray.length) {
    3.75 +					printValueWithNewLinesReplaced(columnArray[wrappedLine]);
    3.76 +
    3.77 +					if (wrappedLine < columnArray.length - 1) {
    3.78 +						out.print(TerminalColor.Red, "↩");
    3.79 +						hasMoreWrappedLines = true;
    3.80 +					} else {
    3.81 +						out.print(" ");
    3.82 +					}
    3.83 +
    3.84 +				} else {
    3.85 +					out.print(repeat(' ', getColumnWidth(i + 1)));
    3.86 +				}
    3.87 +
    3.88 +				if (i == (currentRow.size() - 1)) {
    3.89 +					printTableBorder(" │");
    3.90 +				}
    3.91 +			}
    3.92 +			out.println();
    3.93 +			out.flush();
    3.94 +			wrappedLine++;
    3.95 +		} while (hasMoreWrappedLines);
    3.96 +
    3.97 +		currentRow.clear();
    3.98 +	}
    3.99 +
   3.100 +	@Override
   3.101 +	public void writeEndRowInternal() {
   3.102 +		// already done – wrapped row ends
   3.103 +	}
   3.104 +
   3.105 +	private static String[] split(String s, int width, boolean rightAlign) {
   3.106 +		String[] array = new String[(s.length() - 1) / width + 1];
   3.107 +		for (int i = 0; i < array.length; i++) {
   3.108 +			if (i == array.length - 1) {
   3.109 +				String part = s.substring(i * width, s.length());
   3.110 +				array[i] = rightAlign ? lpad(part, width) : rpad(part, width);
   3.111 +			} else {
   3.112 +				array[i] = s.substring(i * width, (i + 1) * width);
   3.113 +			}
   3.114 +		}
   3.115 +		return array;
   3.116 +	}
   3.117 +}
   3.118 \ No newline at end of file