A utility class for generating ASCII/text-based tables
Table of Contents
There are many instances where I've needed a simple lightweight tool for generating tables for tabular data stored in collections of Scala case classes.
- SBT 0.13+
$ git clone git@github.com:ldaniels528/tabular.git
$ sbt eclipse
$ sbt gen-idea
$ sbt clean publish-local
$ sbt test
The Scala code:
@Test
def basicTable() {
val groceryList = Seq(
GroceryItem("Milk", requestedBy = "kids"),
GroceryItem("Eggs", requestedBy = "Mom"),
GroceryItem("Cheese", requestedBy = "Mom"),
GroceryItem("Beer", quantity = 12, requestedBy = "Dad"))
val tabular = new Tabular()
tabular.transform(groceryList) foreach logger.info
}
case class GroceryItem(item: String, quantity: Int = 1, requestedBy: String)
The output:
2014-08-13 19:44:35 INFO TabularTest:35 - + ------------------------------- +
2014-08-13 19:44:35 INFO TabularTest:35 - | item quantity requestedBy |
2014-08-13 19:44:35 INFO TabularTest:35 - + ------------------------------- +
2014-08-13 19:44:35 INFO TabularTest:35 - | Milk 1 kids |
2014-08-13 19:44:35 INFO TabularTest:35 - | Eggs 1 Mom |
2014-08-13 19:44:35 INFO TabularTest:35 - | Cheese 1 Mom |
2014-08-13 19:44:35 INFO TabularTest:35 - | Beer 12 Dad |
2014-08-13 19:44:35 INFO TabularTest:35 - + ------------------------------- +