Krow

A small library for generating tables in ASCII or HTML formats, built with Kotlin's type-safe builders

Build Status Latest Version Codacy Project Grade Code Coverage

Building Tables

Tables are built with Kotlin's type-safe builder syntax. You open a krow closure, and are then able to call the various methods and set properties that build your table. A complete example is shown below:

val table = krow {
    cell("col1", "row1") { content = "1-1" }
    cell("col1", "row2") { content = "1-2" }

    cell("col2", "row1") { content = "2-1" }
    cell("col2", "row2") { content = "2-2" }

    cell("col3", "row1") { content = "3-1" }
    cell("col3", "row2") { content = "3-2" }

    table {
        wrapTextAt = 30
        horizontalAlignment = HorizontalAlignment.CENTER
        verticalAlignment = VerticalAlignment.TOP
    }
}

A Table is build of Cells arranged in a 2D grid.

The cell() method accepts a pair of keys for column, row index. If either of these two keys do not exist, then the table will add a row or column accordingly, and then set the properties of that cell with the closure opened at that cell.

The row() method takes a key for a row, and applies the closure to all cells in that row. If the row does not exist, the table will add the row before applying the closure to all its cells. In the same way, you can use the column() to setup an entire column of cells.

the table() method applies the closure to every cell in the entire table.

You can also set the ordering of rows and columns with the columns(), and rows() methods, which each take a list of Strings as the names of the columns or rows to add. Other rows or columns may be added later with the above methods, but they will be added at the end or added in place if the column or row already exists. At this time, it is not possible to reorder columns or rows once they have been created, so if you need a specific order, set it before configuring any individual cells, columns, or rows.

Formatting Tables

Once a Table has been created like so:

val table = krow {
    ...
}

You can then print the Table in any format you wish. Krow ships with Formatters that render the table as an HTML table, or as an ASCII table for the command-line. Examples are shown below:

SingleBorder AsciiTable

┌──────┬──────┬──────┬──────┐
│      │ col1 │ col2 │ col3 │
├──────┼──────┼──────┼──────┤
│ row1 │ 1-1  │ 2-1  │ 3-1  │
├──────┼──────┼──────┼──────┤
│ row2 │ 1-2  │ 2-2  │ 3-2  │
└──────┴──────┴──────┴──────┘

DoubleBorder AsciiTable

╔══════╦══════╦══════╦══════╗
║      ║ col1 ║ col2 ║ col3 ║
╠══════╬══════╬══════╬══════╣
║ row1 ║ 1-1  ║ 2-1  ║ 3-1  ║
╠══════╬══════╬══════╬══════╣
║ row2 ║ 1-2  ║ 2-2  ║ 3-2  ║
╚══════╩══════╩══════╩══════╝

Crossing AsciiTable

+------+------+------+------+
|      | col1 | col2 | col3 |
+------+------+------+------+
| row1 | 1-1  | 2-1  | 3-1  |
+------+------+------+------+
| row2 | 1-2  | 2-2  | 3-2  |
+------+------+------+------+

HtmlTable

<table>
  <thead>
  <tr>
    <th></th>
    <th>col1</th>
    <th>col2</th>
    <th>col3</th>
  </tr>
  </thead>
  <tbody>
  <tr>
    <td>row1</td>
    <td>1-1</td>
    <td>2-1</td>
    <td>3-1</td>
  </tr>
  <tr>
    <td>row2</td>
    <td>1-2</td>
    <td>2-2</td>
    <td>3-2</td>
  </tr>
  </tbody>
</table>

BSD 3-Clause License

Copyright (c) 2018, copper-leaf All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  • Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.