Skip navigation links
Ascii Table: 0.2.3

Package de.vandermeer.asciitable.v2.render

V2 classes to render a table.

See: Description

Package de.vandermeer.asciitable.v2.render Description

V2 classes to render a table.

Most of the classes and enumerates here should be used automatically by a V2 ASCII table. The main external class is the V2_TableRenderer, which is used to render a V2 ASCII table.

The Table Renderer Interface

The V2_TableRenderer interface provides the base line for realizing a table renderer. It defines a number of methods to set the behavior of a renderer:

The ASCII Table Renderer Implementation

The class V2_AsciiTableRenderer implements a standard renderer. It is using the theme V2_E_TableThemes.PLAIN_7BIT as default theme.

Storing render information per row - ProcessedRow

To enable multiple renderer objects processing the same table each renderer stores all information of the rendering process in a new table (list of rows). This new table contains ProcessedRow objects that contain the render-generated information. The original table is not changed (except for adjustment of top and bottom rule row types). So each renderer creates its own view of the original table.

A processed row contains a link to the original row (for access to original content and configuration) plus the generated information for processed columns (wrapped lines, normalized, flipped content array) and border types.

Define a border position

The position of a border element in a table row is defined in BorderPosition as This border position assists a renderer to pick the right character from a row theme.

Define a border type

The type of a border element in a table row is defined in BorderType as This border type assists a renderer to pick the right character from a row theme.

A complete border description

Combining border type and border position describes a complete border. With both characteristics, the renderer can pick the correct border character from a row or table theme. Once the type and the position of the row are determined, border character can be retrieved from table and row themes.

For instance, a border of type BorderPosition.MIDDLE and position BorderType.ALL means to use getMidBorderAll() for a mid-rule row on a V2_RowTheme to get the right character. For the row theme V2_E_RowThemes.UTF_DOUBLE_MID this character will be '╬'.

Similar, a border of type BorderPosition.RIGHT and position BorderType.UP means to use getMidBorderAll() for a bottom-rule row. For the row theme V2_E_RowThemes.UTF_DOUBLE_BOTTOM this character will be '╝'.

Render Utilities

The class RenderUtilities implements a number of utility methods applicable to all (many) renderer implementations:

Table and column width

The calculation of the overall table width and the width of individual columns is out-sourced into objects implementing the V2_Width interface. This allows to define many different possible ways of calculating table/column width, and then hand it over to a renderer to build a rendered table.

Width Interface

The width interface defines the following methods:

Calculate width using an absolute table width and equally distributed column width

WidthAbsoluteEven gets the width of the table and equally distributes the available space of the number of columns given by the renderer. The distribution (characters per column) is the table width minus 1 (last border) minus number of columns (for all other borders). The minimum (ideal) column width is the distribution divided by the number of columns. Potential leftover is the distribution minus minimum column width multiplied by number of columns:

	int distribute = this.width-1-this.colNumber;
	int colmin = distribute/this.colNumber;
	int leftover = distribute-colmin*this.colNumber;
 
The calculation of the column width then is to give each column the minimum width and for as long as there are leftover characters one additional character.

	for(int i=0; i<this.colNumber; i++){
		ret[i] = colmin;
		if(leftover!=0){
			ret[i] += 1;
			leftover -= 1;
		}
	}
 
Here are a few examples of calculated column width for tables, all with a padding of 0:

Calculate width with a given, fixed width per column

WidthFixedColumns allows to add fixed column width, one by one. The only calculation it does is adding the given width for the overall table width.

Calculate width using the longest word in a column with no further restrictions

WidthLongestWord calculates the widths of columns taking the longest word (continuous characters between whitespaces) without further restrictions. Padding is automatically added, so the padding adds to the column width. The overall table width is then the sum of all calculated column width.

The following example creates a table with two rows and two columns each and no padding:

	V2_AsciiTable at = new V2_AsciiTable(0);
	at.addRow("first", "information");
	at.addRow("second", "info");
 
Then we use this table to calculate the width using the longest word:

	V2_Width width = new WidthLongestWord();
	int[]  = width.getColumnWidths(at);
 

The column width calculated are 6 for the first column (longest word: second) and 11 for the second column (longest word: information). If the table is created with the default padding of one, the values will change to 8 and 13.

The following example create a table with two rows and two columns each, and it applies different padding to individual columns.

	V2_AsciiTable at = new V2_AsciiTable(0);
	at.addRow("first", "information").setPadding(new int[]{2, 3});
	at.addRow("second", "info").setPadding(new int[]{3, 4});
 

The calculated width for the columns are now as follows: 12 for the first column (longest word: second plus padding of 3 twice) and 17 for the second column (longest word: information plus padding of 3 twice).

Calculate width using the longest word in a column with minimum column width

WidthLongestWordMinCol calculates the widths of columns taking the longest word (continuous characters between whitespaces) but with a minimum column width. Padding is automatically added, so the padding adds to the column width. The overall table width is then the sum of all calculated column width.

The following example creates a table with two rows and two columns each and standard padding (1):

	V2_AsciiTable at = new V2_AsciiTable();
	at.addRow("first", "information");
	at.addRow("second", "info");
 
Then we use this table to calculate the width using the longest word and minimum column width of 11:

	V2_Width width = new WidthLongestWordMinCol(11);
	int[]  = width.getColumnWidths(at);
 

The calculated width for the columns are now as follows: 11 for the first column (longest word: second plus padding of 1 twice = 8, but minimum column width is set to 11) and 13 for the second column (longest word: information plus padding of 1 twice).

We can also define the minimum column width per column. Here, a value of -1 indicates to simply use the longest word and values above 3 (the absolute minimum colmn width) will be taken as minimum width for a particular column:

	V2_Width width = new WidthLongestWordMinCol(new int[]{-1,50});
	int[]  = width.getColumnWidths(at);
 

The calculated width for the columns are now as follows: 8 for the first column (longest word: second plus padding of 1 twice, no minimum width given) and 50 for the second column (longest word: information plus padding of 1 twice but minimum width set to 50).

Calculate width using the longest word in a column with maximum column width

WidthLongestWordMaxCol calculates the widths of columns taking the longest word (continuous characters between whitespaces) but with a maximum column width. Padding is automatically added, so the padding adds to the column width. The overall table width is then the sum of all calculated column width.

The following example creates a table with two rows and two columns each and standard padding (1):

	V2_AsciiTable at = new V2_AsciiTable();
	at.addRow("first", "information");
	at.addRow("second", "info");
 
Then we use this table to calculate the width using the longest word and maximum column width of 10:

	V2_Width width = new WidthLongestWordMaxCol(10)
	int[]  = width.getColumnWidths(at);
 

The calculated width for the columns are now as follows: 8 for the first column (longest word: second plus padding of 1 twice = 8) and 10 for the second column (longest word: information plus padding of 1 twice = 11, but maximum width set to 10).

We can also define the maximum column width per column. Here, a value of -1 indicates to simply use the longest word and other values will be taken as maximum width for a particular column:

	V2_Width width = new WidthLongestWordMaxCol(new int[]{5,-1});
	int[]  = width.getColumnWidths(at);
 

The calculated width for the columns are now as follows: 5 for the first column (longest word: second plus padding of 1 twice, but maximum set to 5) and 13 for the second column (longest word: information plus padding of 1 twice, no maximum given).

Version:
v0.2.3 build 150910 (10-Sep-15) for Java 1.7
Author:
Sven van der Meer <vdmeer.sven@mykolab.com>
Skip navigation links
Ascii Table: 0.2.3

Copyright © 2010–2015. All rights reserved.