Well, I’m at it again releasing another open-source project. I work with Hyperion and the Essbase Java API extensively, so it’s not uncommon for me to be doing things with grids and working on a command-line. I frequently write lightweight command-line clients to test things out. I didn’t love the options I was able to find in Java that would help print/format a text-based table. The ones I did find were clunky and hard to use.
So, you guessed it, I rolled my own. Jolo is a small and lightweight Java library with two goals: print out text-based tables, and have a tight and clean, yet flexible API. So let’s say we want to print out names, cities, and states in a three column table. The code looks like this:
public void testPrintTableWithColumns() {
// setup table definition with column names/widths
TableColumnList tcl = new TableColumnList.Builder()
.add("Name", 40)
.add("City", 15)
.add("State", 2)
.build();
// createRandomRows is a helper function in this case but would otherwise
// be your data that is an Iterable<List<Something>>.
Iterable<List<? extends Object>> data = createRandomRows(10, 3);
// create the printer and print the data
TablePrinter tp = new TablePrinter();
tp.outputTable(tcl, data);
}
Having the TablePrinter separate from the concept of a TableColumnList is nice because we can plug-in different TablePrinter implementations if we want to. In the above example I have a helper method creating the data for me (createRandomRows()) but in your program this would be something that implements the Iterable interface and contains a List of something that extends Object. In Java parlance that means you can print anything that’s Iterable<List<? extends Object>> – note that the toString() method will be called, so you can pass anything in. If I get time I’ll enhance the API a bit to provide some convenience functions and syntactic sugar. Given some data, the above code generates this table:
+----------------------------------------+---------------+--+
|Name |City |St|
+----------------------------------------+---------------+--+
|Jason Jones |Seattle |WA|
|Cameron Lackpour |Philadelphia |PA|
|Tim Tow |Huntsville |AL|
+----------------------------------------+---------------+--+
Note that with a simple parameter in the builder we were able to constrain the width of a given column.
The Hyperion Connection
This isn’t ostensibly Hyperion- or Essbase-related (save for the names in my table, hehe), but if you work on the things that I work on then this might be up your alley. The Jolo page has more information including a link to the Github repository. I will likely push this to Maven Central as time permits to make its inclusion in anyone’s projects all the easier. Unlike many of my other projects, this one is not incredibly commented [yet], so that will be coming in the future weeks as time permits. The API is pretty clean though so you shouldn’t have a hard time using it. Licensed under the very business-friendly Apache Software License.