Ahnfelt / TextTable

Simple plaintext (ASCII art-like) table formatting. It can wrap tables that are too wide. It is written in Haxe.

Clone this repository (size: 23.4 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/Ahnfelt/texttable

Text table formatting

Because roller coasters are awesome (data from Google Squared).

trace("\n" + new TextTable().format([
    new TextTableColumn("Name", ["Cedar Point", "Six Flags Great Adventure", "Viper"]),
    new TextTableColumn("Location", ["Sandusky, Ohio, USA", "Jackson Township, NJ, USA", "Wien"]),
    new TextTableColumn("Description", ["Celebrating 45 years of thrills at Cedar Point in 2009!", 
        "Great Adventure & Wild Safari, New Jersey. PARK HOURS June 5.", 
        "Viper is the world's first roller coaster capapable of a 360-degree heartline roll."]),
    new TextTableColumn("Capacity", [null, null, 12.8]),
    new TextTableColumn("Speed", [120, 70, 50]),
    new TextTableColumn("Model", ["TX-o32102e4", null, "Viper 3A-1"]),
], 20, 75));

Gives the result (notice the wrapping):

.----------------------.----------------------.----------------------.
| Name                 | Location             | Description          |
|----------------------|----------------------|----------------------|
| Six Flags Great Ad...| Jackson Township, ...| Great Adventure & ...|
| Viper                | Wien                 | Viper is the world...|
'----------------------'----------------------'----------------------'
.----------.-------.------------.
| Capacity | Speed | Model      |
|----------|-------|------------|
|//////////| 70    |////////////|
| 12.8     | 50    | Viper 3A-1 |
'----------'-------'------------'

The two last parameters (20 and 75) are the maximum characters shown from a value and the maximum total table width (in characters). There are two additional arguments, maximum shown rows and row offset, which are not used in this example.

The tables are unambiguous: you can distinguish null from strings like "//////" because there is no margin around the //// that indicate null; and when dots ... indicate that there's more data than can be displayed, there is no margin on the right of the dots, so it can be distinguished from strings like "...".

You can customize borders (and null-character) by passing it to the constructor:

1
new TextTable("/", "=", "-", "=", "[", "|", "]", ".", "=", ".", "[", "-", "]", "'", "=", "'").format(...)

Get it from haxelib install TextTable and then haxe -lib TextTable -x MyClass. Remember to import text.TextTable.


This revision is from 2010-02-13 13:29