diff --git a/hledger-lib/Hledger/Data/AccountName.hs b/hledger-lib/Hledger/Data/AccountName.hs index 1a6d0f465..48e5fbd34 100644 --- a/hledger-lib/Hledger/Data/AccountName.hs +++ b/hledger-lib/Hledger/Data/AccountName.hs @@ -138,6 +138,14 @@ accountNameType :: M.Map AccountName AccountType -> AccountName -> Maybe Account accountNameType atypes a = asum (map (`M.lookup` atypes) $ a : parentAccountNames a) <|> accountNameInferType a +-- | The level (depth) of an account name. +-- +-- >>> accountNameLevel "" -- special case +-- 0 +-- >>> accountNameLevel "assets" +-- 1 +-- >>> accountNameLevel "assets:cash" +-- 2 accountNameLevel :: AccountName -> Int accountNameLevel "" = 0 accountNameLevel a = T.length (T.filter (==acctsepchar) a) + 1 diff --git a/hledger-lib/Hledger/Reports/BalanceReport.hs b/hledger-lib/Hledger/Reports/BalanceReport.hs index 0512e35c5..4940bc812 100644 --- a/hledger-lib/Hledger/Reports/BalanceReport.hs +++ b/hledger-lib/Hledger/Reports/BalanceReport.hs @@ -71,7 +71,7 @@ balanceReport rspec j = (rows, total) report = multiBalanceReport rspec j rows = [( prrFullName row , prrDisplayName row - , prrIndent row - 1 -- BalanceReport uses 0-based account depths + , prrIndent row , prrTotal row ) | row <- prRows report] total = prrTotal $ prTotals report diff --git a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs index 3468ada44..3eb068524 100644 --- a/hledger-lib/Hledger/Reports/MultiBalanceReport.hs +++ b/hledger-lib/Hledger/Reports/MultiBalanceReport.hs @@ -400,34 +400,33 @@ buildReportRows ropts displaynames = rowavg = averageMixedAmounts rowbals balance = case accountlistmode_ ropts of ALTree -> aibalance; ALFlat -> aebalance --- | Calculate accounts which are to be displayed in the report, as well as --- their name and depth +-- | Calculate accounts which are to be displayed in the report, +-- and their name and their indent level if displayed in tree mode. displayedAccounts :: ReportSpec -> Set AccountName -> HashMap AccountName (Map DateSpan Account) -> HashMap AccountName DisplayName displayedAccounts ReportSpec{_rsQuery=query,_rsReportOpts=ropts} unelidableaccts valuedaccts - | qdepth == 0 = HM.singleton "..." $ DisplayName "..." "..." 1 + | qdepth == 0 = HM.singleton "..." $ DisplayName "..." "..." 0 | otherwise = HM.mapWithKey (\a _ -> displayedName a) displayedAccts where + displayedName name = case accountlistmode_ ropts of + ALTree -> DisplayName name leaf (max 0 $ level - 1 - boringParents) + ALFlat -> DisplayName name droppedName 0 + where + droppedName = accountNameDrop (drop_ ropts) name + leaf = accountNameFromComponents . reverse . map accountLeafName $ + droppedName : takeWhile notDisplayed parents + level = max 0 $ (accountNameLevel name) - drop_ ropts + parents = take (level - 1) $ parentAccountNames name + boringParents = if no_elide_ ropts then 0 else length $ filter notDisplayed parents + notDisplayed = not . (`HM.member` displayedAccts) + -- Accounts which are to be displayed displayedAccts = (if qdepth == 0 then id else HM.filterWithKey keep) valuedaccts where keep name amts = isInteresting name amts || name `HM.member` interestingParents - displayedName name = case accountlistmode_ ropts of - ALTree -> DisplayName name leaf . max 0 $ level - boringParents - ALFlat -> DisplayName name droppedName 1 - where - droppedName = accountNameDrop (drop_ ropts) name - leaf = accountNameFromComponents . reverse . map accountLeafName $ - droppedName : takeWhile notDisplayed parents - - level = max 0 $ accountNameLevel name - drop_ ropts - parents = take (level - 1) $ parentAccountNames name - boringParents = if no_elide_ ropts then 0 else length $ filter notDisplayed parents - notDisplayed = not . (`HM.member` displayedAccts) - -- Accounts interesting for their own sake isInteresting name amts = d <= qdepth -- Throw out anything too deep diff --git a/hledger-lib/Hledger/Reports/ReportTypes.hs b/hledger-lib/Hledger/Reports/ReportTypes.hs index 8868257b9..88aad3afa 100644 --- a/hledger-lib/Hledger/Reports/ReportTypes.hs +++ b/hledger-lib/Hledger/Reports/ReportTypes.hs @@ -227,13 +227,12 @@ instance ToJSON DisplayName where toJSON = toJSON . displayFull toEncoding = toEncoding . displayFull --- | Construct a flat display name, where the full name is also displayed at --- depth 1 +-- | Construct a display name for a list report, where full names are shown unindented. flatDisplayName :: AccountName -> DisplayName -flatDisplayName a = DisplayName a a 1 +flatDisplayName a = DisplayName a a 0 --- | Construct a tree display name, where only the leaf is displayed at its --- given depth +-- | Construct a display name for a tree report, where leaf names (possibly prefixed by +-- boring parents) are shown indented). treeDisplayName :: AccountName -> DisplayName treeDisplayName a = DisplayName a (accountLeafName a) (accountNameLevel a) diff --git a/hledger/Hledger/Cli/Commands/Balance.hs b/hledger/Hledger/Cli/Commands/Balance.hs index 97d99e1fa..d9c90c6b0 100644 --- a/hledger/Hledger/Cli/Commands/Balance.hs +++ b/hledger/Hledger/Cli/Commands/Balance.hs @@ -932,7 +932,7 @@ multiBalanceReportAsTable opts@ReportOpts{summary_only_, average_, row_total_, b fullRowAsTexts row = (replicate (length rs) (renderacct row), rs) where rs = multiBalanceRowAsText opts row - renderacct row' = T.replicate ((prrIndent row' - 1) * 2) " " <> prrDisplayName row' + renderacct row' = T.replicate (prrIndent row' * 2) " " <> prrDisplayName row' addtotalrow | no_total_ opts = id | otherwise = @@ -1343,7 +1343,7 @@ renderPeriodicAcct :: ReportOpts -> Text -> PeriodicReportRow DisplayName a -> Text renderPeriodicAcct opts space row = renderBalanceAcct opts space - (prrFullName row, prrDisplayName row, prrIndent row - 1) + (prrFullName row, prrDisplayName row, prrIndent row) -- tests