From 2739a70a38b28114d9ab042cdf3597f53337dc02 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 26 Jun 2020 12:14:49 -0700 Subject: [PATCH] balcmds: elide amounts with 3 or more commodities, unless --no-elide Multicolumn balance reports showing many commodities tend to become unreadably wide, especially in tree mode. Now by default we show at most two commodities, and a count of the rest if there are more than two. This should help keep reports somewhat readable by default. --- hledger-lib/Hledger/Data/Amount.hs | 35 +++++++++++++++++++++++++ hledger/Hledger/Cli/Commands/Balance.hs | 12 ++++++--- hledger/Hledger/Cli/Commands/Balance.md | 11 ++++++++ tests/balance/multicommodity.test | 26 ++++++++++++++++++ 4 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 tests/balance/multicommodity.test diff --git a/hledger-lib/Hledger/Data/Amount.hs b/hledger-lib/Hledger/Data/Amount.hs index 244fd6a79..e590d24be 100644 --- a/hledger-lib/Hledger/Data/Amount.hs +++ b/hledger-lib/Hledger/Data/Amount.hs @@ -121,8 +121,10 @@ module Hledger.Data.Amount ( showMixedAmountDebug, showMixedAmountWithoutPrice, showMixedAmountOneLineWithoutPrice, + showMixedAmountElided, cshowMixedAmountWithoutPrice, cshowMixedAmountOneLineWithoutPrice, + cshowMixedAmountElided, showMixedAmountWithZeroCommodity, showMixedAmountWithPrecision, setMixedAmountPrecision, @@ -736,6 +738,39 @@ cshowMixedAmountOneLineWithoutPrice m = intercalate ", " $ map cshowAmountWithou (Mixed as) = normaliseMixedAmountSquashPricesForDisplay $ stripPrices m stripPrices (Mixed as) = Mixed $ map stripprice as where stripprice a = a{aprice=Nothing} +-- | Like showMixedAmountOneLineWithoutPrice, but show at most two commodities, +-- with a elision indicator if there are more. +showMixedAmountElided :: MixedAmount -> String +showMixedAmountElided m = intercalate ", " $ take 2 astrs ++ elisionstr + where + astrs = map showAmountWithoutPrice as + where + (Mixed as) = normaliseMixedAmountSquashPricesForDisplay $ stripPrices m + where + stripPrices (Mixed as) = Mixed $ map stripprice as + where + stripprice a = a{aprice=Nothing} + elisionstr | n > 2 = [show (n - 2) ++ " more.."] + | otherwise = [] + where + n = length astrs + +-- | Colour version. +cshowMixedAmountElided :: MixedAmount -> String +cshowMixedAmountElided m = intercalate ", " $ take 2 astrs ++ elisionstr + where + astrs = map cshowAmountWithoutPrice as + where + (Mixed as) = normaliseMixedAmountSquashPricesForDisplay $ stripPrices m + where + stripPrices (Mixed as) = Mixed $ map stripprice as + where + stripprice a = a{aprice=Nothing} + elisionstr | n > 2 = [show (n - 2) ++ " more.."] + | otherwise = [] + where + n = length astrs + -- | Canonicalise a mixed amount's display styles using the provided commodity style map. canonicaliseMixedAmount :: M.Map CommoditySymbol AmountStyle -> MixedAmount -> MixedAmount canonicaliseMixedAmount styles (Mixed as) = Mixed $ map (canonicaliseAmount styles) as diff --git a/hledger/Hledger/Cli/Commands/Balance.hs b/hledger/Hledger/Cli/Commands/Balance.hs index 36246cf4c..6bbe958c0 100644 --- a/hledger/Hledger/Cli/Commands/Balance.hs +++ b/hledger/Hledger/Cli/Commands/Balance.hs @@ -629,12 +629,16 @@ balanceReportAsTable opts@ReportOpts{average_, row_total_, balancetype_} -- | Given a table representing a multi-column balance report (for example, -- made using 'balanceReportAsTable'), render it in a format suitable for --- console output. +-- 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 = tableAsText ropts showamt +balanceReportTableAsText ropts@ReportOpts{..} = tableAsText ropts showamt where - showamt | color_ ropts = cshowMixedAmountOneLineWithoutPrice - | otherwise = showMixedAmountOneLineWithoutPrice + showamt + | no_elide_ && color_ = cshowMixedAmountOneLineWithoutPrice + | no_elide_ = showMixedAmountOneLineWithoutPrice + | color_ = cshowMixedAmountElided + | otherwise = showMixedAmountElided tests_Balance = tests "Balance" [ diff --git a/hledger/Hledger/Cli/Commands/Balance.md b/hledger/Hledger/Cli/Commands/Balance.md index f951bec97..08b551ff0 100644 --- a/hledger/Hledger/Cli/Commands/Balance.md +++ b/hledger/Hledger/Cli/Commands/Balance.md @@ -316,6 +316,17 @@ supported. The `--transpose` flag can be used to exchange the rows and columns of a multicolumn report. +When showing multicommodity amounts, multicolumn balance reports will +elide any amounts which have more than two commodities, since +otherwise columns could get very wide. The `--no-elide` flag disables +this. Hiding totals with the `-N/--no-total` flag can also help reduce +the width of multicommodity reports. + +When the report is still too wide, a good workaround is to pipe it +into `less -RS` (-R for colour, -S to chop long lines). +Eg: `hledger bal -D | less -RS`. + + ### Budget report With `--budget`, extra columns are displayed showing budget goals for each account and period, if any. diff --git a/tests/balance/multicommodity.test b/tests/balance/multicommodity.test new file mode 100644 index 000000000..b5645ae28 --- /dev/null +++ b/tests/balance/multicommodity.test @@ -0,0 +1,26 @@ +# 1. In tabular balance reports, amounts with more than two commodities are elided. +< +2020-01-01 + (a) 1A + (a) 1B + (a) 1C + (a) 1D + +$ hledger -f- bal -Y +Balance changes in 2020: + + || 2020 +===++================== + a || 1A, 1B, 2 more.. +---++------------------ + || 1A, 1B, 2 more.. + +# 2. Unless --no-elide is used. +$ hledger -f- bal -Y --no-elide +Balance changes in 2020: + + || 2020 +===++================ + a || 1A, 1B, 1C, 1D +---++---------------- + || 1A, 1B, 1C, 1D