Move getPostings arguments into a record.
This seems like the kind of function that can get increasingly sophisticated as the UI gets smarter. A single argument record could lend a certain stability, and we're not likely to lose much in partial application terms.
This commit is contained in:
parent
b5081b41a2
commit
3ae989e8cb
@ -34,6 +34,13 @@ import qualified Data.Set as Set
|
|||||||
import Safe (headMay)
|
import Safe (headMay)
|
||||||
import Control.Exception (throw)
|
import Control.Exception (throw)
|
||||||
|
|
||||||
|
{- | Information used as the basis for suggested account names, amounts,
|
||||||
|
etc in add prompt
|
||||||
|
-}
|
||||||
|
data PostingState = PostingState {
|
||||||
|
psContext :: JournalContext,
|
||||||
|
psAccept :: AccountName -> Bool,
|
||||||
|
psHistory :: Maybe [Posting]}
|
||||||
|
|
||||||
-- | Read transactions from the terminal, prompting for each field,
|
-- | Read transactions from the terminal, prompting for each field,
|
||||||
-- and append them to the journal file. If the journal came from stdin, this
|
-- and append them to the journal file. If the journal came from stdin, this
|
||||||
@ -82,7 +89,7 @@ getTransaction j opts args defaultDate = do
|
|||||||
else True
|
else True
|
||||||
where (ant,_,_,_) = groupPostings $ journalPostings j
|
where (ant,_,_,_) = groupPostings $ journalPostings j
|
||||||
getpostingsandvalidate = do
|
getpostingsandvalidate = do
|
||||||
ps <- getPostings (jContext j) accept bestmatchpostings []
|
ps <- getPostings (PostingState (jContext j) accept bestmatchpostings) []
|
||||||
let t = nulltransaction{tdate=date
|
let t = nulltransaction{tdate=date
|
||||||
,tstatus=False
|
,tstatus=False
|
||||||
,tdescription=description
|
,tdescription=description
|
||||||
@ -101,8 +108,8 @@ getTransaction j opts args defaultDate = do
|
|||||||
-- fragile
|
-- fragile
|
||||||
-- | Read postings from the command line until . is entered, using any
|
-- | Read postings from the command line until . is entered, using any
|
||||||
-- provided historical postings and the journal context to guess defaults.
|
-- provided historical postings and the journal context to guess defaults.
|
||||||
getPostings :: JournalContext -> (AccountName -> Bool) -> Maybe [Posting] -> [Posting] -> InputT IO [Posting]
|
getPostings :: PostingState -> [Posting] -> InputT IO [Posting]
|
||||||
getPostings ctx accept historicalps enteredps = do
|
getPostings st enteredps = do
|
||||||
let bestmatch | isNothing historicalps = Nothing
|
let bestmatch | isNothing historicalps = Nothing
|
||||||
| n <= length ps = Just $ ps !! (n-1)
|
| n <= length ps = Just $ ps !! (n-1)
|
||||||
| otherwise = Nothing
|
| otherwise = Nothing
|
||||||
@ -130,7 +137,6 @@ getPostings ctx accept historicalps enteredps = do
|
|||||||
let amount = fromparse $ runParser (someamount <|> return missingamt) ctx "" amountstr
|
let amount = fromparse $ runParser (someamount <|> return missingamt) ctx "" amountstr
|
||||||
amount' = fromparse $ runParser (someamount <|> return missingamt) nullctx "" amountstr
|
amount' = fromparse $ runParser (someamount <|> return missingamt) nullctx "" amountstr
|
||||||
defaultamtused = Just (showMixedAmount amount) == defaultamountstr
|
defaultamtused = Just (showMixedAmount amount) == defaultamountstr
|
||||||
historicalps'' = if defaultamtused then historicalps' else Nothing
|
|
||||||
commodityadded | c == cwithnodef = Nothing
|
commodityadded | c == cwithnodef = Nothing
|
||||||
| otherwise = c
|
| otherwise = c
|
||||||
where c = maybemixedamountcommodity amount
|
where c = maybemixedamountcommodity amount
|
||||||
@ -139,10 +145,14 @@ getPostings ctx accept historicalps enteredps = do
|
|||||||
p = nullposting{paccount=stripbrackets account,
|
p = nullposting{paccount=stripbrackets account,
|
||||||
pamount=amount,
|
pamount=amount,
|
||||||
ptype=postingtype account}
|
ptype=postingtype account}
|
||||||
|
st' = st{psHistory = if defaultamtused then historicalps' else Nothing}
|
||||||
when (isJust commodityadded) $
|
when (isJust commodityadded) $
|
||||||
liftIO $ hPutStrLn stderr $ printf "using default commodity (%s)" (symbol $ fromJust commodityadded)
|
liftIO $ hPutStrLn stderr $ printf "using default commodity (%s)" (symbol $ fromJust commodityadded)
|
||||||
getPostings ctx accept historicalps'' $ enteredps ++ [p]
|
getPostings st' (enteredps ++ [p])
|
||||||
where
|
where
|
||||||
|
historicalps = psHistory st
|
||||||
|
ctx = psContext st
|
||||||
|
accept = psAccept st
|
||||||
n = length enteredps + 1
|
n = length enteredps + 1
|
||||||
enteredrealps = filter isReal enteredps
|
enteredrealps = filter isReal enteredps
|
||||||
showacctname p = showAccountName Nothing (ptype p) $ paccount p
|
showacctname p = showAccountName Nothing (ptype p) $ paccount p
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user