From 26b6130a9ba02a9dcf829c45d8c80e5db4836f61 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 3 Oct 2008 11:52:07 +0000 Subject: [PATCH] fix filter pattern handling, filtered non -s balance report showing full account names --- Options.hs | 12 ++++++------ hledger.hs | 13 ++++++------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/Options.hs b/Options.hs index 3542ce1b9..5f625e6cb 100644 --- a/Options.hs +++ b/Options.hs @@ -1,4 +1,4 @@ -module Options (parseOptions, parsePatternArgs, nullpats, wildcard, Flag(..), usage, ledgerFilePath) +module Options (parseOptions, parsePatternArgs, regexFor, nullpats, wildcard, Flag(..), usage, ledgerFilePath) where import System.Console.GetOpt import System.Directory @@ -76,10 +76,10 @@ tildeExpand xs = return xs -- | ledger pattern arguments are: 0 or more account patterns -- optionally followed by -- and 0 or more description patterns. --- No arguments implies match all. We convert the arguments to --- a pair of regexps. -parsePatternArgs :: [String] -> (Regex,Regex) -parsePatternArgs args = (regexFor as, regexFor ds') +-- No arguments implies match all. Here we gather these into two lists. +-- parsePatternArgs :: [String] -> (Regex,Regex) +parsePatternArgs :: [String] -> ([String],[String]) +parsePatternArgs args = (as, ds') where (as, ds) = break (=="--") args ds' = dropWhile (=="--") ds @@ -87,7 +87,7 @@ parsePatternArgs args = (regexFor as, regexFor ds') -- or a wildcard if there are none. regexFor :: [String] -> Regex regexFor [] = wildcard -regexFor ss = mkRegex $ "(" ++ (unwords $ intersperse "|" ss) ++ ")" +regexFor ss = mkRegex $ concat $ ["("] ++ (intersperse "|" ss) ++ [")"] wildcard :: Regex wildcard = mkRegex ".*" diff --git a/hledger.hs b/hledger.hs index 25031098d..a7302645c 100644 --- a/hledger.hs +++ b/hledger.hs @@ -55,7 +55,7 @@ main = do | cmd `isPrefixOf` "balance" = balance opts pats | otherwise = putStr usage -type Command = [Flag] -> (Regex,Regex) -> IO () +type Command = [Flag] -> ([String],[String]) -> IO () selftest :: Command selftest opts pats = do @@ -77,19 +77,18 @@ balance opts pats = parseLedgerAndDo opts pats printbalance where showsubs = (ShowSubs `elem` opts) depth = case (pats, showsubs) of - -- when there are no filter patterns and no -s, show - -- only to depth 1. (This was clearer when we used maybe.) - (nullpats, False) -> 1 + -- when there is no -s or pattern args, show with depth 1 + (([],[]), False) -> 1 otherwise -> 9999 -- | parse the user's specified ledger file and do some action with it -- (or report a parse error). This function makes the whole thing go. -parseLedgerAndDo :: [Flag] -> (Regex,Regex) -> (Ledger -> IO ()) -> IO () -parseLedgerAndDo opts pats cmd = do +parseLedgerAndDo :: [Flag] -> ([String],[String]) -> (Ledger -> IO ()) -> IO () +parseLedgerAndDo opts (apats,dpats) cmd = do path <- ledgerFilePath opts parsed <- parseLedgerFile path case parsed of Left err -> parseError err - Right l -> cmd $ cacheLedger l pats + Right l -> cmd $ cacheLedger l (regexFor apats, regexFor dpats) -- ghci helpers