refactor getPostings
This commit is contained in:
parent
86fab58e6a
commit
29d9aed845
@ -75,26 +75,29 @@ getTransaction l args = do
|
|||||||
-- | Read two or more postings from the command line.
|
-- | Read two or more postings from the command line.
|
||||||
getPostings :: Maybe [Posting] -> [Posting] -> IO [Posting]
|
getPostings :: Maybe [Posting] -> [Posting] -> IO [Posting]
|
||||||
getPostings bestmatchps enteredps = do
|
getPostings bestmatchps enteredps = do
|
||||||
account <- askFor (printf "account %d" n)
|
account <- askFor (printf "account %d" n) defaultaccount validateaccount
|
||||||
(maybe Nothing (Just . paccount) bestmatch)
|
|
||||||
(Just $ \s -> not $ null s && (length enteredps < 2))
|
|
||||||
if null account
|
if null account
|
||||||
then return enteredps
|
then return enteredps
|
||||||
else do
|
else do
|
||||||
amount <- liftM (fromparse . parse (someamount <|> return missingamt) "")
|
amountstr <- askFor (printf "amount %d" n) defaultamount validateamount
|
||||||
$ askFor (printf "amount %d" n)
|
let amount = fromparse $ parse (someamount <|> return missingamt) "" amountstr
|
||||||
(maybe Nothing (Just . show . pamount) bestmatch)
|
|
||||||
(Just $ \s -> (null s && (not $ null enteredps)) ||
|
|
||||||
(isRight $ parse (someamount>>many spacenonewline>>eof) "" s))
|
|
||||||
let p = nullrawposting{paccount=account,pamount=amount}
|
let p = nullrawposting{paccount=account,pamount=amount}
|
||||||
if amount == missingamt
|
if amount == missingamt
|
||||||
then return $ enteredps ++ [p]
|
then return $ enteredps ++ [p]
|
||||||
else getPostings bestmatchps $ enteredps ++ [p]
|
else getPostings bestmatchps $ enteredps ++ [p]
|
||||||
where n = length enteredps + 1
|
where
|
||||||
|
n = length enteredps + 1
|
||||||
bestmatch | isNothing bestmatchps = Nothing
|
bestmatch | isNothing bestmatchps = Nothing
|
||||||
| n <= length ps = Just $ ps !! (n-1)
|
| n <= length ps = Just $ ps !! (n-1)
|
||||||
| otherwise = Nothing
|
| otherwise = Nothing
|
||||||
where Just ps = bestmatchps
|
where Just ps = bestmatchps
|
||||||
|
defaultaccount = maybe Nothing (Just . paccount) bestmatch
|
||||||
|
validateaccount = Just $ \s -> not $ null s && (length enteredps < 2)
|
||||||
|
defaultamount = maybe Nothing (Just . show . pamount) bestmatch
|
||||||
|
validateamount = Just $ \s ->
|
||||||
|
(null s && (not $ null enteredps)) ||
|
||||||
|
(isRight $ parse (someamount>>many spacenonewline>>eof) "" s)
|
||||||
|
|
||||||
|
|
||||||
-- | Prompt and read a string value, possibly with a default and a validator.
|
-- | Prompt and read a string value, possibly with a default and a validator.
|
||||||
-- A validator will cause the prompt to repeat until the input is valid.
|
-- A validator will cause the prompt to repeat until the input is valid.
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user