{-# LANGUAGE NoOverloadedLists #-} module Symantic.Compta.View where import Data.String (String) import Data.Semigroup (Semigroup(..)) import Prelude (Int, max) import Symantic.Compta.Lang import qualified Data.List as List import Text.Show (Show(..)) --import qualified Symantic.Document as Doc table :: [[String]] -> String table cells = List.unlines (List.map show rows) where maxCols :: Int maxWidths :: [Int] rows :: [[String]] (maxCols, maxWidths, rows) = List.foldr go (0, List.repeat 0, []) cells go :: [String] -> (Int, [Int], [[String]]) -> (Int, [Int], [[String]]) go row (accMaxCols, accMaxWidths, accRows) = ( max accMaxCols (List.length row) , List.zipWith max accMaxWidths (List.map List.length row <> List.repeat 0) , List.take maxCols (List.zipWith align row maxWidths) : accRows ) align :: String -> Int -> String align s n = List.replicate (n - List.length s) ' ' <> s