cli:Improve pretty table rendering

This fixes table edges always using single-width intersections and
adds support for double horizontal lines with single vertical lines
This commit is contained in:
Eric Mertens 2020-02-27 14:29:59 -08:00 committed by Simon Michael
parent bc4ea83d86
commit 43b118c9b8

View File

@ -83,29 +83,18 @@ renderHLine _ _ _ NoLine = []
renderHLine pretty w h SingleLine = [renderHLine' pretty SingleLine w (horizontalBar pretty) h] renderHLine pretty w h SingleLine = [renderHLine' pretty SingleLine w (horizontalBar pretty) h]
renderHLine pretty w h DoubleLine = [renderHLine' pretty DoubleLine w (doubleHorizontalBar pretty) h] renderHLine pretty w h DoubleLine = [renderHLine' pretty DoubleLine w (doubleHorizontalBar pretty) h]
doubleCross :: Bool -> String
doubleCross pretty = if pretty then "" else "++"
doubleVerticalCross :: Bool -> String
doubleVerticalCross pretty = if pretty then "" else "++"
cross :: Bool -> Char
cross pretty = if pretty then '┼' else '+'
renderHLine' :: Bool -> Properties -> [Int] -> Char -> Header String -> String renderHLine' :: Bool -> Properties -> [Int] -> Char -> Header String -> String
renderHLine' pretty prop is sep h = [ cross pretty, sep ] ++ coreLine ++ [sep, cross pretty] renderHLine' pretty prop is sep h = edge ++ sep : coreLine ++ sep : edge
where where
edge = cross SingleLine prop
coreLine = concatMap helper $ flattenHeader $ zipHeader 0 is h coreLine = concatMap helper $ flattenHeader $ zipHeader 0 is h
helper = either vsep dashes helper = either vsep dashes
dashes (i,_) = replicate i sep dashes (i,_) = replicate i sep
vsep NoLine = replicate 2 sep -- match the double space sep in renderColumns vsep v = sep : cross v prop ++ [sep]
vsep SingleLine = sep : cross pretty : [sep]
vsep DoubleLine = sep : cross' ++ [sep]
cross' = case prop of
DoubleLine -> doubleCross pretty
_ -> doubleVerticalCross pretty
-- padLeft :: Int -> String -> String
-- padLeft l s = padding ++ s
-- where padding = replicate (l - length s) ' '
-- vertical horizontal
cross SingleLine SingleLine = if pretty then "" else "+"
cross SingleLine DoubleLine = if pretty then "" else "+"
cross DoubleLine SingleLine = if pretty then "" else "++"
cross DoubleLine DoubleLine = if pretty then "" else "++"
cross _ _ = ""