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

View File

@ -40,6 +40,7 @@ module Hledger.Utils.String (
-- * wide-character-aware layout
charWidth,
strWidth,
strWidthAnsi,
takeWidth,
fitString,
fitStringMulti,
@ -334,11 +335,19 @@ takeWidth w (c:cs) | cw <= w = c:takeWidth (w-cw) cs
-- see also http://unicode.org/reports/tr11/#Description
-- | Calculate the render width of a string, considering
-- wide characters (counted as double width), ANSI escape codes
-- (not counted), and line breaks (in a multi-line string, the longest
-- line determines the width).
-- wide characters (counted as double width), and line breaks
-- (in a multi-line string, the longest line determines the
-- width).
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.
--

View File

@ -263,7 +263,7 @@ import System.Console.CmdArgs.Explicit as C
import Lucid as L
import Text.Printf (printf)
import Text.Tabular as T
--import Text.Tabular.AsciiWide
import Text.Tabular.AsciiWide (renderWidth)
import Hledger
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
-- unless --no-elide is used.
balanceReportTableAsText :: ReportOpts -> Table String String MixedAmount -> String
balanceReportTableAsText ropts@ReportOpts{..} = tableAsText ropts showamt
balanceReportTableAsText ReportOpts{..} =
trimBorder . renderWidth pretty_tables_ id id showamt . leftAlignRowHeaders
where
showamt
| no_elide_ = showMixedAmountOneLineWithoutPrice color_
| otherwise = showMixedAmountElided color_
showamt = showMixedOneLine showAmountWithoutPrice Nothing mmax color_
mmax = if no_elide_ then Nothing else Just 22
tests_Balance = tests "Balance" [