From 5002b5e3915890542286487efb852c16c0ca988d Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Sun, 6 Jul 2014 16:32:05 -0400 Subject: propellor spin --- src/Utility/Table.hs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/Utility/Table.hs (limited to 'src/Utility/Table.hs') diff --git a/src/Utility/Table.hs b/src/Utility/Table.hs new file mode 100644 index 00000000..910038e8 --- /dev/null +++ b/src/Utility/Table.hs @@ -0,0 +1,28 @@ +{- text based table generation + - + - Copyright 2014 Joey Hess + - + - 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 -- cgit v1.3-2-g0d8e