pptable: Pretty Print containers in a tabular format

[ library, mit, text ] [ Propose Tags ]

When you are faced with tens of records data types contained in a list or similar structure if becomes difficult to view all records during iterative development. This library provides a generic funciton to print any such record types in a tabular format that makes visualizing the data more pleasing. Please see README.md for examples of this.

[Skip to Readme]




Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Versions [RSS],,,
Change log CHANGELOG.md
Dependencies base (>=4.8 && <5), boxes, containers, generic-deriving, pretty, syb, vector [details]
License MIT
Copyright 2016 Guru Devanla
Author Guru Devanla
Maintainer grdvnl@gmail.com
Revised Revision 1 made by GeorgeWilson at 2018-03-18T23:03:57Z
Category Text
Home page https://github.com/gdevanla/pptable#readme
Source repo head: git clone https://github.com/gdevanla/pptable
Uploaded by gdevanla at 2018-03-17T13:45:47Z
Distributions LTSHaskell:, NixOS:, Stackage:
Reverse Dependencies 2 direct, 0 indirect [details]
Downloads 3239 total (17 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 2018-03-17 [all 1 reports]

Readme for pptable-

[back to package description]


Textme.PrettyPrint.Tabulate : Print any list, vector or map of records as a well-formatted readable table. Nested record structures are printed with hierarchically arranged headers, thus clearly showing the nested structures.


  • This module provides simple functions used to print values in tabular format
  • Version
  • Contributions and Bug Reports welcome. Please use the Github issue tracker.

Release Notes (This release has breaking changes)

  • This release has some breaking changes which was required to extend functionality. The extended functionality improves on previous release by printing nested records with hierarchical column headers

  • All Records types need to update their Tabulate instances to provide a flag, namely ExpandWhenNested or DoNotExpandWhenNested.

  • ppTable function has been renamed to printTable to make the function name more descriptive.

  • New function called printTableWithFlds has been added.


    {-# LANGUAGE MultiParamTypeClasses#-}
    {-# LANGUAGE DeriveGeneric #-}
    {-# LANGUAGE DeriveDataTypeable #-}

    import Text.PrettyPrint.Tabulate
    import qualified GHC.Generics as G
    import Data.Data

    import qualified Text.PrettyPrint.Tabulate as T

    import qualified Data.Map as Map
    import qualified Data.List as List
    import qualified Data.Vector as Vector

Instance Declaration Requirements

  1. All records definitions should derive from Data and Generic

  2. All field types that are record attributes should have an instance of CellValueFormatter. Default instances for standard types are already provided.

  3. All records that need to be printed as a table need to be an instance of Tabulate.

  4. Depending on if the nested record's fields have to be expanded, the Tabulate instance could either be

    instance Tabulate Price ExpandWhenNested or instance Tabulate Price DoNotExpandWhenNested.

    data FxCode = USD | EUR | JPY deriving (Show, Data, G.Generic)
    instance T.CellValueFormatter FxCode

    -- This record type will be nested inside `Stock`
    data Price = Price {price::Double, fx_code::FxCode} deriving (Data, G.Generic, Show)

    -- if we do not want the `Price` records to be expanded into their own fields
    -- then choose `T.DoNotExpandWhenNested`
    instance T.Tabulate Price T.ExpandWhenNested
    instance T.CellValueFormatter Price

    data Stock = Stock {ticker::String, local_price::Price, marketCap::Double} deriving (
        Data, G.Generic, Show)
    instance T.Tabulate Stock T.ExpandWhenNested

Once we have the records and required instances created, we can see how the created records can be viewed in the tabular format.

    yahoo =  Stock {ticker="YHOO", local_price=Price 42.29101010 USD, marketCap=40e9}
    google = Stock {ticker="GOOG", local_price=Price 774.210101 EUR, marketCap=532.09e9}
    amazon = Stock {ticker="AMZN", local_price=Price 799.161717 JPY, marketCap=378.86e9}

    tickers = [yahoo, google, amazon]
    tickers_vector = Vector.fromList tickers
    tickers_map:: Map.Map Integer Stock
    tickers_map = Map.fromList [(10, yahoo), (100, google), (1000, amazon)]

    printExamples:: IO ()
    printExamples = do
        putStrLn "Printing records in a list\n"
        T.printTable tickers

        putStrLn "\nPrinting records in a map with the index.\nNote the `key(s)` are printed as first columns"
        T.printTable tickers_map

        putStrLn "\nPrinting records in a vector\n"
        T.printTable tickers_vector

        -- Sometimes records may have too many fields. In those case, specific fields can
        -- be chosen to be printed. Currently, support for this functionality is
        -- minimal. The 'headers` are not printed. In the future, a function that
        -- can take header labels as a list will be provided.

        putStrLn "\nPrinting specific fields. Note, currently field names are not printed"
        T.printTableWithFlds [T.DFld (price . local_price), T.DFld ticker] tickers_map

        putStrLn "\nPrint nested record in a map, individually"
        T.printTable $ fmap local_price tickers_map

    main:: IO ()
    main = do


    Printing records in a list

    ticker     local_price        local_price     marketCap
    -          price              fx_code         -
    YHOO           42.2910101     USD               4.0000000e10
    GOOG          774.2101010     EUR               5.3209000e11
    AMZN          799.1617170     JPY               3.7886000e11

    Printing records in a map with the index (Note the `key` is printed as the first column)

    -        ticker     local_price        local_price     marketCap
    -        -          price              fx_code         -
    10       YHOO           42.2910101     USD               4.0000000e10
    100      GOOG          774.2101010     EUR               5.3209000e11
    1000     AMZN          799.1617170     JPY               3.7886000e11

    Printing records in a vector

    ticker     local_price        local_price     marketCap
    -          price              fx_code         -
    YHOO           42.2910101     USD               4.0000000e10
    GOOG          774.2101010     EUR               5.3209000e11
    AMZN          799.1617170     JPY               3.7886000e11

    Printing specific fields. Note, currently field names are not printed
    10           42.2910101     YHOO
    100         774.2101010     GOOG
    1000        799.1617170     AMZN