Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pretty-print a table in Haskell?

I would like to prettyprint a table-like data structure in Haskell (a list of columns).

For example...

Table [
    StrCol "strings" ["a", "bc", "c"],
    IntCol "ints" [1, 30, -2],
    DblCol "doubles" [2.0, 4.5, -3.2]]

Should render something like...

strings ints doubles
    "a"    1     2.0
   "bc"   30     4.5
    "c"   -2    -3.2

Currently I have implemented this functionality in Text.PrettyPrint that comes with the most recent version of the Haskell Platform. Unfortunately the <+> operator orients multi-line documents "diagonally" from each other.

eg

(text "a" $+$ text "b") <+> (text "c" $+$ text "d")

renders as

a
b c
  d

rather than

a c
b d

As a result, I transpose the cells and merge them horizontally first, then vertically, but this results in the columns not being aligned.

In an older table pretty-printing question, augustss refers to adding some more code to have the columns automatically adapt to the widest entry.

I'm guessing that "sizedText" with the maximum length of each column would do this, except that this function doesn't appear to be part of the Haskell Platform Text.PrettyPrint module (I think it's in the closely related Text.Pretty package).

What's the simplest non-hacky way of implementing this pretty-print functionality? This is a small part of my project so I'd like to avoid writing my own pretty-printing, and I'd prefer to avoid module dependencies if I can.

like image 333
bakuhatsu Avatar asked Apr 24 '12 06:04

bakuhatsu


People also ask

How do you print a table?

To print a table: To select all the cells in the table, select the first cell and scroll to the last cell. If your table has a lot of columns, instead of scrolling, select the first cell, press and hold the Shift key and select the last cell. Select File > Print > Print.

Can we directly print from table?

Can we directly print from table? Select a cell within the table to enable the table. Select File, and then select Print. Under Settings, select the arrow next to Print Active Sheets and select Print Selected Table.


1 Answers

http://hackage.haskell.org/package/boxes

The boxes package

A pretty-printing library for laying out text in two dimensions, using a simple box model....

like image 84
Daniel Wagner Avatar answered Sep 21 '22 17:09

Daniel Wagner