lib,cli: bal uses new amount display functions, no longer needs to strip ansi.

This commit is contained in:
Stephen Morgan 2020-11-02 11:32:53 +11:00
parent e50a8c0f34
commit a2b7a03fc4
3 changed files with 30 additions and 18 deletions

View File

@ -20,6 +20,8 @@ module Hledger.Reports.MultiBalanceReport (
compoundBalanceReportWith, compoundBalanceReportWith,
tableAsText, tableAsText,
trimBorder,
leftAlignRowHeaders,
sortRows, sortRows,
sortRowsLike, sortRowsLike,
@ -597,17 +599,18 @@ dbg'' s = let p = "multiBalanceReport" in Hledger.Utils.dbg5 (p++" "++s)
-- common rendering helper, XXX here for now -- common rendering helper, XXX here for now
tableAsText :: ReportOpts -> (a -> String) -> Table String String a -> String tableAsText :: ReportOpts -> (a -> String) -> Table String String a -> String
tableAsText (ReportOpts{pretty_tables_ = pretty}) showcell = tableAsText (ReportOpts{pretty_tables_ = pretty}) showcell =
unlines trimBorder
. trimborder
. lines
. render pretty id id showcell . render pretty id id showcell
. align . leftAlignRowHeaders
trimBorder :: String -> String
trimBorder = unlines . map (drop 1 . init) . drop 1 . init . lines
leftAlignRowHeaders :: Table String ch a -> Table String ch a
leftAlignRowHeaders (Table l t d) = Table l' t d
where where
trimborder = drop 1 . init . map (drop 1 . init) acctswidth = maximum' $ map strWidth (headerContents l)
align (Table l t d) = Table l' t d l' = padRightWide acctswidth <$> l
where
acctswidth = maximum' $ map strWidth (headerContents l)
l' = padRightWide acctswidth <$> l
-- tests -- tests

View File

@ -40,6 +40,7 @@ module Hledger.Utils.String (
-- * wide-character-aware layout -- * wide-character-aware layout
charWidth, charWidth,
strWidth, strWidth,
strWidthAnsi,
takeWidth, takeWidth,
fitString, fitString,
fitStringMulti, fitStringMulti,
@ -334,11 +335,19 @@ takeWidth w (c:cs) | cw <= w = c:takeWidth (w-cw) cs
-- see also http://unicode.org/reports/tr11/#Description -- see also http://unicode.org/reports/tr11/#Description
-- | Calculate the render width of a string, considering -- | Calculate the render width of a string, considering
-- wide characters (counted as double width), ANSI escape codes -- wide characters (counted as double width), and line breaks
-- (not counted), and line breaks (in a multi-line string, the longest -- (in a multi-line string, the longest line determines the
-- line determines the width). -- width).
strWidth :: String -> Int strWidth :: String -> Int
strWidth = maximum . (0:) . map (foldr (\a b -> charWidth a + b) 0) . lines . stripAnsi strWidth = maximum . (0:) . map (foldr (\a b -> charWidth a + b) 0) . lines
-- | Like strWidth, but also strips ANSI escape sequences before
-- calculating the width.
--
-- This is no longer used in code, as widths are calculated before
-- adding ANSI escape sequences, but is being kept around for now.
strWidthAnsi :: String -> Int
strWidthAnsi = strWidth . stripAnsi
-- | Strip ANSI escape sequences from a string. -- | Strip ANSI escape sequences from a string.
-- --

View File

@ -263,7 +263,7 @@ import System.Console.CmdArgs.Explicit as C
import Lucid as L import Lucid as L
import Text.Printf (printf) import Text.Printf (printf)
import Text.Tabular as T import Text.Tabular as T
--import Text.Tabular.AsciiWide import Text.Tabular.AsciiWide (renderWidth)
import Hledger import Hledger
import Hledger.Cli.CliOptions import Hledger.Cli.CliOptions
@ -609,11 +609,11 @@ balanceReportAsTable opts@ReportOpts{average_, row_total_, balancetype_}
-- console output. Amounts with more than two commodities will be elided -- console output. Amounts with more than two commodities will be elided
-- unless --no-elide is used. -- unless --no-elide is used.
balanceReportTableAsText :: ReportOpts -> Table String String MixedAmount -> String balanceReportTableAsText :: ReportOpts -> Table String String MixedAmount -> String
balanceReportTableAsText ropts@ReportOpts{..} = tableAsText ropts showamt balanceReportTableAsText ReportOpts{..} =
trimBorder . renderWidth pretty_tables_ id id showamt . leftAlignRowHeaders
where where
showamt showamt = showMixedOneLine showAmountWithoutPrice Nothing mmax color_
| no_elide_ = showMixedAmountOneLineWithoutPrice color_ mmax = if no_elide_ then Nothing else Just 22
| otherwise = showMixedAmountElided color_
tests_Balance = tests "Balance" [ tests_Balance = tests "Balance" [