Coder Social home page Coder Social logo

pydawan / krow Goto Github PK

View Code? Open in Web Editor NEW

This project forked from copper-leaf/krow

0.0 1.0 0.0 115 KB

A small library for generating tables in ASCII or HTML formats

Home Page: https://javaeden.github.io/Krow/

License: MIT License

Kotlin 98.18% Java 1.82%

krow's Introduction

Krow

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

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>

krow's People

Contributors

cjbrooks12 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.