From 86b510917dae4792a1bd4036713ec134961d7e4c Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Sat, 22 Nov 2008 13:11:54 +0000 Subject: [PATCH] quick --depth option, a hledger feature --- BalanceCommand.hs | 2 +- NOTES | 1 - Options.hs | 13 +++++++++++++ Tests.hs | 12 ++++++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/BalanceCommand.hs b/BalanceCommand.hs index 6e8ce56b4..7eb58561b 100644 --- a/BalanceCommand.hs +++ b/BalanceCommand.hs @@ -129,7 +129,7 @@ showBalanceReport opts args l = acctsstr ++ (if collapse then "" else totalstr) sub = SubTotal `elem` opts empty = Empty `elem` opts collapse = Collapse `elem` opts - maxdepth = 9999 + maxdepth = fromMaybe 9999 $ depthFromOpts opts totalstr = if isZeroMixedAmount total then "" else printf "--------------------\n%20s\n" $ showMixedAmount total diff --git a/NOTES b/NOTES index baa5ac06a..9b85176b2 100644 --- a/NOTES +++ b/NOTES @@ -16,7 +16,6 @@ implementations were its consequences." --Niklaus Wirth *** commodity @ rate, for tracking client hours in main ledger *** actual/effective entry & txn dates, for ... *** --display, for reconciling recent transactions with real balance -*** depth control *** more ledger features from README *** new features **** option for strict ledger-compatible output (or, compare xml ?) diff --git a/Options.hs b/Options.hs index f22907932..d1f3bb0c1 100644 --- a/Options.hs +++ b/Options.hs @@ -27,6 +27,7 @@ options = [ Option ['b'] ["begin"] (ReqArg Begin "YYYY/MM/DD") "report on entries on or after this date", Option ['e'] ["end"] (ReqArg End "YYYY/MM/DD") "report on entries prior to this date", Option ['C'] ["cleared"] (NoArg Cleared) "report only on cleared entries", + Option [] ["depth"] (ReqArg Depth "N") "balance report: maximum account depth to show", Option ['E'] ["empty"] (NoArg Empty) "balance report: show accounts with zero balance", Option ['R'] ["real"] (NoArg Real) "report only on real (non-virtual) transactions", Option ['n'] ["collapse"] (NoArg Collapse) "balance report: no grand total", @@ -41,6 +42,7 @@ data Opt = Begin String | End String | Cleared | + Depth String | Empty | Real | Collapse | @@ -109,6 +111,17 @@ endDateFromOpts opts = getenddate _ = [] defaultdate = "" +-- | Get the value of the depth option, if any. +depthFromOpts :: [Opt] -> Maybe Int +depthFromOpts opts = + case depthopts of + (x:_) -> Just $ read x + _ -> Nothing + where + depthopts = concatMap getdepth opts + getdepth (Depth s) = [s] + getdepth _ = [] + -- | Gather any ledger-style account/description pattern arguments into -- two lists. These are 0 or more account patterns optionally followed by -- -- and 0 or more description patterns. diff --git a/Tests.hs b/Tests.hs index 04e949863..cfac82529 100644 --- a/Tests.hs +++ b/Tests.hs @@ -227,6 +227,18 @@ balancecommand_tests = TestList [ ([Collapse], ["cash"]) `gives` (" $-2 assets:cash\n" ++ "") + , + "balance report with --depth 1" ~: + ([SubTotal,Collapse,Depth "1"], ["assets"]) `gives` + (" $-1 assets\n" ++ + "") + , + "balance report with --depth 2" ~: + ([SubTotal,Collapse,Depth "2"], ["assets"]) `gives` + (" $-1 assets\n" ++ + " $-2 cash\n" ++ + " $1 saving\n" ++ + "") ] where gives (opts,pats) e = do l <- ledgerfromfile pats "sample.ledger"