From 73572e2d8505812a18cf25a7966b36df55ab5981 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Mon, 24 May 2010 20:45:48 +0000 Subject: [PATCH] balance: with --flat, --drop N elides leading account name components. Feature or frill ? --- Hledger/Cli/Commands/Balance.hs | 2 +- Hledger/Cli/Options.hs | 11 ++++++++++- MANUAL.markdown | 5 ++++- hledger-lib/Hledger/Data/AccountName.hs | 3 +++ 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Hledger/Cli/Commands/Balance.hs b/Hledger/Cli/Commands/Balance.hs index b424688fc..7e41924a9 100644 --- a/Hledger/Cli/Commands/Balance.hs +++ b/Hledger/Cli/Commands/Balance.hs @@ -140,7 +140,7 @@ showInterestingAccount opts l interestingaccts a = concatTopPadded [amt, " ", n bal | Flat `elem` opts = exclusiveBalance acct | otherwise = abalance acct acct = ledgerAccount l a - name | Flat `elem` opts = a + name | Flat `elem` opts = accountNameDrop (dropFromOpts opts) a | otherwise = depthspacer ++ partialname parents = parentAccountNames a interestingparents = filter (`elem` interestingaccts) parents diff --git a/Hledger/Cli/Options.hs b/Hledger/Cli/Options.hs index f430d19df..2a81059a8 100644 --- a/Hledger/Cli/Options.hs +++ b/Hledger/Cli/Options.hs @@ -76,6 +76,7 @@ options = [ ,Option "E" ["empty"] (NoArg Empty) "show empty/zero things which are normally elided" ,Option "R" ["real"] (NoArg Real) "report only on real (non-virtual) transactions" ,Option "" ["flat"] (NoArg Flat) "balance report: show full account names, unindented" + ,Option "" ["drop"] (ReqArg Drop "N") "balance report: with --flat, elide first N account name components" ,Option "" ["no-total"] (NoArg NoTotal) "balance report: hide the final total" -- ,Option "s" ["subtotal"] (NoArg SubTotal) "balance report: show subaccounts" ,Option "W" ["weekly"] (NoArg WeeklyOpt) "register report: show weekly summary" @@ -115,6 +116,7 @@ data Opt = Empty | Real | Flat | + Drop {value::String} | NoTotal | SubTotal | WeeklyOpt | @@ -212,13 +214,20 @@ intervalFromOpts opts = periodopts = reverse $ optValuesForConstructor Period opts intervalopts = reverse $ filter (`elem` [WeeklyOpt,MonthlyOpt,QuarterlyOpt,YearlyOpt]) opts --- | Get the value of the (last) depth option, if any, otherwise a large number. +-- | Get the value of the (last) depth option, if any. depthFromOpts :: [Opt] -> Maybe Int depthFromOpts opts = listtomaybeint $ optValuesForConstructor Depth opts where listtomaybeint [] = Nothing listtomaybeint vs = Just $ read $ last vs +-- | Get the value of the (last) drop option, if any, otherwise 0. +dropFromOpts :: [Opt] -> Int +dropFromOpts opts = fromMaybe 0 $ listtomaybeint $ optValuesForConstructor Drop opts + where + listtomaybeint [] = Nothing + listtomaybeint vs = Just $ read $ last vs + -- | Get the value of the (last) display option, if any. displayExprFromOpts :: [Opt] -> Maybe String displayExprFromOpts opts = listtomaybe $ optValuesForConstructor Display opts diff --git a/MANUAL.markdown b/MANUAL.markdown index 6c9905d1f..b6095ba60 100644 --- a/MANUAL.markdown +++ b/MANUAL.markdown @@ -282,13 +282,16 @@ Examples: $ hledger balance food -p 'last month' A final total is displayed, use `--no-total` to suppress this. Also, the -`--depth` option shows accounts only to the specified depth, useful for an overview: +`--depth N` option shows accounts only to the specified depth, useful for +an overview: $ for y in 2006 2007 2008 2009 2010; do echo; echo $y; hledger -f $y.ledger balance ^expenses --depth 2; done With `--flat`, a non-hierarchical list of full account names is displayed instead. This mode shows just the accounts actually contributing to the balance, making the arithmetic a little more obvious to non-hledger users. +In this mode you can also use `--drop N` to elide the first few account +name components. ##### chart diff --git a/hledger-lib/Hledger/Data/AccountName.hs b/hledger-lib/Hledger/Data/AccountName.hs index b877a68b1..08fbed7d2 100644 --- a/hledger-lib/Hledger/Data/AccountName.hs +++ b/hledger-lib/Hledger/Data/AccountName.hs @@ -32,6 +32,9 @@ accountNameLevel :: AccountName -> Int accountNameLevel "" = 0 accountNameLevel a = length (filter (==acctsepchar) a) + 1 +accountNameDrop :: Int -> AccountName -> AccountName +accountNameDrop n = accountNameFromComponents . drop n . accountNameComponents + -- | ["a:b:c","d:e"] -> ["a","a:b","a:b:c","d","d:e"] expandAccountNames :: [AccountName] -> [AccountName] expandAccountNames as = nub $ concatMap expand as