From 17ab6cb0ab7de8878e669217188032818b3dca1b Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Thu, 16 Oct 2008 09:50:16 +0000 Subject: [PATCH] support --real/-R flag --- Ledger/RawLedger.hs | 16 +++++++++++++--- Options.hs | 4 +++- Utils.hs | 4 ++-- hledger.hs | 3 ++- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Ledger/RawLedger.hs b/Ledger/RawLedger.hs index afbda3035..8cd421618 100644 --- a/Ledger/RawLedger.hs +++ b/Ledger/RawLedger.hs @@ -14,6 +14,7 @@ import Ledger.AccountName import Ledger.Amount import Ledger.Entry import Ledger.Transaction +import Ledger.RawTransaction negativepatternchar = '-' @@ -43,9 +44,10 @@ rawLedgerAccountNameTree l = accountNameTreeFrom $ rawLedgerAccountNames l -- | Remove ledger entries we are not interested in. -- Keep only those which fall between the begin and end dates, and match --- the description pattern, and match the cleared flag. -filterRawLedger :: String -> String -> [String] -> Bool -> RawLedger -> RawLedger -filterRawLedger begin end pats clearedonly = +-- the description pattern, and are cleared or real if those options are active. +filterRawLedger :: String -> String -> [String] -> Bool -> Bool -> RawLedger -> RawLedger +filterRawLedger begin end pats clearedonly realonly = + filterRawLedgerTransactionsByRealness realonly . filterRawLedgerEntriesByClearedStatus clearedonly . filterRawLedgerEntriesByDate begin end . filterRawLedgerEntriesByDescription pats @@ -75,6 +77,14 @@ filterRawLedgerEntriesByClearedStatus False l = l filterRawLedgerEntriesByClearedStatus True (RawLedger ms ps es f) = RawLedger ms ps (filter estatus es) f +-- | Strip out any (virtual transactions), if the flag is true, otherwise +-- do no filtering. +filterRawLedgerTransactionsByRealness :: Bool -> RawLedger -> RawLedger +filterRawLedgerTransactionsByRealness False l = l +filterRawLedgerTransactionsByRealness True (RawLedger ms ps es f) = + RawLedger ms ps (map filtertxns es) f + where filtertxns e@Entry{etransactions=ts} = e{etransactions=filter isReal ts} + -- | Check if a set of ledger account/description patterns matches the -- given account name or entry description, applying ledger's special -- cases. diff --git a/Options.hs b/Options.hs index 5e14b00b8..1287161c5 100644 --- a/Options.hs +++ b/Options.hs @@ -25,7 +25,8 @@ options = [ Option ['f'] ["file"] (ReqArg File "FILE") "ledger file; - means use standard input", 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 transactions", + Option ['C'] ["cleared"] (NoArg Cleared) "report only on cleared entries", + Option ['R'] ["real"] (NoArg Real) "report only on real (non-virtual) transactions", Option ['s'] ["showsubs"] (NoArg ShowSubs) "in the balance report, include subaccounts", Option ['h'] ["help","usage"] (NoArg Help) "show this help", Option ['V'] ["version"] (NoArg Version) "show version" @@ -37,6 +38,7 @@ data Opt = Begin String | End String | Cleared | + Real | ShowSubs | Help | Version diff --git a/Utils.hs b/Utils.hs index d5c4f82cc..517b45541 100644 --- a/Utils.hs +++ b/Utils.hs @@ -21,7 +21,7 @@ rawledgerfromfile f = do ledgerfromfile :: FilePath -> IO Ledger ledgerfromfile f = do l <- rawledgerfromfile f - return $ cacheLedger $ filterRawLedger "" "" [] False l + return $ cacheLedger $ filterRawLedger "" "" [] False False l -- | get a RawLedger from the file your LEDGER environment variable -- variable points to or (WARNING) an empty one if there was a problem. @@ -35,7 +35,7 @@ myrawledger = do myledger :: IO Ledger myledger = do l <- myrawledger - return $ cacheLedger $ filterRawLedger "" "" [] False l + return $ cacheLedger $ filterRawLedger "" "" [] False False l -- | get a named account from your ledger file myaccount :: AccountName -> IO Account diff --git a/hledger.hs b/hledger.hs index 13bc2f77b..62a4f9a9f 100644 --- a/hledger.hs +++ b/hledger.hs @@ -70,9 +70,10 @@ parseLedgerAndDo :: [Opt] -> [String] -> ([Opt] -> [String] -> Ledger -> IO ()) parseLedgerAndDo opts args cmd = ledgerFilePathFromOpts opts >>= parseLedgerFile >>= either printParseError runthecommand where - runthecommand = cmd opts args . cacheLedger . normaliseRawLedgerAmounts . filterRawLedger begin end descpats cleared + runthecommand = cmd opts args . cacheLedger . normaliseRawLedgerAmounts . filterRawLedger begin end descpats cleared real begin = beginDateFromOpts opts end = endDateFromOpts opts cleared = Cleared `elem` opts + real = Real `elem` opts descpats = snd $ parseAccountDescriptionArgs args