lib: Remove some uses of pad(Left|Right)Wide to ensure we're using the

width provided by showMixed.

Also refactor renderTable to be a bit clearer, and to avoid duplicate
calculations.
This commit is contained in:
Stephen Morgan 2020-11-02 14:16:46 +11:00
parent e9a16edb58
commit 7e47c11fda
2 changed files with 21 additions and 16 deletions

View File

@ -168,7 +168,7 @@ showPosting p@Posting{paccount=a,pamount=amt,ptype=t} =
BalancedVirtualPosting -> (\s -> "["++s++"]", acctnamewidth-2) BalancedVirtualPosting -> (\s -> "["++s++"]", acctnamewidth-2)
VirtualPosting -> (\s -> "("++s++")", acctnamewidth-2) VirtualPosting -> (\s -> "("++s++")", acctnamewidth-2)
_ -> (id,acctnamewidth) _ -> (id,acctnamewidth)
showamount = padLeftWide 12 . showMixedAmount showamount = fst . showMixed showAmount (Just 12) Nothing False
showComment :: Text -> String showComment :: Text -> String

View File

@ -22,27 +22,32 @@ renderTable :: Bool -- ^ Whether to display the outer borders
-> String -> String
renderTable borders pretty fr fc f (Table rh ch cells) = renderTable borders pretty fr fc f (Table rh ch cells) =
unlines . addBorders $ unlines . addBorders $
[ renderColumns borders pretty sizes ch2 renderColumns borders pretty sizes ch2
, bar VM DoubleLine -- +======================================+ : bar VM DoubleLine -- +======================================+
] ++ : renderRs (fmap renderR $ zipHeader [] cellContents rowHeaders)
(renderRs $ fmap renderR $ zipHeader [] cells $ fmap fr rh)
where where
bar vpos prop = concat $ renderHLine vpos borders pretty sizes ch2 prop
-- ch2 and cell2 include the row and column labels
ch2 = Group DoubleLine [Header emptyCell, fmap fc ch]
cells2 = headerContents ch2
: zipWith (\h cs -> h : map f cs) rhStrings cells
--
renderR (cs,h) = renderColumns borders pretty sizes $ Group DoubleLine renderR (cs,h) = renderColumns borders pretty sizes $ Group DoubleLine
[ Header h [ Header h
, fmap fst $ zipHeader emptyCell (map f cs) ch] , fmap fst $ zipHeader emptyCell cs colHeaders
rhStrings = map fr $ headerContents rh ]
rowHeaders = fmap fr rh
colHeaders = fmap fc ch
cellContents = map (map f) cells
-- ch2 and cell2 include the row and column labels
ch2 = Group DoubleLine [Header emptyCell, colHeaders]
cells2 = headerContents ch2 : zipWith (:) (headerContents rowHeaders) cellContents
-- maximum width for each column -- maximum width for each column
sizes = map (maximum . map csWidth) $ transpose cells2 sizes = map (maximum . map csWidth) $ transpose cells2
renderRs (Header s) = [s] renderRs (Header s) = [s]
renderRs (Group p hs) = concat . intersperse sep $ map renderRs hs renderRs (Group p hs) = concat . intersperse sep $ map renderRs hs
where sep = renderHLine VM borders pretty sizes ch2 p where sep = renderHLine VM borders pretty sizes ch2 p
-- borders and bars
addBorders xs = if borders then bar VT SingleLine : xs ++ [bar VB SingleLine] else xs addBorders xs = if borders then bar VT SingleLine : xs ++ [bar VB SingleLine] else xs
bar vpos prop = concat $ renderHLine vpos borders pretty sizes ch2 prop
data CellSpec = CellSpec data CellSpec = CellSpec
@ -90,8 +95,8 @@ renderColumns borders pretty is h = addBorders coreLine
addBorders xs = if borders then leftBar pretty ++ xs ++ rightBar pretty else ' ' : xs ++ " " addBorders xs = if borders then leftBar pretty ++ xs ++ rightBar pretty else ' ' : xs ++ " "
coreLine = concatMap helper $ flattenHeader $ zipHeader 0 is h coreLine = concatMap helper $ flattenHeader $ zipHeader 0 is h
helper = either hsep (\(w, cs) -> case csAlign cs of helper = either hsep (\(w, cs) -> case csAlign cs of
AlignLeft -> padRightWide w (csString cs) AlignLeft -> csString cs ++ replicate (w - csWidth cs) ' '
AlignRight -> padLeftWide w (csString cs) AlignRight -> replicate (w - csWidth cs) ' ' ++ csString cs
) )
hsep :: Properties -> String hsep :: Properties -> String
hsep NoLine = " " hsep NoLine = " "