From 855d4e113135d16b1cae80486efc1c00b6e62c9c Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 5 Dec 2014 12:56:33 -0800 Subject: [PATCH] balance: with --tree and --flat, use the last (fix #219) --- .../Hledger/Reports/MultiBalanceReports.hs | 2 +- hledger-lib/Hledger/Reports/ReportOptions.hs | 29 +++++++++++++++---- hledger/Hledger/Cli/Print.hs | 2 +- tests/balance/219.test | 23 +++++++++++++++ 4 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 tests/balance/219.test diff --git a/hledger-lib/Hledger/Reports/MultiBalanceReports.hs b/hledger-lib/Hledger/Reports/MultiBalanceReports.hs index 44fbaf7d0..57bb6acad 100644 --- a/hledger-lib/Hledger/Reports/MultiBalanceReports.hs +++ b/hledger-lib/Hledger/Reports/MultiBalanceReports.hs @@ -122,7 +122,7 @@ multiBalanceReport opts q j = MultiBalanceReport (displayspans, items, totals) (startbalanceitems,_) = dbg "starting balance report" $ balanceReport opts' precedingq j where opts' | tree_ opts = opts{no_elide_=True} - | otherwise = opts{flat_=True} + | otherwise = opts{accountlistmode_=ALFlat} startingBalanceFor a = fromMaybe nullmixedamt $ lookup a startacctbals startAccts = dbg "startAccts" $ map fst startacctbals diff --git a/hledger-lib/Hledger/Reports/ReportOptions.hs b/hledger-lib/Hledger/Reports/ReportOptions.hs index c7b5f76fc..0365a3b43 100644 --- a/hledger-lib/Hledger/Reports/ReportOptions.hs +++ b/hledger-lib/Hledger/Reports/ReportOptions.hs @@ -8,9 +8,12 @@ Options common to most hledger reports. module Hledger.Reports.ReportOptions ( ReportOpts(..), BalanceType(..), + AccountListMode(..), FormatStr, defreportopts, rawOptsToReportOpts, + flat_, + tree_, dateSpanFromOpts, intervalFromOpts, clearedValueFromOpts, @@ -47,6 +50,11 @@ data BalanceType = PeriodBalance -- ^ The change of balance in each period. instance Default BalanceType where def = PeriodBalance +-- | Should accounts be displayed: in the command's default style, hierarchically, or as a flat list ? +data AccountListMode = ALDefault | ALTree | ALFlat deriving (Eq, Show, Data, Typeable) + +instance Default AccountListMode where def = ALDefault + -- | Standard options for customising report filtering and output, -- corresponding to hledger's command-line options and query language -- arguments. Used in hledger-lib and above. @@ -75,8 +83,7 @@ data ReportOpts = ReportOpts { ,related_ :: Bool -- balance ,balancetype_ :: BalanceType - ,flat_ :: Bool -- mutually - ,tree_ :: Bool -- exclusive + ,accountlistmode_ :: AccountListMode ,drop_ :: Int ,no_total_ :: Bool } deriving (Show, Data, Typeable) @@ -110,7 +117,6 @@ defreportopts = ReportOpts def def def - def rawOptsToReportOpts :: RawOpts -> IO ReportOpts rawOptsToReportOpts rawopts = do @@ -138,12 +144,18 @@ rawOptsToReportOpts rawopts = do ,average_ = boolopt "average" rawopts ,related_ = boolopt "related" rawopts ,balancetype_ = balancetypeopt rawopts - ,flat_ = boolopt "flat" rawopts - ,tree_ = boolopt "tree" rawopts + ,accountlistmode_ = accountlistmodeopt rawopts ,drop_ = intopt "drop" rawopts ,no_total_ = boolopt "no-total" rawopts } +accountlistmodeopt :: RawOpts -> AccountListMode +accountlistmodeopt rawopts = + case reverse $ filter (`elem` ["tree","flat"]) $ map fst rawopts of + ("tree":_) -> ALTree + ("flat":_) -> ALFlat + _ -> ALDefault + balancetypeopt :: RawOpts -> BalanceType balancetypeopt rawopts | length [o | o <- ["cumulative","historical"], isset o] > 1 @@ -181,6 +193,13 @@ maybeperiodopt d rawopts = Just $ parsePeriodExpr d s +-- | Legacy-compatible convenience aliases for accountlistmode_. +tree_ :: ReportOpts -> Bool +tree_ = (==ALTree) . accountlistmode_ + +flat_ :: ReportOpts -> Bool +flat_ = (==ALFlat) . accountlistmode_ + -- | Figure out the date span we should report on, based on any -- begin/end/period options provided. A period option will cause begin and -- end options to be ignored. diff --git a/hledger/Hledger/Cli/Print.hs b/hledger/Hledger/Cli/Print.hs index 864273b5f..8e2fc2c05 100644 --- a/hledger/Hledger/Cli/Print.hs +++ b/hledger/Hledger/Cli/Print.hs @@ -38,7 +38,7 @@ print' opts@CliOpts{reportopts_=ropts} j = do let q = queryFromOpts d ropts fmt = outputFormatFromOpts opts (render, ropts') = case fmt of - "csv" -> ((++"\n") . printCSV . entriesReportAsCsv, ropts{flat_=True}) + "csv" -> ((++"\n") . printCSV . entriesReportAsCsv, ropts{accountlistmode_=ALFlat}) _ -> (entriesReportAsText, ropts) writeOutput opts $ render $ entriesReport ropts' q j diff --git a/tests/balance/219.test b/tests/balance/219.test new file mode 100644 index 000000000..e4dd34a74 --- /dev/null +++ b/tests/balance/219.test @@ -0,0 +1,23 @@ +# issue 219, --tree and --flat flags should override each other cleanly +# 1. multiple flags ending with --flat, equivalent to --flat +hledgerdev -f balance-multicol.journal bal -MEH --no-total date:2013/1 --tree --flat +>>> +Ending balances (historical) in 2013/01: + + || 2013/01/31 +=================++============= + assets:checking || 10 + +>>>= 0 + +# 2. multiple flags ending with --tree, equivalent to --tree +hledgerdev -f balance-multicol.journal bal -MEH --no-total date:2013/1 --flat --tree +>>> +Ending balances (historical) in 2013/01: + + || 2013/01/31 +============++============= + assets || 10 + checking || 10 + +>>>= 0