module Database.HaskRel.FWTabulation (
present1LineValue, presentNLineValue,
maxLen, padTranspose ) where
import Data.List ( intercalate )
maxLen :: Ord b => [b] -> [b] -> [b]
maxLen = zipWith max
buildOneColumn :: Int -> String -> String -> String
buildOneColumn hPad a str = str ++ a ++ concat ( replicate ( hPad length a ) " " )
buildColumns :: [Int] -> [String] -> String
buildColumns [] [] = ""
buildColumns [p] [t] = buildOneColumn p t ""
buildColumns (p:ps) (t:ts) = buildColumns' (p:ps) (t:ts) ""
buildColumns [] [t] = buildOneColumn 0 t ""
buildColumns [] (t:ts) = buildColumns' [] (t:ts) ""
buildColumns [p] [] = buildOneColumn p "" "Internal Error: "
buildColumns (p:ps) [] = buildColumns' (p:ps) [] ""
buildColumns' :: [Int] -> [String] -> String -> String
buildColumns' [] [] str = str
buildColumns' [p] [t] str = buildOneColumn p t str
buildColumns' (p:ps) (t:ts) str = buildColumns' ps ts $ buildOneColumn p t str ++ " │ "
buildColumns' [] [t] str = str ++ "Internal Error: " ++ t
buildColumns' [] (t:ts) str = str ++ "Internal Error: " ++ intercalate " │ " (t:ts)
buildColumns' [p] [] str = str ++ "Internal Error: " ++ show p
buildColumns' (p:ps) [] str = str ++ "Internal Error: " ++ intercalate " │ " ( map show $ p:ps )
colWidths ::
(Foldable t, Foldable t1) =>
[[t1 a1]] -> [t a] -> [Int]
colWidths l hdr = foldl1 maxLen [ map length hdr, allColWidths l ]
allColWidths :: Foldable t => [[t a]] -> [Int]
allColWidths = map $ maximum . map length
nColWidths :: (Foldable t, Foldable t1) => [[[t1 a1]]] -> [t a] -> [Int]
nColWidths ll hdr = foldl1 maxLen $ map length hdr : mapListLen ll
mapListLen :: Foldable t => [[[t a]]] -> [[Int]]
mapListLen = map allColWidths
present1LineValue :: [[String]] -> [String] -> [String]
present1LineValue strRep header =
let hPad = colWidths strRep header
in
[ "┌─" ++ hPadTable hPad "─" "─┬─" ++ "─┐",
"│ " ++ buildColumns hPad header ++ " │",
"├─" ++ hPadTable hPad "─" "─┼─" ++ "─┤"]
++ buildRow hPad strRep ++
[ "└─" ++ hPadTable hPad "─" "─┴─" ++ "─┘"]
presentNLineValue :: [[[String]]] -> [String] -> [String]
presentNLineValue strRepList' hdr =
let
hPad = nColWidths strRepList' hdr
in
[ "┌─" ++ hPadTable hPad "─" "─┬─" ++ "─┐",
"│ " ++ buildColumns hPad hdr ++ " │",
"╞═" ++ hPadTable hPad "═" "═╪═" ++ "═╡" ]
++ foldr (\a b -> buildRow hPad a ++ b ) [] strRepList' ++
[ "└─" ++ hPadTable hPad "─" "─┴─" ++ "─┘" ]
buildRow :: [Int] -> [[String]] -> [String]
buildRow hPad strRep = buildRow' hPad $ padTranspose strRep
buildRow' :: [Int] -> [[String]] -> [String]
buildRow' _ [] = ["│ │"]
buildRow' hPad [strRep] = ["│ " ++ buildColumns hPad strRep ++ " │"]
buildRow' hPad (strRep:strRepX) = ( "│ " ++ buildColumns hPad strRep ++ " │" ) : buildRow' hPad strRepX
padTranspose :: [[[t]]] -> [[[t]]]
padTranspose x = padTranspose' x ( maximum ( map length x ) 1 )
padTranspose' :: [[[t]]] -> Int -> [[[t]]]
padTranspose' [] _ = []
padTranspose' ([] : xss) l = padTranspose' xss ( l 1 )
padTranspose' ((x:xs) : xss) l = (x : map next xss) : padTranspose' (rPad l xs : map rest xss) ( l 1 )
next :: [[t]] -> [t]
next [] = []
next xs = head xs
rest :: [t] -> [t]
rest [] = []
rest xs = tail xs
rPad :: Int -> [[t]] -> [[t]]
rPad m xs = xs ++ replicate ( m length xs ) []
hPadColumn :: Int -> [a] -> [a]
hPadColumn x fillChar = concat ( replicate x fillChar )
hPadTable :: [Int] -> String -> String -> String
hPadTable [] _ _ = ""
hPadTable [x] fillChar _ = hPadColumn x fillChar
hPadTable (x:xs) fillChar divChar = hPadColumn x fillChar ++ divChar ++ hPadTable xs fillChar divChar