From 89513fc2564aee5fc50e03bbfa773a499d793de0 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Thu, 25 Jun 2015 16:31:57 -0700 Subject: [PATCH] stats: support -o/--outputfile, useful for benchmarks --- doc/manual.md | 5 ++++- hledger/Hledger/Cli/Stats.hs | 10 ++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/doc/manual.md b/doc/manual.md index b6ae88c3c..9822965e4 100644 --- a/doc/manual.md +++ b/doc/manual.md @@ -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} diff --git a/hledger/Hledger/Cli/Stats.hs b/hledger/Hledger/Cli/Stats.hs index e50238774..b48b901d4 100644 --- a/hledger/Hledger/Cli/Stats.hs +++ b/hledger/Hledger/Cli/Stats.hs @@ -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 =