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.
This commit is contained in:
Simon Michael 2020-06-26 12:14:49 -07:00
parent 1a321c9ae0
commit 2739a70a38
4 changed files with 80 additions and 4 deletions

View File

@ -121,8 +121,10 @@ module Hledger.Data.Amount (
showMixedAmountDebug, showMixedAmountDebug,
showMixedAmountWithoutPrice, showMixedAmountWithoutPrice,
showMixedAmountOneLineWithoutPrice, showMixedAmountOneLineWithoutPrice,
showMixedAmountElided,
cshowMixedAmountWithoutPrice, cshowMixedAmountWithoutPrice,
cshowMixedAmountOneLineWithoutPrice, cshowMixedAmountOneLineWithoutPrice,
cshowMixedAmountElided,
showMixedAmountWithZeroCommodity, showMixedAmountWithZeroCommodity,
showMixedAmountWithPrecision, showMixedAmountWithPrecision,
setMixedAmountPrecision, setMixedAmountPrecision,
@ -736,6 +738,39 @@ cshowMixedAmountOneLineWithoutPrice m = intercalate ", " $ map cshowAmountWithou
(Mixed as) = normaliseMixedAmountSquashPricesForDisplay $ stripPrices m (Mixed as) = normaliseMixedAmountSquashPricesForDisplay $ stripPrices m
stripPrices (Mixed as) = Mixed $ map stripprice as where stripprice a = a{aprice=Nothing} 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. -- | Canonicalise a mixed amount's display styles using the provided commodity style map.
canonicaliseMixedAmount :: M.Map CommoditySymbol AmountStyle -> MixedAmount -> MixedAmount canonicaliseMixedAmount :: M.Map CommoditySymbol AmountStyle -> MixedAmount -> MixedAmount
canonicaliseMixedAmount styles (Mixed as) = Mixed $ map (canonicaliseAmount styles) as canonicaliseMixedAmount styles (Mixed as) = Mixed $ map (canonicaliseAmount styles) as

View File

@ -629,12 +629,16 @@ balanceReportAsTable opts@ReportOpts{average_, row_total_, balancetype_}
-- | Given a table representing a multi-column balance report (for example, -- | Given a table representing a multi-column balance report (for example,
-- made using 'balanceReportAsTable'), render it in a format suitable for -- 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 :: ReportOpts -> Table String String MixedAmount -> String
balanceReportTableAsText ropts = tableAsText ropts showamt balanceReportTableAsText ropts@ReportOpts{..} = tableAsText ropts showamt
where where
showamt | color_ ropts = cshowMixedAmountOneLineWithoutPrice showamt
| otherwise = showMixedAmountOneLineWithoutPrice | no_elide_ && color_ = cshowMixedAmountOneLineWithoutPrice
| no_elide_ = showMixedAmountOneLineWithoutPrice
| color_ = cshowMixedAmountElided
| otherwise = showMixedAmountElided
tests_Balance = tests "Balance" [ tests_Balance = tests "Balance" [

View File

@ -316,6 +316,17 @@ supported.
The `--transpose` flag can be used to exchange the rows and columns of The `--transpose` flag can be used to exchange the rows and columns of
a multicolumn report. 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 ### Budget report
With `--budget`, extra columns are displayed showing budget goals for each account and period, if any. With `--budget`, extra columns are displayed showing budget goals for each account and period, if any.

View File

@ -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