TabularFormatter: basic column padding v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Mon, 23 Dec 2013 12:16:22 +0100
branchv_0
changeset 39be8db46a38c3
parent 38 ff5bbc06ed29
child 40 a9db7fb3ce65
TabularFormatter: basic column padding
java/sql-dk/src/info/globalcode/sql/dk/formatting/AbstractFormatter.java
java/sql-dk/src/info/globalcode/sql/dk/formatting/ColumnDescriptor.java
java/sql-dk/src/info/globalcode/sql/dk/formatting/ColumnsHeader.java
java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java
     1.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/AbstractFormatter.java	Mon Dec 23 11:52:49 2013 +0100
     1.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/AbstractFormatter.java	Mon Dec 23 12:16:22 2013 +0100
     1.3 @@ -202,6 +202,17 @@
     1.4  		}
     1.5  	}
     1.6  
     1.7 +	/**
     1.8 +	 * @param value from ResultSet
     1.9 +	 * @return String representation of given value
    1.10 +	 */
    1.11 +	protected String toString(Object value) {
    1.12 +		/**
    1.13 +		 * TODO: basic formatting: boolean, null, date, numbers…
    1.14 +		 */
    1.15 +		return String.valueOf(value);
    1.16 +	}
    1.17 +
    1.18  	@Override
    1.19  	public void writeStartUpdatesResult() {
    1.20  		pushState(State.UPDATES_RESULT, EnumSet.of(State.DATABASE));
     2.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/ColumnDescriptor.java	Mon Dec 23 11:52:49 2013 +0100
     2.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/ColumnDescriptor.java	Mon Dec 23 12:16:22 2013 +0100
     2.3 @@ -29,6 +29,7 @@
     2.4  	private String typeName;
     2.5  	private boolean firstColumn;
     2.6  	private boolean lastColumn;
     2.7 +	private int columnNumber;
     2.8  
     2.9  	/**
    2.10  	 * @return column name
    2.11 @@ -84,4 +85,15 @@
    2.12  	public void setLastColumn(boolean lastColumn) {
    2.13  		this.lastColumn = lastColumn;
    2.14  	}
    2.15 +
    2.16 +	/**
    2.17 +	 * @return number of this column, 1 = first
    2.18 +	 */
    2.19 +	public int getColumnNumber() {
    2.20 +		return columnNumber;
    2.21 +	}
    2.22 +
    2.23 +	public void setColumnNumber(int columnNumber) {
    2.24 +		this.columnNumber = columnNumber;
    2.25 +	}
    2.26  }
     3.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/ColumnsHeader.java	Mon Dec 23 11:52:49 2013 +0100
     3.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/ColumnsHeader.java	Mon Dec 23 12:16:22 2013 +0100
     3.3 @@ -27,13 +27,13 @@
     3.4   * @author Ing. František Kučera (frantovo.cz)
     3.5   */
     3.6  public class ColumnsHeader {
     3.7 -
     3.8 +	
     3.9  	private ResultSetMetaData metaData;
    3.10 -
    3.11 +	
    3.12  	public ColumnsHeader(ResultSetMetaData metaData) {
    3.13  		this.metaData = metaData;
    3.14  	}
    3.15 -
    3.16 +	
    3.17  	public int getColumnCount() {
    3.18  		try {
    3.19  			return metaData.getColumnCount();
    3.20 @@ -41,16 +41,19 @@
    3.21  			throw new IllegalStateException("Error during getting column count.", e);
    3.22  		}
    3.23  	}
    3.24 -
    3.25 +	
    3.26  	public List<ColumnDescriptor> getColumnDescriptors() {
    3.27  		try {
    3.28  			int count = metaData.getColumnCount();
    3.29  			List<ColumnDescriptor> list = new ArrayList<>(count);
    3.30 -
    3.31 +			
    3.32  			for (int i = 1; i <= count; i++) {
    3.33  				ColumnDescriptor cd = new ColumnDescriptor();
    3.34 +				
    3.35  				cd.setFirstColumn(i == 1);
    3.36  				cd.setLastColumn(i == count);
    3.37 +				cd.setColumnNumber(i);
    3.38 +				
    3.39  				cd.setLabel(metaData.getColumnLabel(i));
    3.40  				cd.setName(metaData.getColumnName(i));
    3.41  				cd.setType(metaData.getColumnType(i));
    3.42 @@ -58,7 +61,7 @@
    3.43  				/** TODO: more properties */
    3.44  				list.add(cd);
    3.45  			}
    3.46 -
    3.47 +			
    3.48  			return list;
    3.49  		} catch (SQLException e) {
    3.50  			throw new IllegalStateException("Error during building column descriptors.", e);
     4.1 --- a/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java	Mon Dec 23 11:52:49 2013 +0100
     4.2 +++ b/java/sql-dk/src/info/globalcode/sql/dk/formatting/TabularFormatter.java	Mon Dec 23 12:16:22 2013 +0100
     4.3 @@ -19,6 +19,8 @@
     4.4  
     4.5  import info.globalcode.sql.dk.ColorfulPrintWriter;
     4.6  import static info.globalcode.sql.dk.ColorfulPrintWriter.*;
     4.7 +import static info.globalcode.sql.dk.Functions.lpad;
     4.8 +import static info.globalcode.sql.dk.Functions.rpad;
     4.9  
    4.10  /**
    4.11   *
    4.12 @@ -27,8 +29,11 @@
    4.13  public class TabularFormatter extends AbstractFormatter {
    4.14  
    4.15  	public static final String NAME = "tabular";
    4.16 +	private static final String HEADER_TYPE_PREFIX = " (";
    4.17 +	private static final String HEADER_TYPE_SUFFIX = ")";
    4.18  	private ColorfulPrintWriter out;
    4.19  	private boolean firstResult = true;
    4.20 +	private int[] columnWidth;
    4.21  
    4.22  	public TabularFormatter(FormatterContext formatterContext) {
    4.23  		super(formatterContext);
    4.24 @@ -45,14 +50,18 @@
    4.25  	public void writeColumnsHeader(ColumnsHeader header) {
    4.26  		super.writeColumnsHeader(header);
    4.27  
    4.28 +		columnWidth = new int[header.getColumnCount()];
    4.29 +
    4.30  		for (ColumnDescriptor cd : header.getColumnDescriptors()) {
    4.31  			out.print(TerminalStyle.Bright, cd.getLabel());
    4.32 -			out.print(" (");
    4.33 +			out.print(HEADER_TYPE_PREFIX);
    4.34  			out.print(cd.getTypeName());
    4.35 -			out.print(")");
    4.36 +			out.print(HEADER_TYPE_SUFFIX);
    4.37  			if (!cd.isLastColumn()) {
    4.38  				out.print(TerminalColor.Green, " | ");
    4.39  			}
    4.40 +
    4.41 +			setColumnWidth(cd.getColumnNumber(), cd.getLabel().length() + cd.getTypeName().length() + HEADER_TYPE_PREFIX.length() + HEADER_TYPE_SUFFIX.length());
    4.42  		}
    4.43  		out.println();
    4.44  		out.flush();
    4.45 @@ -66,7 +75,31 @@
    4.46  			out.print(TerminalColor.Green, " | ");
    4.47  		}
    4.48  
    4.49 -		out.print(TerminalColor.Cyan, String.valueOf(value));
    4.50 +		out.print(TerminalColor.Cyan, toString(value));
    4.51 +	}
    4.52 +
    4.53 +	private int getColumnWidth(int columnNumber) {
    4.54 +		return columnWidth[columnNumber - 1];
    4.55 +	}
    4.56 +
    4.57 +	private void setColumnWidth(int columnNumber, int width) {
    4.58 +		columnWidth[columnNumber - 1] = width;
    4.59 +	}
    4.60 +
    4.61 +	private void updateColumnWidth(int columnNumber, int width) {
    4.62 +		int oldWidth = getColumnWidth(columnNumber);
    4.63 +		setColumnWidth(columnNumber, Math.max(width, oldWidth));
    4.64 +
    4.65 +	}
    4.66 +
    4.67 +	@Override
    4.68 +	protected String toString(Object value) {
    4.69 +		final int width = getColumnWidth(getCurrentColumnsCount());
    4.70 +		if (value instanceof Number) {
    4.71 +			return lpad(super.toString(value), width);
    4.72 +		} else {
    4.73 +			return rpad(super.toString(value), width);
    4.74 +		}
    4.75  	}
    4.76  
    4.77  	@Override