stats: support -o/--outputfile, useful for benchmarks

This commit is contained in:
Simon Michael 2015-06-25 16:31:57 -07:00
parent 01db48fcfc
commit 89513fc256
2 changed files with 10 additions and 5 deletions

View File

@ -1587,7 +1587,7 @@ $ hledger balance -o - # write to stdout (the default)
$ hledger balance -o FILE # write to FILE
```
The balance, print and register commands can write their output to a
The balance, print, register and stats commands can write their output to a
destination other than the console. This is controlled by the
`-o/--output-file` option.
@ -1852,6 +1852,9 @@ The stats command displays summary information for the whole journal, or
a matched part of it. With a [reporting interval](#reporting-interval),
it shows a report for each report period.
The stats command also supports `-o/--output-file`
for controlling [output destination](#output-destination).
#### test
```{.shell .right}

View File

@ -23,13 +23,15 @@ import qualified Data.Map as Map
import Hledger
import Hledger.Cli.Options
import Prelude hiding (putStr)
import Hledger.Utils.UTF8IOCompat (putStr)
import Hledger.Cli.Utils (writeOutput)
statsmode = (defCommandMode $ ["stats"] ++ aliases) {
modeHelp = "show some journal statistics" `withAliases` aliases
,modeGroupFlags = Group {
groupUnnamed = []
groupUnnamed = [
flagReq ["output-file","o"] (\s opts -> Right $ setopt "output-file" s opts) "FILE[.FMT]" "write output to FILE instead of stdout. A recognised FMT suffix influences the format."
]
,groupHidden = []
,groupNamed = [generalflagsgroup1]
}
@ -39,7 +41,7 @@ statsmode = (defCommandMode $ ["stats"] ++ aliases) {
-- like Register.summarisePostings
-- | Print various statistics for the journal.
stats :: CliOpts -> Journal -> IO ()
stats CliOpts{reportopts_=reportopts_} j = do
stats opts@CliOpts{reportopts_=reportopts_} j = do
d <- getCurrentDay
let q = queryFromOpts d reportopts_
l = ledgerFromJournal q j
@ -47,7 +49,7 @@ stats CliOpts{reportopts_=reportopts_} j = do
intervalspans = splitSpan (intervalFromOpts reportopts_) reportspan
showstats = showLedgerStats l d
s = intercalate "\n" $ map showstats intervalspans
putStr s
writeOutput opts s
showLedgerStats :: Ledger -> Day -> DateSpan -> String
showLedgerStats l today span =