Formatter: Rewrite code that used an n + k pattern
[match/match.git] / program / Formatter.hs
CommitLineData
967c39ef
MM
1module Formatter where
2import Data.List
3
7b8c0e4e
MM
4#if __GLASGOW_HASKELL__ <= 606
5intercalate xs xss = concat (intersperse xs xss)
6#endif
7
967c39ef
MM
8padWith :: a -> Int -> [a] -> [a]
9padWith _ 0 l = l
10padWith e n [] = replicate n e
00819c19
MM
11-- http://hackage.haskell.org/trac/haskell-prime/wiki/RemoveNPlusK
12-- Ugg... GHC could at least give a meaningful error message. ~ Matt 2011-08-27
13padWith e n (h:t) =
14 if n == 0 then error "padWith: list is already longer than the requested length"
15 else h:(padWith e (n-1) t)
967c39ef
MM
16
17formatTable :: [[String]] -> String
18formatTable cells =
19 let columnWidths = map (\col -> maximum $ map length col)
20 $ transpose cells in
21 intercalate "\n" $
22 map (\row ->
23 let rowCells = zipWith (padWith ' ') columnWidths row in
24 intercalate " " rowCells
25 ) cells