propellor/src/Utility/Table.hs

30 lines
861 B
Haskell
Raw Normal View History

2014-07-06 20:32:05 +00:00
{- text based table generation
-
2014-11-22 02:26:39 +00:00
- Copyright 2014 Joey Hess <id@joeyh.name>
2014-07-06 20:32:05 +00:00
-
- 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 = reverse $ (0:) $ drop 1 $ reverse $
sumrows (map (map length) table)
2014-07-06 20:32:05 +00:00
sumrows [] = repeat 0
sumrows [r] = r
sumrows (r1:r2:rs) = sumrows $ map (uncurry max) (zip r1 r2) : rs