From 7ed0705398840f2785b1009971db4e30b888fe4a Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Mon, 9 Sep 2013 18:25:53 -0700 Subject: [PATCH] register: `--average/-A` shows a running average, like ledger --- MANUAL.md | 8 +++++--- NEWS.md | 1 + hledger-lib/Hledger/Reports.hs | 16 ++++++++++------ hledger/Hledger/Cli/Options.hs | 2 ++ 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/MANUAL.md b/MANUAL.md index e391a295a..16eead7fa 100644 --- a/MANUAL.md +++ b/MANUAL.md @@ -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. diff --git a/NEWS.md b/NEWS.md index 19fb75bed..ee1d1fe0b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/hledger-lib/Hledger/Reports.hs b/hledger-lib/Hledger/Reports.hs index 0ad7d0b27..de7e4422d 100644 --- a/hledger-lib/Hledger/Reports.hs +++ b/hledger-lib/Hledger/Reports.hs @@ -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 diff --git a/hledger/Hledger/Cli/Options.hs b/hledger/Hledger/Cli/Options.hs index 306bdf499..95cf5dbae 100644 --- a/hledger/Hledger/Cli/Options.hs +++ b/hledger/Hledger/Cli/Options.hs @@ -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 }