table-layout: Layout text as grid or table.

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

`table-layout` is a library for text-based table layout, it provides several functions and types which help in this task from the ground up, although using them is not necessary. It provides the following layout features:

  • Fixed-size and arbitrarily sized columns and limiting versions of those

  • Positional alignment of content in a column

  • Alignment of content within a column at a character occurence

  • Cut marks show that content has been trimmed

  • Fancy tables with optional headers and user styles

  • Justified text layout over multiple rows

A small tutorial is provided in the README.md file.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 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, 0.9.0.2, 0.9.1.0, 1.0.0.0 (info)
Dependencies base (>=4.8 && <4.10), data-default-class (>=0.0 && <0.1), data-default-instances-base (>=0.1 && <0.2) [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 2016-05-25T13:29:24Z
Distributions NixOS:1.0.0.0
Reverse Dependencies 2 direct, 0 indirect [details]
Executables table-layout-test-styles
Downloads 10582 total (75 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2016-11-25 [all 1 reports]

Readme for table-layout-0.6.0.1

[back to package description]

table-layout

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 (these are called cut marks). Columns can be positionally aligned as left, right or center and additionally aligned at certain character occurences, e.g. to display floating point numbers. Those specifications are then applied to a list of rows (currently only String is supported).

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 and text can be rendered justified.

Tutorial

Basic grid layout

Render some text rows as grid:

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

layoutToString 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:

mapM_ putStrLn $ layoutToLines (map ((: []) . show) [1.2, 100.5, 0.037, 5000.00001]) [numCol]

We simply display the given numbers as a dot-aligned single column:

   1.2    
 100.5    
   3.7e-2 
5000.00001

Improving readability of grids

Big grids are usually not that readable, so to improve their readability two functions are provided:

  • altLines will alternate functions applied to lines.
  • 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.

Table layout

Grids are fine, but sometimes we want to explicitly display a table, e.g. as output in a database application. This is where layoutTableToString comes in handy:

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

A row group is a group of rows which form one cell, meaning that each line of a group is not visually seperated from the other ones. The second argument specifies an optional header, the third the column specifications and the style. This will yield the following result:

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

Table headers

The same is possible with headers:

putStrLn $ layoutTableToString [ rowG ["A very long text", "0.42000000"]
                               , rowG ["Short text", "100200.5"]
                               ]
                               (Just (["Title", "Length"], repeat def))
                               [fixedLeftCol 10, column (fixed 10) center dotAlign def]
                               unicodeS

Some fixed length columns are used this time and the header is displayed with a different style (additionally the header column will be specified differently):

┌────────────┬────────────┐
│   Title    │   Length   │
╞════════════╪════════════╡
│ 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 will be there. The following piece of code will display a left-justified text alongside the length of the text:

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

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

+----------------------------------------------------+--------+
|                        Text                        | Length |
+----------------------------------------------------+--------+
| Lorem  ipsum dolor sit amet, consetetur sadipscing |        |
| elitr,  sed  diam nonumy eirmod tempor invidunt ut |        |
| labore  et  dolore  magna  aliquyam erat, sed diam |        |
| voluptua.  At  vero  eos  et  accusam et justo duo |    295 |
| dolores et ea rebum. Stet clita kasd gubergren, no |        |
| sea  takimata  sanctus  est  Lorem ipsum dolor sit |        |
| amet.                                              |        |
+----------------------------------------------------+--------+

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

Suggestions

Feel free to contact me, I'm always happy about some feedback!