t2
package t2
Defines types to create, access, and print tables.
Getting Started
Below is an example of how to use TableBuilder
and TableWriter
. Using
these utilities, it builds a Table
and writes the content to stdout.
// Build table with first row as column header val table = t2.TableBuilder() .add("#", "Effective Date", "Currency Code", "Exchange Rate") .add("1", "2021-01-04", "USD", "0.690236") .add("2", "2021-01-05", "USD", "0.690627") .add("3", "2021-01-06", "USD", "0.689332") .build() // Create table writer with supplied configuration val writer = t2.TableWriter( "ansiColorEnabled" -> "true", "tableBorderColor" -> Console.CYAN, "tableHeaderColor" -> (Console.YELLOW_B ++ Console.BLACK), "columnRightAlign" -> "0,3", // Right align first and last columns "rowHeaderEnabled" -> "true", "rowHeaderColor" -> (Console.BOLD ++ Console.CYAN), "bodySeparatorColor" -> Console.YELLOW ) // Write table to standard output writer.write(System.out, table)
The generated output would look something like the following if printed to a color-enabled terminal.
=================================================== # Effective Date Currency Code Exchange Rate --------------------------------------------------- 1 2021-01-04 USD 0.690236 2 2021-01-05 USD 0.690627 3 2021-01-06 USD 0.689332 ===================================================
The table writer can be reconfigured for changing such things as cell padding, characters used for table borders and row separator, and more.
- Alphabetic
- By Inheritance
- t2
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Type Members
-
trait
Table extends AnyRef
Defines table.
Defines table.
How to Create Table
A
Table
can be created using a factory defined in companion object or incrementally built using aTableBuilder
.The two tables created in the example below are effectively the same.
// Create table with data supplied as Seq[Seq[String]] val table1 = t2.Table( Seq( Seq("#", "Effective Date", "Currency Code", "Exchange Rate"), Seq("1", "2021-01-04", "USD", "0.690236"), Seq("2", "2021-01-05", "USD", "0.690627"), Seq("3", "2021-01-06", "USD", "0.689332") ) ) // Incrementally build table by adding value sequences val table2 = t2.TableBuilder() .add("#", "Effective Date", "Currency Code", "Exchange Rate") .add("1", "2021-01-04", "USD", "0.690236") .add("2", "2021-01-05", "USD", "0.690627") .add("3", "2021-01-06", "USD", "0.689332") .build() // Assert equality assert(table1.rows == table2.rows)
- See also
-
trait
TableBuilder extends AnyRef
Defines table builder.
Defines table builder.
Usage
Below is an example of how to create and use the default
TableBuilder
.// Build table with first row as column header val table = t2.TableBuilder() .add("#", "Effective Date", "Currency Code", "Exchange Rate") .add("1", "2021-01-04", "USD", "0.690236") .add("2", "2021-01-05", "USD", "0.690627") .add("3", "2021-01-06", "USD", "0.689332") .build()
-
trait
TableWriter extends AnyRef
Defines table writer.
Defines table writer.
Usage
Below is an example of how to create, configure, and use the default
TableWriter
.// Build table val table = t2.TableBuilder() .add("#", "Effective Date", "Currency Code", "Exchange Rate") .add("1", "2021-01-04", "USD", "0.690236") .add("2", "2021-01-05", "USD", "0.690627") .add("3", "2021-01-06", "USD", "0.689332") .build() // Create table writer with supplied configuration val writer = t2.TableWriter( "ansiColorEnabled" -> "true", "tableBorderColor" -> Console.CYAN, "tableHeaderColor" -> (Console.YELLOW_B ++ Console.BLACK), "columnRightAlign" -> "0,3", // Right align first and last columns "rowHeaderEnabled" -> "true", "rowHeaderColor" -> (Console.BOLD ++ Console.CYAN), "bodySeparatorColor" -> Console.YELLOW ) // Write table to standard output writer.write(System.out, table)
The generated output would look something like the following if printed to a color-enabled terminal.
=================================================== # Effective Date Currency Code Exchange Rate --------------------------------------------------- 1 2021-01-04 USD 0.690236 2 2021-01-05 USD 0.690627 3 2021-01-06 USD 0.689332 ===================================================
The table writer can be reconfigured for changing such things as cell padding, characters used for table borders and row separator, and more.
Value Members
-
object
Table
Provides
Table
factory. -
object
TableBuilder
Provides
TableBuilder
factory. -
object
TableWriter
Provides
TableWriter
factory.