diff --git a/hledger-lib/Hledger/Reports.hs b/hledger-lib/Hledger/Reports.hs index 854730236..66ca39eb2 100644 --- a/hledger-lib/Hledger/Reports.hs +++ b/hledger-lib/Hledger/Reports.hs @@ -539,6 +539,7 @@ isInteresting :: ReportOpts -> Ledger -> AccountName -> Bool isInteresting opts l a | flat_ opts = isInterestingFlat opts l a | otherwise = isInterestingIndented opts l a +-- | Determine whether an account should get its own line in the --flat balance report. isInterestingFlat :: ReportOpts -> Ledger -> AccountName -> Bool isInterestingFlat opts l a = notempty || emptyflag where @@ -546,16 +547,19 @@ isInterestingFlat opts l a = notempty || emptyflag notempty = not $ isZeroMixedAmount $ exclusiveBalance acct emptyflag = empty_ opts +-- | Determine whether an account should get its own line in the indented +-- balance report. Cf Balance module doc. isInterestingIndented :: ReportOpts -> Ledger -> AccountName -> Bool isInterestingIndented opts l a - | numinterestingsubs==1 && not atmaxdepth = notlikesub - | otherwise = notzero || emptyflag + | numinterestingsubs == 1 && samebalanceassub && not atmaxdepth = False + | numinterestingsubs < 2 && zerobalance && not emptyflag = False + | otherwise = True where atmaxdepth = isJust d && Just (accountNameLevel a) == d where d = depth_ opts emptyflag = empty_ opts acct = ledgerAccount l a - notzero = not $ isZeroMixedAmount inclbalance where inclbalance = abalance acct - notlikesub = not $ isZeroMixedAmount exclbalance where exclbalance = sumPostings $ apostings acct + zerobalance = isZeroMixedAmount inclbalance where inclbalance = abalance acct + samebalanceassub = isZeroMixedAmount exclbalance where exclbalance = sumPostings $ apostings acct numinterestingsubs = length $ filter isInterestingTree subtrees where isInterestingTree = treeany (isInteresting opts l . aname) diff --git a/hledger/Hledger/Cli.hs b/hledger/Hledger/Cli.hs index b7e5c5081..586bb811a 100644 --- a/hledger/Hledger/Cli.hs +++ b/hledger/Hledger/Cli.hs @@ -249,20 +249,6 @@ tests_Hledger_Cli = TestList ," 0" ] - ,"balance report elides zero-balance root account(s)" ~: do - j <- readJournal' - (unlines - ["2008/1/1 one" - ," test:a 1" - ," test:b" - ]) - accountsReportAsText defreportopts (accountsReport defreportopts nullfilterspec j) `is` - [" 1 test:a" - ," -1 test:b" - ,"--------------------" - ," 0" - ] - ] ,"journalCanonicaliseAmounts" ~: diff --git a/hledger/Hledger/Cli/Balance.hs b/hledger/Hledger/Cli/Balance.hs index 5f972b858..e7d76b074 100644 --- a/hledger/Hledger/Cli/Balance.hs +++ b/hledger/Hledger/Cli/Balance.hs @@ -82,15 +82,14 @@ Also, the balance report shows the total of all displayed accounts, when that is non-zero. Here, it is displayed because the accounts shown add up to $-1. -Here is a more precise definition of \"interesting\" accounts in ledger's -balance report: - -- an account which has just one interesting subaccount branch, and which - is not at the report's maximum depth, is interesting if the balance is - different from the subaccount's, and otherwise boring. - -- any other account is interesting if it has a non-zero balance, or the -E - flag is used. +Also, non-interesting accounts may be elided. Here's an imperfect +description of the ledger balance command's eliding behaviour: +\"Interesting\" accounts are displayed on their own line. An account less +deep than the report's max depth, with just one interesting subaccount, +and the same balance as the subaccount, is non-interesting, and prefixed +to the subaccount's line, unless (hledger's) --no-elide is in effect. +An account with a zero inclusive balance and less than two interesting +subaccounts is not displayed at all, unless --empty is in effect. -} diff --git a/tests/eliding-balance.test b/tests/eliding-balance.test index 30bae6259..bbab4b3e0 100644 --- a/tests/eliding-balance.test +++ b/tests/eliding-balance.test @@ -1,3 +1,4 @@ +#!/usr/bin/env shelltest # 1. One commodity. Zero accounts should be elided but the final total should not. bin/hledger -f - balance <<< @@ -12,3 +13,36 @@ bin/hledger -f - balance -------------------- 0 >>>=0 + +# 2. An uninteresting parent account (with same balance as its single subaccount) is elided by default, like ledger +bin/hledger -f - balance --no-total +<<< +1/1 + (a:b) 1 +>>> + 1 a:b +>>>=0 + +# 3. But not with --no-elide +bin/hledger -f - balance --no-total --no-elide +<<< +1/1 + (a:b) 1 +>>> + 1 a + 1 b +>>>=0 + +# 4. Nor when it has more than one subaccount +bin/hledger -f - balance --no-total +<<< +1/1 + (a:b) 1 + (a:c) -1 +>>> + 0 a + 1 b + -1 c +>>>2 +>>>=0 +