From 2a5f7819af47effef70acdb85a379a341f20c329 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Mon, 24 Feb 2020 17:48:54 -0800 Subject: [PATCH] bs,cf,is,bal,print,reg: support json output format --- hledger-lib/Hledger/Reports/ReportTypes.hs | 8 ++++++-- hledger/Hledger/Cli/Commands/Balance.hs | 8 ++++++-- hledger/Hledger/Cli/Commands/Balance.md | 7 +++++-- hledger/Hledger/Cli/Commands/Balancesheet.md | 6 ++++-- hledger/Hledger/Cli/Commands/Balancesheetequity.md | 7 +++++++ hledger/Hledger/Cli/Commands/Cashflow.md | 6 ++++-- hledger/Hledger/Cli/Commands/Incomestatement.md | 6 ++++-- hledger/Hledger/Cli/Commands/Print.hs | 2 ++ hledger/Hledger/Cli/Commands/Print.md | 7 ++++++- hledger/Hledger/Cli/Commands/Register.hs | 2 ++ hledger/Hledger/Cli/Commands/Register.md | 6 ++++-- hledger/Hledger/Cli/CompoundBalanceCommand.hs | 4 +++- 12 files changed, 53 insertions(+), 16 deletions(-) diff --git a/hledger-lib/Hledger/Reports/ReportTypes.hs b/hledger-lib/Hledger/Reports/ReportTypes.hs index e9063724c..63f77efdc 100644 --- a/hledger-lib/Hledger/Reports/ReportTypes.hs +++ b/hledger-lib/Hledger/Reports/ReportTypes.hs @@ -1,6 +1,8 @@ {- | New common report types, used by the BudgetReport for now, perhaps all reports later. -} +{-# LANGUAGE DeriveAnyClass #-} +{-# LANGUAGE DeriveGeneric #-} module Hledger.Reports.ReportTypes ( PeriodicReport(..) @@ -17,7 +19,9 @@ module Hledger.Reports.ReportTypes , prNormaliseSign ) where +import Data.Aeson import Data.Decimal +import GHC.Generics (Generic) import Hledger.Data type Percentage = Decimal @@ -63,7 +67,7 @@ data PeriodicReport a b = -- significant. Usually displayed as report columns. , prRows :: [PeriodicReportRow a b] -- One row per account in the report. , prTotals :: PeriodicReportRow () b -- The grand totals row. - } deriving (Show) + } deriving (Show, Generic, ToJSON) data PeriodicReportRow a b = PeriodicReportRow @@ -72,7 +76,7 @@ data PeriodicReportRow a b = , prrAmounts :: [b] -- The data value for each subperiod. , prrTotal :: b -- The total of this row's values. , prrAverage :: b -- The average of this row's values. - } deriving (Show) + } deriving (Show, Generic, ToJSON) -- | Figure out the overall date span of a PeridicReport periodicReportSpan :: PeriodicReport a b -> DateSpan diff --git a/hledger/Hledger/Cli/Commands/Balance.hs b/hledger/Hledger/Cli/Commands/Balance.hs index 747bbdd18..890a462a1 100644 --- a/hledger/Hledger/Cli/Commands/Balance.hs +++ b/hledger/Hledger/Cli/Commands/Balance.hs @@ -253,6 +253,7 @@ module Hledger.Cli.Commands.Balance ( ,tests_Balance ) where +import Data.Aeson (toJSON) import Data.List import Data.Maybe --import qualified Data.Map as Map @@ -318,6 +319,7 @@ balance opts@CliOpts{rawopts_=rawopts,reportopts_=ropts@ReportOpts{..}} j = do assrt = not $ ignore_assertions_ $ inputopts_ opts render = case fmt of "txt" -> budgetReportAsText ropts + "json" -> (++"\n") . pshow . toJSON -- XXX pshow for pretty output, but it may generate some junk _ -> const $ error' $ unsupportedOutputFormatError fmt writeOutput opts $ render budgetreport @@ -326,8 +328,9 @@ balance opts@CliOpts{rawopts_=rawopts,reportopts_=ropts@ReportOpts{..}} j = do let report = multiBalanceReport ropts (queryFromOpts d ropts) j render = case fmt of "txt" -> multiBalanceReportAsText ropts - "csv" -> (++ "\n") . printCSV . multiBalanceReportAsCsv ropts - "html" -> (++ "\n") . TL.unpack . L.renderText . multiBalanceReportAsHtml ropts + "csv" -> (++"\n") . printCSV . multiBalanceReportAsCsv ropts + "html" -> (++"\n") . TL.unpack . L.renderText . multiBalanceReportAsHtml ropts + "json" -> (++"\n") . pshow . toJSON -- XXX pshow for pretty output, but it may generate some junk _ -> const $ error' $ unsupportedOutputFormatError fmt writeOutput opts $ render report @@ -342,6 +345,7 @@ balance opts@CliOpts{rawopts_=rawopts,reportopts_=ropts@ReportOpts{..}} j = do render = case fmt of "txt" -> balanceReportAsText "csv" -> \ropts r -> (++ "\n") $ printCSV $ balanceReportAsCsv ropts r + "json" -> const $ (++"\n") . pshow . toJSON -- XXX pshow for pretty output, but it may generate some junk _ -> const $ error' $ unsupportedOutputFormatError fmt writeOutput opts $ render ropts report diff --git a/hledger/Hledger/Cli/Commands/Balance.md b/hledger/Hledger/Cli/Commands/Balance.md index 559e9efb9..2da7bf0c1 100644 --- a/hledger/Hledger/Cli/Commands/Balance.md +++ b/hledger/Hledger/Cli/Commands/Balance.md @@ -511,5 +511,8 @@ Budget performance in 2019/01: ### Output format -The balance command supports [output destination](hledger.html#output-destination) and [output format](hledger.html#output-format) selection. - +This command also supports the +[output destination](hledger.html#output-destination) and +[output format](hledger.html#output-format) options +The output formats supported are +`txt`, `csv`, (multicolumn non-budget reports only) `html`, and (experimental) `json`. diff --git a/hledger/Hledger/Cli/Commands/Balancesheet.md b/hledger/Hledger/Cli/Commands/Balancesheet.md index f9064021b..adb55603c 100644 --- a/hledger/Hledger/Cli/Commands/Balancesheet.md +++ b/hledger/Hledger/Cli/Commands/Balancesheet.md @@ -43,6 +43,8 @@ dates (and `-T/--row-total`, since summing end balances generally does not make Instead of absolute values [percentages](#percentages) can be displayed with `-%`. -This command also supports +This command also supports the [output destination](hledger.html#output-destination) and -[output format](hledger.html#output-format) selection. +[output format](hledger.html#output-format) options +The output formats supported are +`txt`, `csv`, `html`, and (experimental) `json`. diff --git a/hledger/Hledger/Cli/Commands/Balancesheetequity.md b/hledger/Hledger/Cli/Commands/Balancesheetequity.md index 81d359b69..f5d48be6b 100644 --- a/hledger/Hledger/Cli/Commands/Balancesheetequity.md +++ b/hledger/Hledger/Cli/Commands/Balancesheetequity.md @@ -30,3 +30,10 @@ Total: -------------------- 0 ``` + + +This command also supports the +[output destination](hledger.html#output-destination) and +[output format](hledger.html#output-format) options +The output formats supported are +`txt`, `csv`, `html`, and (experimental) `json`. diff --git a/hledger/Hledger/Cli/Commands/Cashflow.md b/hledger/Hledger/Cli/Commands/Cashflow.md index 566f5613a..eb84a09d3 100644 --- a/hledger/Hledger/Cli/Commands/Cashflow.md +++ b/hledger/Hledger/Cli/Commands/Cashflow.md @@ -34,6 +34,8 @@ you can alter the report mode with `--change`/`--cumulative`/`--historical`. Instead of absolute values [percentages](#percentages) can be displayed with `-%`. -This command also supports +This command also supports the [output destination](hledger.html#output-destination) and -[output format](hledger.html#output-format) selection. +[output format](hledger.html#output-format) options +The output formats supported are +`txt`, `csv`, `html`, and (experimental) `json`. diff --git a/hledger/Hledger/Cli/Commands/Incomestatement.md b/hledger/Hledger/Cli/Commands/Incomestatement.md index 79c560c1d..865d0dd81 100644 --- a/hledger/Hledger/Cli/Commands/Incomestatement.md +++ b/hledger/Hledger/Cli/Commands/Incomestatement.md @@ -45,6 +45,8 @@ you can alter the report mode with `--change`/`--cumulative`/`--historical`. Instead of absolute values [percentages](#percentages) can be displayed with `-%`. -This command also supports +This command also supports the [output destination](hledger.html#output-destination) and -[output format](hledger.html#output-format) selection. +[output format](hledger.html#output-format) options +The output formats supported are +`txt`, `csv`, `html`, and (experimental) `json`. diff --git a/hledger/Hledger/Cli/Commands/Print.hs b/hledger/Hledger/Cli/Commands/Print.hs index 923eb471b..32631b6af 100644 --- a/hledger/Hledger/Cli/Commands/Print.hs +++ b/hledger/Hledger/Cli/Commands/Print.hs @@ -15,6 +15,7 @@ module Hledger.Cli.Commands.Print ( ) where +import Data.Aeson (toJSON) import Data.Maybe (isJust) import Data.Text (Text) import qualified Data.Text as T @@ -56,6 +57,7 @@ printEntries opts@CliOpts{reportopts_=ropts} j = do render = case fmt of "txt" -> entriesReportAsText opts "csv" -> (++"\n") . printCSV . entriesReportAsCsv + "json" -> (++"\n") . pshow . toJSON -- XXX pshow for pretty output, but it may generate some junk _ -> const $ error' $ unsupportedOutputFormatError fmt writeOutput opts $ render $ entriesReport ropts q j diff --git a/hledger/Hledger/Cli/Commands/Print.md b/hledger/Hledger/Cli/Commands/Print.md index 124fbc2ee..64d063814 100644 --- a/hledger/Hledger/Cli/Commands/Print.md +++ b/hledger/Hledger/Cli/Commands/Print.md @@ -71,7 +71,12 @@ This assumes that transactions added to FILE always have same or increasing date and that transactions on the same day do not get reordered. See also the [import](#import) command. -This command also supports [output destination](hledger.html#output-destination) and [output format](hledger.html#output-format) selection. +This command also supports the +[output destination](hledger.html#output-destination) and +[output format](hledger.html#output-format) options +The output formats supported are +`txt`, `csv`, and (experimental) `json`. + Here's an example of print's CSV output: ```shell diff --git a/hledger/Hledger/Cli/Commands/Register.hs b/hledger/Hledger/Cli/Commands/Register.hs index 0d1d405f3..111df2fc2 100644 --- a/hledger/Hledger/Cli/Commands/Register.hs +++ b/hledger/Hledger/Cli/Commands/Register.hs @@ -18,6 +18,7 @@ module Hledger.Cli.Commands.Register ( ,tests_Register ) where +import Data.Aeson (toJSON) import Data.List import Data.Maybe -- import Data.Text (Text) @@ -60,6 +61,7 @@ register opts@CliOpts{reportopts_=ropts} j = do let fmt = outputFormatFromOpts opts render | fmt=="txt" = postingsReportAsText | fmt=="csv" = const ((++"\n") . printCSV . postingsReportAsCsv) + | fmt=="json" = const ((++"\n") . pshow . toJSON) -- XXX pshow for pretty output, but it may generate some junk | otherwise = const $ error' $ unsupportedOutputFormatError fmt writeOutput opts $ render opts $ postingsReport ropts (queryFromOpts d ropts) j diff --git a/hledger/Hledger/Cli/Commands/Register.md b/hledger/Hledger/Cli/Commands/Register.md index 87ed44786..dd8383e18 100644 --- a/hledger/Hledger/Cli/Commands/Register.md +++ b/hledger/Hledger/Cli/Commands/Register.md @@ -117,7 +117,9 @@ $ hledger reg -w 100,40 # set overall width 100, description width 40 $ hledger reg -w $COLUMNS,40 # use terminal width, & description width 40 ``` -This command also supports +This command also supports the [output destination](hledger.html#output-destination) and -[output format](hledger.html#output-format) selection. +[output format](hledger.html#output-format) options +The output formats supported are +`txt`, `csv`, and (experimental) `json`. diff --git a/hledger/Hledger/Cli/CompoundBalanceCommand.hs b/hledger/Hledger/Cli/CompoundBalanceCommand.hs index b055b39ab..b76af8c60 100644 --- a/hledger/Hledger/Cli/CompoundBalanceCommand.hs +++ b/hledger/Hledger/Cli/CompoundBalanceCommand.hs @@ -13,6 +13,7 @@ module Hledger.Cli.CompoundBalanceCommand ( ,compoundBalanceCommand ) where +import Data.Aeson (toJSON) import Data.List (foldl') import Data.Maybe import qualified Data.Text as TS @@ -238,7 +239,8 @@ compoundBalanceCommand CompoundBalanceCommandSpec{..} opts@CliOpts{reportopts_=r case fmt of "txt" -> compoundBalanceReportAsText ropts' cbr "csv" -> printCSV (compoundBalanceReportAsCsv ropts cbr) ++ "\n" - "html" -> (++ "\n") $ TL.unpack $ L.renderText $ compoundBalanceReportAsHtml ropts cbr + "html" -> (++"\n") $ TL.unpack $ L.renderText $ compoundBalanceReportAsHtml ropts cbr + "json" -> (++"\n") $ pshow $ toJSON cbr -- XXX pshow for pretty output, but it may generate some junk _ -> error' $ unsupportedOutputFormatError fmt -- | Summarise one or more (inclusive) end dates, in a way that's