ed851b347befb6a63cb3603ef13e26ff8efbf997
[match/match.git] / program / Formatter.hs
1 module Formatter where
2 import Data.List
3
4 #if __GLASGOW_HASKELL__ <= 606
5 intercalate xs xss = concat (intersperse xs xss)
6 #endif
7
8 padWith :: a -> Int -> [a] -> [a]
9 padWith _ 0 l = l
10 padWith e n [] = replicate n e
11 padWith e (n+1) (h:t) = h:(padWith e n t)
12
13 formatTable :: [[String]] -> String
14 formatTable cells =
15         let columnWidths = map (\col -> maximum $ map length col)
16                 $ transpose cells in
17         intercalate "\n" $
18                 map (\row ->
19                         let rowCells = zipWith (padWith ' ') columnWidths row in
20                         intercalate " " rowCells
21                 ) cells