Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java library for writing a table on the command line

Does anybody know a library that would transform a String[][] into a String table as below?

| title1 | title2 |
|________|________|
| blah   | blih   |
| blah2  | blih2  |
etc...

I could write formatted strings for this, but it will be non-generic code, tied to the parameters. Writing generic code for this purpose would be more expensive than what I'm willing to invest.

So all in all, I think a library would be justified for this.

Any idea if this exists already?

like image 273
matthieus Avatar asked Dec 14 '10 17:12

matthieus


People also ask

Is Java good for CLI?

We've seen that Java isn't just useful for big web applications - it works just fine for short-running CLI applications too, made easier with jbang and picocli. This was just an example though - Twilio already has a very useful CLI already which can send SMS and do a lot more besides.


2 Answers

According to daniel_or_else in this thread:

Using j-text-utils you may print to console a table like:

_____________________________________________________________
| Item Num | Description           | Rating |  Cost | Price |
=============================================================
| 8675309  | Tommy Tutone Album    |      3 |  5.99 | 10.99 |
| 5619452  | Led Zeppelin Box Set  |      5 | 10.42 | 24.95 |
| 9154732  | Justin Bieber Box Set |     -1 |  0.00 |  0.00 |

And it as simple as:

TextTable tt = new TextTable(columnNames, data);
tt.printTable();
like image 121
devBadger Avatar answered Nov 15 '22 10:11

devBadger


I am not aware of any library that does this, but take a look at this SO question:

Java: Print a 2D String array as a right-justified table

It shows you how you can create a generic method which dynamically-generates format strings for each column.

like image 26
dogbane Avatar answered Nov 15 '22 12:11

dogbane