]> Git — Sourcephile - haskell/symantic-compta.git/blob - src/Symantic/Compta/View.hs
init
[haskell/symantic-compta.git] / src / Symantic / Compta / View.hs
1 {-# LANGUAGE NoOverloadedLists #-}
2 module Symantic.Compta.View where
3
4 import Data.String (String)
5 import Data.Semigroup (Semigroup(..))
6 import Prelude (Int, max)
7 import Symantic.Compta.Lang
8 import qualified Data.List as List
9 import Text.Show (Show(..))
10 --import qualified Symantic.Document as Doc
11
12 table :: [[String]] -> String
13 table cells = List.unlines (List.map show rows)
14 where
15 maxCols :: Int
16 maxWidths :: [Int]
17 rows :: [[String]]
18 (maxCols, maxWidths, rows) = List.foldr go (0, List.repeat 0, []) cells
19
20 go :: [String] -> (Int, [Int], [[String]]) -> (Int, [Int], [[String]])
21 go row (accMaxCols, accMaxWidths, accRows) =
22 ( max accMaxCols (List.length row)
23 , List.zipWith max accMaxWidths (List.map List.length row <> List.repeat 0)
24 , List.take maxCols (List.zipWith align row maxWidths) : accRows
25 )
26
27 align :: String -> Int -> String
28 align s n = List.replicate (n - List.length s) ' ' <> s