register: --average/-A shows a running average, like ledger

This commit is contained in:
Simon Michael 2013-09-09 18:25:53 -07:00
parent 3d205ec03f
commit 7ed0705398
4 changed files with 18 additions and 9 deletions

View File

@ -720,15 +720,17 @@ summary postings within each interval:
$ hledger register --monthly rent
$ hledger register --monthly -E food --depth 4
The `--average`/`-A` flag shows a running average instead of the running total.
The `--related`/`-r` flag shows the *other* postings in the transactions
of the postings which would normally be shown.
The `--width`/`-w` option adjusts the width of the output. By default,
this is 80 characters. To allow more space for descriptions and account
names, use `-w` to increase the width to 120 characters, or `-wN` to set
any desired width (at least 50 recommended, with no space before the N -
eg `-w200` or `--width=200`,
The `--related`/`-r` flag shows the *other* postings in the transactions
of the postings which would normally be shown.
#### balance
The balance command displays accounts and their balances, indented to show the account hierarchy.

View File

@ -6,6 +6,7 @@ title: hledger news
## unreleased
- register: `--average/-A` shows a running average, like ledger
- queries: `sym:REGEXP` matches (whole) commodity symbols
- queries: `amt` now uses the = operator by default, eg amt:50 finds amounts equal to 50
- don't break when there are non-ascii characters in CSV files

View File

@ -94,6 +94,7 @@ data ReportOpts = ReportOpts {
,yearly_ :: Bool
,format_ :: Maybe FormatStr
,related_ :: Bool
,average_ :: Bool
,query_ :: String -- all arguments, as a string
} deriving (Show)
@ -124,6 +125,7 @@ defreportopts = ReportOpts
def
def
def
def
instance Default ReportOpts where def = defreportopts
@ -254,14 +256,14 @@ type PostingsReport = (String -- label for the running balance col
type PostingsReportItem = (Maybe Day -- posting date, if this is the first posting in a transaction or if it's different from the previous posting's date
,Maybe String -- transaction description, if this is the first posting in a transaction
,Posting -- the posting, possibly with account name depth-clipped
,MixedAmount -- the running total after this posting
,MixedAmount -- the running total after this posting (or with --average, the running average)
)
-- | Select postings from the journal and add running balance and other
-- information to make a postings report. Used by eg hledger's register command.
postingsReport :: ReportOpts -> Query -> Journal -> PostingsReport
postingsReport opts q j = -- trace ("q: "++show q++"\nq': "++show q') $
(totallabel, postingsReportItems ps nullposting wd depth startbal (+))
(totallabel, postingsReportItems ps nullposting wd depth startbal runningcalcfn 1)
where
ps | interval == NoInterval = displayableps
| otherwise = summarisePostingsByInterval interval depth empty reportspan displayableps
@ -293,14 +295,16 @@ postingsReport opts q j = -- trace ("q: "++show q++"\nq': "++show q') $
reportspan | empty = requestedspan `orDatesFrom` journalspan
| otherwise = requestedspan `spanIntersect` matchedspan
startbal = sumPostings precedingps
runningcalcfn | average_ opts = \i avg amt -> avg + (amt - avg) `divideMixedAmount` (fromIntegral i)
| otherwise = \_ bal amt -> bal + amt
totallabel = "Total"
balancelabel = "Balance"
-- | Generate postings report line items.
postingsReportItems :: [Posting] -> Posting -> WhichDate -> Int -> MixedAmount -> (MixedAmount -> MixedAmount -> MixedAmount) -> [PostingsReportItem]
postingsReportItems [] _ _ _ _ _ = []
postingsReportItems (p:ps) pprev wd d b sumfn = i:(postingsReportItems ps p wd d b' sumfn)
postingsReportItems :: [Posting] -> Posting -> WhichDate -> Int -> MixedAmount -> (Int -> MixedAmount -> MixedAmount -> MixedAmount) -> Int -> [PostingsReportItem]
postingsReportItems [] _ _ _ _ _ _ = []
postingsReportItems (p:ps) pprev wd d b runningcalcfn itemnum = i:(postingsReportItems ps p wd d b' runningcalcfn (itemnum+1))
where
i = mkpostingsReportItem showdate showdesc wd p' b'
showdate = isfirstintxn || isdifferentdate
@ -309,7 +313,7 @@ postingsReportItems (p:ps) pprev wd d b sumfn = i:(postingsReportItems ps p wd d
isdifferentdate = case wd of PrimaryDate -> postingDate p /= postingDate pprev
SecondaryDate -> postingDate2 p /= postingDate2 pprev
p' = p{paccount=clipAccountName d $ paccount p}
b' = b `sumfn` pamount p
b' = runningcalcfn itemnum b (pamount p)
-- | Generate one postings report line item, containing the posting,
-- the current running balance, and optionally the posting date and/or

View File

@ -215,6 +215,7 @@ postingsmode = (commandmode ["register","postings"]) {
,modeGroupFlags = Group {
groupUnnamed = [
flagOpt (show defaultWidthWithFlag) ["width","w"] (\s opts -> Right $ setopt "width" s opts) "N" "increase or set the output width (default: 80)"
,flagNone ["average","A"] (\opts -> setboolopt "average" opts) "show the running average instead of the running total"
,flagNone ["related","r"] (\opts -> setboolopt "related" opts) "show the other postings in the transactions of those that would have been shown"
]
,groupHidden = []
@ -350,6 +351,7 @@ toCliOpts rawopts = do
,quarterly_ = boolopt "quarterly" rawopts
,yearly_ = boolopt "yearly" rawopts
,format_ = maybestringopt "format" rawopts
,average_ = boolopt "average" rawopts -- register
,related_ = boolopt "related" rawopts -- register
,query_ = unwords $ listofstringopt "args" rawopts
}