diff --git a/Ledger/Ledger.hs b/Ledger/Ledger.hs index 9247aca7e..a7ddd9d3b 100644 --- a/Ledger/Ledger.hs +++ b/Ledger/Ledger.hs @@ -74,39 +74,6 @@ cacheLedger acctpat l = in Ledger l ant amap maxprecision acctpat filteredant filteredamap --- | Remove ledger entries we are not interested in. --- Keep only those which fall between the begin and end dates, and match --- the description patterns. -filterLedgerEntries :: String -> String -> Regex -> RawLedger -> RawLedger -filterLedgerEntries begin end descpat = - filterLedgerEntriesByDate begin end . - filterLedgerEntriesByDescription descpat - --- | Keep only entries whose description matches the description pattern. -filterLedgerEntriesByDescription :: Regex -> RawLedger -> RawLedger -filterLedgerEntriesByDescription descpat (RawLedger ms ps es f) = - RawLedger ms ps (filter matchdesc es) f - where - matchdesc :: Entry -> Bool - matchdesc e = case matchRegex descpat (edescription e) of - Nothing -> False - otherwise -> True - --- | Keep only entries which fall between begin and end dates. --- We include entries on the begin date and exclude entries on the end --- date, like ledger. An empty date string means no restriction. -filterLedgerEntriesByDate :: String -> String -> RawLedger -> RawLedger -filterLedgerEntriesByDate begin end (RawLedger ms ps es f) = - RawLedger ms ps (filter matchdate es) f - where - matchdate :: Entry -> Bool - matchdate e = (begin == "" || entrydate >= begindate) && - (end == "" || entrydate < enddate) - where - begindate = parsedate begin :: UTCTime - enddate = parsedate end - entrydate = parsedate $ edate e - -- | List a 'Ledger' 's account names. accountnames :: Ledger -> [AccountName] accountnames l = drop 1 $ flatten $ accountnametree l diff --git a/Ledger/RawLedger.hs b/Ledger/RawLedger.hs index 01dc06d27..e8a369c96 100644 --- a/Ledger/RawLedger.hs +++ b/Ledger/RawLedger.hs @@ -37,3 +37,37 @@ rawLedgerAccountNames = sort . expandAccountNames . rawLedgerAccountNamesUsed rawLedgerAccountNameTree :: RawLedger -> Tree AccountName 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. +filterRawLedgerEntries :: String -> String -> Regex -> RawLedger -> RawLedger +filterRawLedgerEntries begin end descpat = + filterRawLedgerEntriesByDate begin end . + filterRawLedgerEntriesByDescription descpat + +-- | Keep only entries whose description matches the description pattern. +filterRawLedgerEntriesByDescription :: Regex -> RawLedger -> RawLedger +filterRawLedgerEntriesByDescription descpat (RawLedger ms ps es f) = + RawLedger ms ps (filter matchdesc es) f + where + matchdesc :: Entry -> Bool + matchdesc e = case matchRegex descpat (edescription e) of + Nothing -> False + otherwise -> True + +-- | Keep only entries which fall between begin and end dates. +-- We include entries on the begin date and exclude entries on the end +-- date, like ledger. An empty date string means no restriction. +filterRawLedgerEntriesByDate :: String -> String -> RawLedger -> RawLedger +filterRawLedgerEntriesByDate begin end (RawLedger ms ps es f) = + RawLedger ms ps (filter matchdate es) f + where + matchdate :: Entry -> Bool + matchdate e = (begin == "" || entrydate >= begindate) && + (end == "" || entrydate < enddate) + where + begindate = parsedate begin :: UTCTime + enddate = parsedate end + entrydate = parsedate $ edate e + diff --git a/hledger.hs b/hledger.hs index 68cabff22..af1f24b57 100644 --- a/hledger.hs +++ b/hledger.hs @@ -61,7 +61,7 @@ parseLedgerAndDo :: [Opt] -> [String] -> ([Opt] -> [String] -> Ledger -> IO ()) parseLedgerAndDo opts args cmd = ledgerFilePathFromOpts opts >>= parseLedgerFile >>= either printParseError runthecommand where - runthecommand = cmd opts args . cacheLedger acctpat . filterLedgerEntries begin end descpat + runthecommand = cmd opts args . cacheLedger acctpat . filterRawLedgerEntries begin end descpat begin = beginDateFromOpts opts end = endDateFromOpts opts acctpat = regexFor acctpats @@ -81,7 +81,7 @@ myrawledger = do myledger :: IO Ledger myledger = do l <- myrawledger - return $ cacheLedger wildcard $ filterLedgerEntries "" "" wildcard l + return $ cacheLedger wildcard $ filterRawLedgerEntries "" "" wildcard l -- | get a Ledger from the given file path rawledgerfromfile :: String -> IO RawLedger