summaryrefslogtreecommitdiff
path: root/src/Utility/Table.hs
blob: 910038e891205850f0719c5f29026972092bb4a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{- text based table generation
 -
 - Copyright 2014 Joey Hess <joey@kitenet.net>
 -
 - License: BSD-2-clause
 -}

module Utility.Table where

type Table = [[String]]

-- | A table with a header that is set off with lines under each
-- header item.
tableWithHeader :: [String] -> [[String]] -> Table
tableWithHeader header rows = header : map linesep header : rows
  where
	linesep = map (const '-')

-- | Formats a table to lines, automatically padding rows to the same size.
formatTable :: Table -> [String]
formatTable table = map (\r -> unwords (map pad (zip r rowsizes))) table
  where
	pad (cell, size) = cell ++ take (size - length cell) padding
	padding = repeat ' '
	rowsizes = sumrows (map (map length) table)
	sumrows [] = repeat 0
	sumrows [r] = r
	sumrows (r1:r2:rs) = sumrows $ map (uncurry max) (zip r1 r2) : rs