balance: --flat provides a simple non-hierarchical format

This commit is contained in:
Simon Michael 2010-05-24 20:27:52 +00:00
parent 927948a644
commit 5d7f141239
3 changed files with 36 additions and 8 deletions

View File

@ -123,21 +123,25 @@ showBalanceReport opts filterspec j = acctsstr ++ totalstr
l = journalToLedger filterspec j l = journalToLedger filterspec j
acctsstr = unlines $ map showacct interestingaccts acctsstr = unlines $ map showacct interestingaccts
where where
showacct = showInterestingAccount l interestingaccts showacct = showInterestingAccount opts l interestingaccts
interestingaccts = filter (isInteresting opts l) acctnames interestingaccts = filter (isInteresting opts l) acctnames
acctnames = sort $ tail $ flatten $ treemap aname accttree acctnames = sort $ tail $ flatten $ treemap aname accttree
accttree = ledgerAccountTree (fromMaybe 99999 $ depthFromOpts opts) l accttree = ledgerAccountTree (fromMaybe 99999 $ depthFromOpts opts) l
totalstr | NoTotal `elem` opts = "" totalstr | NoTotal `elem` opts = ""
| notElem Empty opts && isZeroMixedAmount total = ""
| otherwise = printf "--------------------\n%s\n" $ padleft 20 $ showMixedAmountWithoutPrice total | otherwise = printf "--------------------\n%s\n" $ padleft 20 $ showMixedAmountWithoutPrice total
where where
total = sum $ map abalance $ ledgerTopAccounts l total = sum $ map abalance $ ledgerTopAccounts l
-- | Display one line of the balance report with appropriate indenting and eliding. -- | Display one line of the balance report with appropriate indenting and eliding.
showInterestingAccount :: Ledger -> [AccountName] -> AccountName -> String showInterestingAccount :: [Opt] -> Ledger -> [AccountName] -> AccountName -> String
showInterestingAccount l interestingaccts a = concatTopPadded [amt, " ", depthspacer ++ partialname] showInterestingAccount opts l interestingaccts a = concatTopPadded [amt, " ", name]
where where
amt = padleft 20 $ showMixedAmountWithoutPrice $ abalance $ ledgerAccount l a amt = padleft 20 $ showMixedAmountWithoutPrice bal
bal | Flat `elem` opts = exclusiveBalance acct
| otherwise = abalance acct
acct = ledgerAccount l a
name | Flat `elem` opts = a
| otherwise = depthspacer ++ partialname
parents = parentAccountNames a parents = parentAccountNames a
interestingparents = filter (`elem` interestingaccts) parents interestingparents = filter (`elem` interestingaccts) parents
depthspacer = replicate (indentperlevel * length interestingparents) ' ' depthspacer = replicate (indentperlevel * length interestingparents) ' '
@ -147,9 +151,23 @@ showInterestingAccount l interestingaccts a = concatTopPadded [amt, " ", depths
partialname = accountNameFromComponents $ reverse (map accountLeafName ps) ++ [accountLeafName a] partialname = accountNameFromComponents $ reverse (map accountLeafName ps) ++ [accountLeafName a]
where ps = takeWhile boring parents where boring = not . (`elem` interestingparents) where ps = takeWhile boring parents where boring = not . (`elem` interestingparents)
exclusiveBalance :: Account -> MixedAmount
exclusiveBalance = sumPostings . apostings
-- | Is the named account considered interesting for this ledger's balance report ? -- | Is the named account considered interesting for this ledger's balance report ?
isInteresting :: [Opt] -> Ledger -> AccountName -> Bool isInteresting :: [Opt] -> Ledger -> AccountName -> Bool
isInteresting opts l a isInteresting opts l a | Flat `elem` opts = isInterestingFlat opts l a
| otherwise = isInterestingIndented opts l a
isInterestingFlat :: [Opt] -> Ledger -> AccountName -> Bool
isInterestingFlat opts l a = notempty || emptyflag
where
acct = ledgerAccount l a
notempty = not $ isZeroMixedAmount $ exclusiveBalance acct
emptyflag = Empty `elem` opts
isInterestingIndented :: [Opt] -> Ledger -> AccountName -> Bool
isInterestingIndented opts l a
| numinterestingsubs==1 && not atmaxdepth = notlikesub | numinterestingsubs==1 && not atmaxdepth = notlikesub
| otherwise = notzero || emptyflag | otherwise = notzero || emptyflag
where where

View File

@ -75,6 +75,7 @@ options = [
,Option "" ["effective"] (NoArg Effective) "use transactions' effective dates, if any" ,Option "" ["effective"] (NoArg Effective) "use transactions' effective dates, if any"
,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 "" ["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"
@ -113,6 +114,7 @@ data Opt =
Effective | Effective |
Empty | Empty |
Real | Real |
Flat |
NoTotal | NoTotal |
SubTotal | SubTotal |
WeeklyOpt | WeeklyOpt |

View File

@ -224,6 +224,7 @@ Here is the command-line help:
--effective use transactions' effective dates, if any --effective use transactions' effective dates, if any
-E --empty show empty/zero things which are normally elided -E --empty show empty/zero things which are normally elided
-R --real report only on real (non-virtual) transactions -R --real report only on real (non-virtual) transactions
--flat balance report: show full account names, unindented
--no-total balance report: hide the final total --no-total balance report: hide the final total
-W --weekly register report: show weekly summary -W --weekly register report: show weekly summary
-M --monthly register report: show monthly summary -M --monthly register report: show monthly summary
@ -274,14 +275,21 @@ Examples:
##### balance ##### balance
The balance command displays accounts and their balances. The balance command displays accounts and their balances, indented to show the account hierarchy.
Examples: Examples:
$ hledger balance $ hledger balance
$ 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
`--depth` 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
instead. This mode shows just the accounts actually contributing to the
balance, making the arithmetic a little more obvious to non-hledger users.
##### chart ##### chart
The chart command saves a pie chart of your top account balances to an The chart command saves a pie chart of your top account balances to an