From dc7a692a1905984d794f30272a9fda52adc05ecb Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 3 Jun 2011 02:14:36 +0000 Subject: [PATCH] refactor: remove costbasis and whichdate from FilterSpec --- hledger-lib/Hledger/Data/Journal.hs | 21 ++++++--------------- hledger-lib/Hledger/Data/Types.hs | 2 -- hledger/Hledger/Cli.hs | 7 ++++--- hledger/Hledger/Cli/Balance.hs | 3 ++- hledger/Hledger/Cli/Options.hs | 6 ++++-- hledger/Hledger/Cli/Print.hs | 13 +++++++------ hledger/Hledger/Cli/Register.hs | 3 ++- 7 files changed, 25 insertions(+), 30 deletions(-) diff --git a/hledger-lib/Hledger/Data/Journal.hs b/hledger-lib/Hledger/Data/Journal.hs index da9a249c4..7044e8ad0 100644 --- a/hledger-lib/Hledger/Data/Journal.hs +++ b/hledger-lib/Hledger/Data/Journal.hs @@ -54,10 +54,8 @@ nullfilterspec = FilterSpec { ,cleared=Nothing ,real=False ,empty=False - ,costbasis=False ,acctpats=[] ,descpats=[] - ,whichdate=ActualDate ,depth=Nothing } @@ -100,38 +98,32 @@ journalAccountNameTree = accountNameTreeFrom . journalAccountNames -- Various kinds of filtering on journals. We do it differently depending -- on the command. --- | Keep only transactions we are interested in, as described by --- the filter specification. May also massage the data a little. +-- | Keep only transactions we are interested in, as described by the +-- filter specification. filterJournalTransactions :: FilterSpec -> Journal -> Journal filterJournalTransactions FilterSpec{datespan=datespan ,cleared=cleared -- ,real=real -- ,empty=empty - -- ,costbasis=_ ,acctpats=apats ,descpats=dpats - ,whichdate=whichdate ,depth=depth } = filterJournalTransactionsByClearedStatus cleared . filterJournalPostingsByDepth depth . filterJournalTransactionsByAccount apats . filterJournalTransactionsByDescription dpats . - filterJournalTransactionsByDate datespan . - journalSelectingDate whichdate + filterJournalTransactionsByDate datespan --- | Keep only postings we are interested in, as described by --- the filter specification. May also massage the data a little. --- This can leave unbalanced transactions. +-- | Keep only postings we are interested in, as described by the filter +-- specification. This can leave unbalanced transactions. filterJournalPostings :: FilterSpec -> Journal -> Journal filterJournalPostings FilterSpec{datespan=datespan ,cleared=cleared ,real=real ,empty=empty --- ,costbasis=costbasis ,acctpats=apats ,descpats=dpats - ,whichdate=whichdate ,depth=depth } = filterJournalPostingsByRealness real . @@ -140,8 +132,7 @@ filterJournalPostings FilterSpec{datespan=datespan filterJournalPostingsByDepth depth . filterJournalPostingsByAccount apats . filterJournalTransactionsByDescription dpats . - filterJournalTransactionsByDate datespan . - journalSelectingDate whichdate + filterJournalTransactionsByDate datespan -- | Keep only transactions whose description matches the description patterns. filterJournalTransactionsByDescription :: [String] -> Journal -> Journal diff --git a/hledger-lib/Hledger/Data/Types.hs b/hledger-lib/Hledger/Data/Types.hs index b2a32e781..1e4547365 100644 --- a/hledger-lib/Hledger/Data/Types.hs +++ b/hledger-lib/Hledger/Data/Types.hs @@ -193,10 +193,8 @@ data FilterSpec = FilterSpec { ,cleared :: Maybe Bool -- ^ only include if cleared\/uncleared\/don't care ,real :: Bool -- ^ only include if real\/don't care ,empty :: Bool -- ^ include if empty (ie amount is zero) - ,costbasis :: Bool -- ^ convert all amounts to cost basis ,acctpats :: [String] -- ^ only include if matching these account patterns ,descpats :: [String] -- ^ only include if matching these description patterns - ,whichdate :: WhichDate -- ^ which dates to use (actual or effective) ,depth :: Maybe Int } deriving (Show) diff --git a/hledger/Hledger/Cli.hs b/hledger/Hledger/Cli.hs index 21732b37b..07b34708b 100644 --- a/hledger/Hledger/Cli.hs +++ b/hledger/Hledger/Cli.hs @@ -281,9 +281,10 @@ tests_Hledger_Cli = TestList "print expenses" ~: do let args = ["expenses"] - l <- samplejournalwithopts [] args + opts = [] + l <- samplejournalwithopts opts args t <- getCurrentLocalTime - showTransactions (optsToFilterSpec [] args t) l `is` unlines + showTransactions opts (optsToFilterSpec opts args t) l `is` unlines ["2008/06/03 * eat & shop" ," expenses:food $1" ," expenses:supplies $1" @@ -295,7 +296,7 @@ tests_Hledger_Cli = TestList do l <- samplejournal t <- getCurrentLocalTime - showTransactions (optsToFilterSpec [Depth "2"] [] t) l `is` unlines + showTransactions [] (optsToFilterSpec [Depth "2"] [] t) l `is` unlines ["2008/01/01 income" ," income:salary $-1" ,"" diff --git a/hledger/Hledger/Cli/Balance.hs b/hledger/Hledger/Cli/Balance.hs index 72897debc..9a2ea71ca 100644 --- a/hledger/Hledger/Cli/Balance.hs +++ b/hledger/Hledger/Cli/Balance.hs @@ -131,7 +131,8 @@ type BalanceReportItem = (AccountName -- full account name balance :: [Opt] -> [String] -> Journal -> IO () balance opts args j = do t <- getCurrentLocalTime - putStr $ balanceReportAsText opts $ balanceReport opts (optsToFilterSpec opts args t) j + let j' = journalSelectingDate (whichDateFromOpts opts) j + putStr $ balanceReportAsText opts $ balanceReport opts (optsToFilterSpec opts args t) j' -- | Render a balance report as plain text suitable for console output. balanceReportAsText :: [Opt] -> BalanceReport -> String diff --git a/hledger/Hledger/Cli/Options.hs b/hledger/Hledger/Cli/Options.hs index fc5caa5af..3c845b2b7 100644 --- a/hledger/Hledger/Cli/Options.hs +++ b/hledger/Hledger/Cli/Options.hs @@ -249,6 +249,10 @@ clearedValueFromOpts opts | null os = Nothing | otherwise = Just False where os = optsWithConstructors [Cleared,UnCleared] opts +-- | Detect which date we will report on, based on --effective. +whichDateFromOpts :: [Opt] -> WhichDate +whichDateFromOpts opts = if Effective `elem` opts then EffectiveDate else ActualDate + -- | Were we invoked as \"hours\" ? usingTimeProgramName :: IO Bool usingTimeProgramName = do @@ -281,10 +285,8 @@ optsToFilterSpec opts args t = FilterSpec { ,cleared=clearedValueFromOpts opts ,real=Real `elem` opts ,empty=Empty `elem` opts - ,costbasis=CostBasis `elem` opts ,acctpats=apats ,descpats=dpats - ,whichdate = if Effective `elem` opts then EffectiveDate else ActualDate ,depth = depthFromOpts opts } where (apats,dpats) = parsePatternArgs args diff --git a/hledger/Hledger/Cli/Print.hs b/hledger/Hledger/Cli/Print.hs index db9a2782d..c928a5843 100644 --- a/hledger/Hledger/Cli/Print.hs +++ b/hledger/Hledger/Cli/Print.hs @@ -28,14 +28,15 @@ type JournalReportItem = Transaction print' :: [Opt] -> [String] -> Journal -> IO () print' opts args j = do t <- getCurrentLocalTime - putStr $ showTransactions (optsToFilterSpec opts args t) j + let j' = journalSelectingDate (whichDateFromOpts opts) j + putStr $ showTransactions opts (optsToFilterSpec opts args t) j' -showTransactions :: FilterSpec -> Journal -> String -showTransactions fspec j = journalReportAsText [] fspec $ journalReport [] fspec j +showTransactions :: [Opt] -> FilterSpec -> Journal -> String +showTransactions opts fspec j = journalReportAsText opts fspec $ journalReport [] fspec j -journalReportAsText :: [Opt] -> FilterSpec -> JournalReport -> String -- XXX unlike the others, this one needs fspec not opts -journalReportAsText _ fspec items = concatMap (showTransactionForPrint effective) items - where effective = EffectiveDate == whichdate fspec +journalReportAsText :: [Opt] -> FilterSpec -> JournalReport -> String +journalReportAsText opts _ items = concatMap (showTransactionForPrint effective) items + where effective = Effective `elem` opts journalReport :: [Opt] -> FilterSpec -> Journal -> JournalReport journalReport _ fspec j = sortBy (comparing tdate) $ jtxns $ filterJournalTransactions fspec j diff --git a/hledger/Hledger/Cli/Register.hs b/hledger/Hledger/Cli/Register.hs index 9d93451a9..21cea1f03 100644 --- a/hledger/Hledger/Cli/Register.hs +++ b/hledger/Hledger/Cli/Register.hs @@ -78,7 +78,8 @@ registerReport opts fspec j = getitems ps nullposting startbal (precedingps, displayableps, _) = postingsMatchingDisplayExpr (displayExprFromOpts opts) $ depthClipPostings depth $ journalPostings - $ filterJournalPostings fspec{depth=Nothing} j + $ filterJournalPostings fspec{depth=Nothing} j' + j' = journalSelectingDate (whichDateFromOpts opts) j startbal = sumPostings precedingps filterspan = datespan fspec (interval, depth, empty) = (intervalFromOpts opts, depthFromOpts opts, Empty `elem` opts)