balance: with --flat, --drop N elides leading account name components. Feature or frill ?

This commit is contained in:
Simon Michael 2010-05-24 20:45:48 +00:00
parent 5d7f141239
commit 73572e2d85
4 changed files with 18 additions and 3 deletions

View File

@ -140,7 +140,7 @@ showInterestingAccount opts l interestingaccts a = concatTopPadded [amt, " ", n
bal | Flat `elem` opts = exclusiveBalance acct bal | Flat `elem` opts = exclusiveBalance acct
| otherwise = abalance acct | otherwise = abalance acct
acct = ledgerAccount l a acct = ledgerAccount l a
name | Flat `elem` opts = a name | Flat `elem` opts = accountNameDrop (dropFromOpts opts) a
| otherwise = depthspacer ++ partialname | otherwise = depthspacer ++ partialname
parents = parentAccountNames a parents = parentAccountNames a
interestingparents = filter (`elem` interestingaccts) parents interestingparents = filter (`elem` interestingaccts) parents

View File

@ -76,6 +76,7 @@ options = [
,Option "E" ["empty"] (NoArg Empty) "show empty/zero things which are normally elided" ,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 "R" ["real"] (NoArg Real) "report only on real (non-virtual) transactions"
,Option "" ["flat"] (NoArg Flat) "balance report: show full account names, unindented" ,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 "" ["no-total"] (NoArg NoTotal) "balance report: hide the final total"
-- ,Option "s" ["subtotal"] (NoArg SubTotal) "balance report: show subaccounts" -- ,Option "s" ["subtotal"] (NoArg SubTotal) "balance report: show subaccounts"
,Option "W" ["weekly"] (NoArg WeeklyOpt) "register report: show weekly summary" ,Option "W" ["weekly"] (NoArg WeeklyOpt) "register report: show weekly summary"
@ -115,6 +116,7 @@ data Opt =
Empty | Empty |
Real | Real |
Flat | Flat |
Drop {value::String} |
NoTotal | NoTotal |
SubTotal | SubTotal |
WeeklyOpt | WeeklyOpt |
@ -212,13 +214,20 @@ intervalFromOpts opts =
periodopts = reverse $ optValuesForConstructor Period opts periodopts = reverse $ optValuesForConstructor Period opts
intervalopts = reverse $ filter (`elem` [WeeklyOpt,MonthlyOpt,QuarterlyOpt,YearlyOpt]) 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 :: [Opt] -> Maybe Int
depthFromOpts opts = listtomaybeint $ optValuesForConstructor Depth opts depthFromOpts opts = listtomaybeint $ optValuesForConstructor Depth opts
where where
listtomaybeint [] = Nothing listtomaybeint [] = Nothing
listtomaybeint vs = Just $ read $ last vs 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. -- | Get the value of the (last) display option, if any.
displayExprFromOpts :: [Opt] -> Maybe String displayExprFromOpts :: [Opt] -> Maybe String
displayExprFromOpts opts = listtomaybe $ optValuesForConstructor Display opts displayExprFromOpts opts = listtomaybe $ optValuesForConstructor Display opts

View File

@ -282,13 +282,16 @@ Examples:
$ hledger balance food -p 'last month' $ hledger balance food -p 'last month'
A final total is displayed, use `--no-total` to suppress this. Also, the 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 $ 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 With `--flat`, a non-hierarchical list of full account names is displayed
instead. This mode shows just the accounts actually contributing to the instead. This mode shows just the accounts actually contributing to the
balance, making the arithmetic a little more obvious to non-hledger users. 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 ##### chart

View File

@ -32,6 +32,9 @@ accountNameLevel :: AccountName -> Int
accountNameLevel "" = 0 accountNameLevel "" = 0
accountNameLevel a = length (filter (==acctsepchar) a) + 1 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"] -- | ["a:b:c","d:e"] -> ["a","a:b","a:b:c","d","d:e"]
expandAccountNames :: [AccountName] -> [AccountName] expandAccountNames :: [AccountName] -> [AccountName]
expandAccountNames as = nub $ concatMap expand as expandAccountNames as = nub $ concatMap expand as