module Formatter where import Data.List #if __GLASGOW_HASKELL__ <= 606 intercalate xs xss = concat (intersperse xs xss) #endif padWith :: a -> Int -> [a] -> [a] padWith _ 0 l = l padWith e n [] = replicate n e -- http://hackage.haskell.org/trac/haskell-prime/wiki/RemoveNPlusK -- Ugg... GHC could at least give a meaningful error message. ~ Matt 2011-08-27 padWith e n (h:t) = if n == 0 then error "padWith: list is already longer than the requested length" else h:(padWith e (n-1) t) formatTable :: [[String]] -> String formatTable cells = let columnWidths = map (\col -> maximum $ map length col) $ transpose cells in intercalate "\n" $ map (\row -> let rowCells = zipWith (padWith ' ') columnWidths row in intercalate " " rowCells ) cells