From a2b7a03fc44db858c93a7b68c5f83a1e0a2c3e54 Mon Sep 17 00:00:00 2001 From: Stephen Morgan Date: Mon, 2 Nov 2020 11:32:53 +1100 Subject: [PATCH] lib,cli: bal uses new amount display functions, no longer needs to strip ansi. --- .../Hledger/Reports/MultiBalanceReport.hs | 21 +++++++++++-------- hledger-lib/Hledger/Utils/String.hs | 17 +++++++++++---- hledger/Hledger/Cli/Commands/Balance.hs | 10 ++++----- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs index 0f9c4d93e..242a0b2a3 100644 --- a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs +++ b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs @@ -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 diff --git a/hledger-lib/Hledger/Utils/String.hs b/hledger-lib/Hledger/Utils/String.hs index f15a80074..bf593cf27 100644 --- a/hledger-lib/Hledger/Utils/String.hs +++ b/hledger-lib/Hledger/Utils/String.hs @@ -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. -- diff --git a/hledger/Hledger/Cli/Commands/Balance.hs b/hledger/Hledger/Cli/Commands/Balance.hs index f03f04e20..2057ee1af 100644 --- a/hledger/Hledger/Cli/Commands/Balance.hs +++ b/hledger/Hledger/Cli/Commands/Balance.hs @@ -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" [