table-layout: Format tabular data as grid or table.

[ bsd3, library, program, text ] [ Propose Tags ]

table-layout is a library for text-based table layout and cell formatting with the following features:

The module Text.Layout.Table contains everything that is required. However, all of the intermediate functionality is also exposed which makes it easy to create something completely new. A small tutorial is provided in the README.md file.


[Skip to Readme]
Versions [faq] 0.1.0.0, 0.2.0.0, 0.3.0.0, 0.4.0.0, 0.4.0.1, 0.5.0.0, 0.5.1.1, 0.5.2.0, 0.6.0.0, 0.6.0.1, 0.7.0.0, 0.8.0.0, 0.8.0.1, 0.8.0.2, 0.8.0.3, 0.8.0.4, 0.8.0.5, 0.9.0.0, 0.9.0.1 (info)
Change log CHANGELOG.md
Dependencies base (>=4.9 && <4.15), data-default-class (>=0.1.1 && <0.2), data-default-instances-base (==0.1.*) [details]
License BSD-3-Clause
Author Moritz Bruder
Maintainer muesli4@gmail.com
Category Text
Home page https://github.com/muesli4/table-layout
Source repo head: git clone git://github.com/muesli4/table-layout.git
Uploaded by muesli4 at 2020-06-14T16:42:11Z
Distributions NixOS:0.9.0.1
Executables table-layout-test-styles
Downloads 7498 total (51 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs available [build log]
Last success reported on 2020-06-14 [all 1 reports]

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

For package maintainers and hackage trustees


Readme for table-layout-0.9.0.1

[back to package description]

table-layout Hackage version

This package can be used to render character-based table layouts which should be displayed with monospace fonts.

Purpose

The focus of this library lies on rendering cells with different styles per column:

  • Columns can be fixed in size or expanding to make content fit.
  • Whenever content has to be cut, it is possible to indicate this with special strings, which are called cut marks.
  • Columns can be positionally aligned as left, right or center.
  • Columns may also be aligned at certain character occurence with respect to the other cells of that column. One such purpose is to display floating point numbers.

Those specifications are then applied to a list of rows. A row is simply a list of a cell. A cell is a type that implements the Cell type class.

Typically cells are rendered as a grid, but it is also possible to render tables with simulated lines, including styling support. Such tables can use optional headers and multiple lines per cell. Multi-line content can be aligned vertically, with respect to the other horizontally adjacent cells, and text can be rendered justified.

Tutorial

Grid layout

Render some text rows as grid:

> putStrLn $ gridString [column expand left def def, column expand right def def]
                        [ ["top left", "top right"]
                        , ["bottom left", "bottom right"]
                        ]

gridString will join cells with a whitespace and rows with a newline character. The result is not spectacular but does look as expected:

top left       top right
bottom left bottom right

There are sensible default values for all column specification types, even for columns. We could have used just def for the first column.

Number columns

Additionally some common types are provided. A particularly useful one is numCol:

> import Numeric
> let toRow d = [showFFloat Nothing d ""]
> mapM_ putStrLn $ gridLines [numCol] $ toRow <$> [1.2, 100.5, 0.037, 5000.00001]

This will display the given numbers as a dot-aligned single column:

   1.2    
 100.5    
   0.037
5000.00001

Improving readability of grids

Big grids are usually not that readable. To improve their readability, two functions are provided:

  • altLines will apply the given function in an alternating pattern. E.g., color every second row grey.
  • checkeredCells will checker cells with 2 different functions.

A good way to use this would be the ansi-terminal package, provided you are using a terminal to output your text. Another way to introduce color into cells is the Formatted type:

> :set -XOverloadedStrings
> let red s = formatted "\ESC[31m" s "\ESC[0m"
> gridString [def, numCol] [["Jim", "1203"], ["Jane", "523"], ["Jack", red "-959000"]]

This way the color can depend on the cell content.

Table layout

For more complex data, grids do not offer as much visibility. Sometimes we want to explicitly display a table, for example, as output in a database application. tableLines and tableString are used to create a table.

putStrLn $ tableString [def , numCol]
                       unicodeRoundS
                       def
                       [ rowG ["Jack", "184.74"]
                       , rowG ["Jane", "162.2"]
                       ]

A row group is a group of rows which are not visually separated from each other. Thus multiple rows form one cell.

In addition we specify the style and an optional header. By default the header is not visible. This will yield the following result:

╭──────┬────────╮
│ Jack │ 184.74 │
├──────┼────────┤
│ Jane │ 162.2  │
╰──────┴────────╯

Table headers

Optionally we can use table headers. titlesH will center titles, whereas fullH allows more control:

putStrLn $ tableString [fixedLeftCol 10, column (fixed 10) center dotAlign def]
                       unicodeS
                       (titlesH ["Text", "Number"])
                       [ rowG ["A very long text", "0.42000000"]
                       , rowG ["Short text", "100200.5"]
                       ]

Headers are always displayed with a different style than the other columns (centered by default). A maximum column width is respected, otherwise a header may acquire additional space.

┌────────────┬────────────┐
│    Text    │   Number   │
╞════════════╪════════════╡
│ A very lo… │   0.42000… │
├────────────┼────────────┤
│ Short text │ …00.5      │
└────────────┴────────────┘

Vertical positioning and justified text

Because a row group consists of multiple lines, we may also want to align the content of cells vertically, especially when we don't know how many lines there will be. The following piece of code will display a left-justified text alongside the length of the text:

let txt = "Lorem ipsum ..." 
in putStrLn $ tableString [fixedLeftCol 50, numCol]
                          asciiS
                          (titlesH ["Text", "Length"])
                          [ colsAllG center [ justifyText 50 txt
                                            , [show $ length txt]
                                            ]
                          ]

colsAllG will merge the given columns into a row group with the given positioning:

+----------------------------------------------------+--------+
|                        Text                        | Length |
+----------------------------------------------------+--------+
| Lorem  ipsum dolor sit amet, consectetur adipisici |        |
| elit,  sed eiusmod  tempor incidunt  ut labore  et |        |
| dolore magna aliqua. Ut enim ad minim veniam, quis |        |
| nostrud   exercitation  ullamco  laboris  nisi  ut |        |
| aliquid  ex ea  commodi consequat.  Quis aute iure |    429 |
| reprehenderit   in  voluptate  velit  esse  cillum |        |
| dolore  eu fugiat  nulla pariatur.  Excepteur sint |        |
| obcaecat cupiditat non proident, sunt in culpa qui |        |
| officia deserunt mollit anim id est laborum.       |        |
+----------------------------------------------------+--------+

Additionally, the positioning can be specified for each column with colsG. For grids colsAsRows and colsAsRowsAll are provided.

Get in contact

  • Report issues and suggestions to the GitHub page.
  • Any kind of feedback is welcome.
  • Contributions are much appreciated. Contact me first for bigger changes.