From e2c55aafa9fc915d1a33423157c48d8262d9d63c Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Wed, 4 Apr 2018 13:04:34 +0100 Subject: [PATCH] budget: --drop preserves the top-level account --- hledger-lib/Hledger/Data/AccountName.hs | 18 +++++++++++++++++- hledger-lib/Hledger/Reports/BudgetReport.hs | 2 +- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/hledger-lib/Hledger/Data/AccountName.hs b/hledger-lib/Hledger/Data/AccountName.hs index aeb39d2a0..cd523c5eb 100644 --- a/hledger-lib/Hledger/Data/AccountName.hs +++ b/hledger-lib/Hledger/Data/AccountName.hs @@ -57,8 +57,24 @@ accountNameLevel :: AccountName -> Int accountNameLevel "" = 0 accountNameLevel a = T.length (T.filter (==acctsepchar) a) + 1 +-- | Remove some number of account name components from the front of the account name. +-- If the special "" top-level account is present, it is preserved and +-- dropping affects the rest of the account name. accountNameDrop :: Int -> AccountName -> AccountName -accountNameDrop n = accountNameFromComponents . drop n . accountNameComponents +accountNameDrop n a + | a == unbudgetedAccount = a + | unbudgetedAccountAndSep `T.isPrefixOf` a = + case accountNameDrop n $ T.drop (T.length unbudgetedAccountAndSep) a of + "" -> unbudgetedAccount + a' -> unbudgetedAccountAndSep <> a' + | otherwise = accountNameFromComponents $ drop n $ accountNameComponents a + where + unbudgetedAccountAndSep = unbudgetedAccount <> acctsep + +-- | A top-level account prefixed to some accounts in budget reports. +-- Defined here so it can be ignored by accountNameDrop. +unbudgetedAccount :: T.Text +unbudgetedAccount = "" -- | Sorted unique account names implied by these account names, -- ie these plus all their parent accounts up to the root. diff --git a/hledger-lib/Hledger/Reports/BudgetReport.hs b/hledger-lib/Hledger/Reports/BudgetReport.hs index d3330d733..d3604bb31 100644 --- a/hledger-lib/Hledger/Reports/BudgetReport.hs +++ b/hledger-lib/Hledger/Reports/BudgetReport.hs @@ -106,7 +106,7 @@ budgetRollUp showunbudgeted budget j = j { jtxns = remapTxn <$> jtxns j } | otherwise = case parentAccountName acctName of "" | showunbudgeted -> origAcctName - | otherwise -> T.append (T.pack ":") acctName -- TODO: --drop should not remove this + | otherwise -> unbudgetedAccount <> acctsep <> acctName parent -> remapAccount' parent remapPosting p = p { paccount = remapAccount $ paccount p, porigin = Just . fromMaybe p $ porigin p } remapTxn = mapPostings (map remapPosting)