From 7e47c11fda139db2601a6c0052e53a65cee036ec Mon Sep 17 00:00:00 2001 From: Stephen Morgan Date: Mon, 2 Nov 2020 14:16:46 +1100 Subject: [PATCH] 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. --- hledger-lib/Hledger/Data/Posting.hs | 2 +- hledger-lib/Text/Tabular/AsciiWide.hs | 35 +++++++++++++++------------ 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/hledger-lib/Hledger/Data/Posting.hs b/hledger-lib/Hledger/Data/Posting.hs index db03c9745..006fb19ce 100644 --- a/hledger-lib/Hledger/Data/Posting.hs +++ b/hledger-lib/Hledger/Data/Posting.hs @@ -168,7 +168,7 @@ showPosting p@Posting{paccount=a,pamount=amt,ptype=t} = BalancedVirtualPosting -> (\s -> "["++s++"]", acctnamewidth-2) VirtualPosting -> (\s -> "("++s++")", acctnamewidth-2) _ -> (id,acctnamewidth) - showamount = padLeftWide 12 . showMixedAmount + showamount = fst . showMixed showAmount (Just 12) Nothing False showComment :: Text -> String diff --git a/hledger-lib/Text/Tabular/AsciiWide.hs b/hledger-lib/Text/Tabular/AsciiWide.hs index 4d1d9f984..b2bdff2fa 100644 --- a/hledger-lib/Text/Tabular/AsciiWide.hs +++ b/hledger-lib/Text/Tabular/AsciiWide.hs @@ -22,27 +22,32 @@ renderTable :: Bool -- ^ Whether to display the outer borders -> String renderTable borders pretty fr fc f (Table rh ch cells) = unlines . addBorders $ - [ renderColumns borders pretty sizes ch2 - , bar VM DoubleLine -- +======================================+ - ] ++ - (renderRs $ fmap renderR $ zipHeader [] cells $ fmap fr rh) + renderColumns borders pretty sizes ch2 + : bar VM DoubleLine -- +======================================+ + : renderRs (fmap renderR $ zipHeader [] cellContents rowHeaders) 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 - [ Header h - , fmap fst $ zipHeader emptyCell (map f cs) ch] - rhStrings = map fr $ headerContents rh + [ Header h + , fmap fst $ zipHeader emptyCell cs colHeaders + ] + + 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 sizes = map (maximum . map csWidth) $ transpose cells2 renderRs (Header s) = [s] renderRs (Group p hs) = concat . intersperse sep $ map renderRs hs 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 + bar vpos prop = concat $ renderHLine vpos borders pretty sizes ch2 prop 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 ++ " " coreLine = concatMap helper $ flattenHeader $ zipHeader 0 is h helper = either hsep (\(w, cs) -> case csAlign cs of - AlignLeft -> padRightWide w (csString cs) - AlignRight -> padLeftWide w (csString cs) + AlignLeft -> csString cs ++ replicate (w - csWidth cs) ' ' + AlignRight -> replicate (w - csWidth cs) ' ' ++ csString cs ) hsep :: Properties -> String hsep NoLine = " "