Tables - Sam36502/SimpleTUI GitHub Wiki
SimpleTUI provides a convenient way of displaying large amounts of data with ease. The Table class lets you take lots of information and display it in a table with simple ASCII-art borders.
In order to organise the data given, tables are broken down into columns. Each
column has a title that will be displayed in the header row of the table. Columns
are generic which means you can store any object or data type in them as long as
they have a toString()
method. The columns will automatically format anything they output
so that the whole column is an equal width for its entire length. You can also choose
how the data should be aligned.
To create a new column, you need an array of data and the correct data type to go with it.
Example:
Integer[] data = {1, 2, 3, 4};
Column<Integer> col = new Column("Column Title", data, Column.alignType.RIGHT);
The above code snippet will create a column with the title "Column Title" and fill it with the provided data. It will also align all of the data elements to the right side of the column when it is displayed.
You may want to update the data in your table after creating the columns, or
you may want to retrieve some of the data. In the example below, col
is the
same column defined above in 'Creating A Column'.
Example:
// Getting data:
col.get(2); // Returns 3
// Setting data:
col.set(2, 5); // Column data now looks like this: {1, 2, 5, 4}
// Getting formatted data:
col.getFormatted(2); // Returns " 5"
// The title has set the width of the column and the data is aligned with the right side
// Getting the column title:
col.toString(); // Returns the title as-is ("Column Title")
col.getFormattedTitle(); // Returns the title formatted ("Column Title")
// No formatting is applied to the title, because it's the longest element
// in the table. The width of each column is set by it's widest element.
To create a table you simply pass an array of columns into the table constructor like so:
Column[] cols =
{
new Column<String>("Name", {"Lorem", "Ipsum", "Dolor", "Sit"}, Column.alignType.CENTER),
new Column<Integer>("Number", {1, 2, 3, 4}, Column.alignType.RIGHT),
new Column<Double>("Float", {3.14, 1.41, 0.00009}, Column.alignType.LEFT)
}
Table tbl = new Table(cols);
Then to format and display the table contents, just use printTable()
:
tbl.printTable();
// Output:
+-----+------+-------+
|Name |Number|Float |
+-----+------+-------+
|Lorem| 1|3.14 |
|Ipsum| 2|1.41 |
|Dolor| 3|0.00009|
| Sit | 4| |
+-----+------+-------+