Skip navigation links
Ascii Table: 0.2.3

Package de.vandermeer.asciitable.v1

Original version of an ASCII (and UTF-8) Table, code is frozen (only bug fixes).

See: Description

Package de.vandermeer.asciitable.v1 Description

Original version of an ASCII (and UTF-8) Table, code is frozen (only bug fixes). The class allows to define a table with n columns. Width set either as overall table width with equally distributed column widths or as fixed width per column. A table can be rendered using a table theme. A set of themes is pre-defined, additional themes can be defined as needed.

Using the ASCII Table V1

First get a new instance of the ASCII Table using one of the two static factory methods: Once the table is created addRow(Object…​) can be used to add rows to it. Finally, call render() render the table into a string. To change the appearance of the rendered table use setTheme() and setPaddingCharacter(). The table theme determines the borders of the table. The default theme is V1_StandardTableThemes.PLAIN_7BIT. A number of themes are provided in V1_StandardTableThemes, including light, double and heavy UTF-8 character boxes and LaTeX style table themes. Individual themes can be created by creating a class implementing the V1_TableTheme interface. The padding character is used to pad content lines in table rows. The default is blank (␣). Themes and padding characters are applied per render call, enabling a table to be rendered multiple times with different options.

A simple example with overall width

A simple example creating a table with 3 rows and three columns. The first row spans 3 columns, each column of the second row fits into a single line, and the second and third column of the third row are broken into two lines:

	V1_AsciiTable at = V1_AsciiTable.newTable(3, 76);
	at.addRow(null, null, "Table Heading");
	at.addRow("first row (col1)", "with some information", "and more information");
	at.addRow("second row (col1)", "with some information (col2)", "and more information (col3)");
	System.out.println(at.render());
 
The output of this example will be:
        +--------------------------------------------------------------------------+
        |                              Table Heading                               |
        +------------------------+------------------------+------------------------+
        |first row (col1)        |with some information   |and more information    |
        +------------------------+------------------------+------------------------+
        |second row (col1)       |with some information   |and more information    |
        |                        |(col2)                  |(col3)                  |
        +------------------------+------------------------+------------------------+

 

An example with fixed columns

Another example, this time using an Integer[] to initialize the table. The table again has 3 rows and 3 columns, but this time column 1 is set to 10 characters, column 2 is set to 15 characters and column 3 is set to 20 characters:

	Integer[] columns = new Integer[]{10, 15, 20};
	V1_AsciiTable at = V1_AsciiTable.newTable(columns);
	at.addRow(null, null, "Table Heading");
	at.addRow("row 1", "this is col 2", "and this is column 3");
	at.addRow("row 2", "some text for column 2", "and some text for column 3");
	System.out.println(at.render());
 
The output of this example will be:
        +-----------------------------------------------+
        |                 Table Heading                 |
        +----------+---------------+--------------------+
        |row 1     |this is col 2  |and this is column 3|
        +----------+---------------+--------------------+
        |row 2     |some text for  |and some text for   |
        |          |column 2       |column 3            |
        +----------+---------------+--------------------+
 

Examples with different table themes

The table theme can be changed any time before rendering the table. Take the example above, we can change the theme to V1_StandardTableThemes.LIGHT and then render the table:

	at.setTheme(V1_StandardTableThemes.LIGHT);
	at.render();
	System.out.println(at.render());
 
The output will now be:
        ┌───────────────────────────────────────────────┐
        │                 Table Heading                 │
        ├──────────┬───────────────┬────────────────────┤
        │row 1     │this is col 2  │and this is column 3│
        ├──────────┼───────────────┼────────────────────┤
        │row 2     │some text for  │and some text for   │
        │          │column 2       │column 3            │
        └──────────┴───────────────┴────────────────────┘
 
If we use the theme V1_StandardTableThemes.DOUBLE, the output should look like this:

	at.setTheme(V1_StandardTableThemes.DOUBLE);
	at.render();
	System.out.println(at.render());
 
The output will now be:
        ╔═══════════════════════════════════════════════╗
        ║                 Table Heading                 ║
        ╠══════════╦═══════════════╦════════════════════╣
        ║row 1     ║this is col 2  ║and this is column 3║
        ╠══════════╬═══════════════╬════════════════════╣
        ║row 2     ║some text for  ║and some text for   ║
        ║          ║column 2       ║column 3            ║
        ╚══════════╩═══════════════╩════════════════════╝
 
If we use the theme V1_StandardTableThemes.LIGHT_DOUBLE, the output should look like this:

	at.setTheme(V1_StandardTableThemes.LIGHT_DOUBLE);
	at.render();
	System.out.println(at.render());
 
The output will now be:
        ╒═══════════════════════════════════════════════╕
        │                 Table Heading                 │
        ╞══════════╤═══════════════╤════════════════════╡
        │row 1     │this is col 2  │and this is column 3│
        ╞══════════╪═══════════════╪════════════════════╡
        │row 2     │some text for  │and some text for   │
        │          │column 2       │column 3            │
        ╘══════════╧═══════════════╧════════════════════╛
 
If we use the theme V1_StandardTableThemes.DOUBLE_LIGHT, the output should look like this:

	at.setTheme(V1_StandardTableThemes.DOUBLE_LIGHT);
	at.render();
	System.out.println(at.render());
 
The output will now be:
        ╓───────────────────────────────────────────────╖
        ║                 Table Heading                 ║
        ╟──────────╥───────────────╥────────────────────╢
        ║row 1     ║this is col 2  ║and this is column 3║
        ╟──────────╫───────────────╫────────────────────╢
        ║row 2     ║some text for  ║and some text for   ║
        ║          ║column 2       ║column 3            ║
        ╙──────────╨───────────────╨────────────────────╜
 
The look and feel of themes with heavy characters can differ, depending on the font that is being used. Many console fonts on windows do not show heavy box drawing characters as monotype or have varying width for whitespaces when using heavy character. The following shows a table using the standard heavy theme (V1_StandardTableThemes.HEAVY):
        ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
        ┃                 Table Heading                 ┃
        ┣━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━┫
        ┃row 1     ┃this is col 2  ┃and this is column 3┃
        ┣━━━━━━━━━━╋━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━┫
        ┃row 2     ┃some text for  ┃and some text for   ┃
        ┃          ┃column 2       ┃column 3            ┃
        ┗━━━━━━━━━━┻━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━┛
 
There are also a number of LaTeX style themes pre-defined. For instance V1_StandardTableThemes.LATEX_LIGHT_TRIPLE_DASH, which unfortunately does not render easily to HTML:
        ┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
                          Table Heading                  
        ┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
         row 1      this is col 2   and this is column 3 
        ┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
         row 2      some text for   and some text for    
                    column 2        column 3             
        ┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
 
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.