add: cleanups
This commit is contained in:
parent
4f1ce14033
commit
8aba692926
@ -43,9 +43,9 @@ data PostingState = PostingState {
|
|||||||
psSuggestHistoricalAmount :: Bool,
|
psSuggestHistoricalAmount :: Bool,
|
||||||
psHistory :: Maybe [Posting]}
|
psHistory :: Maybe [Posting]}
|
||||||
|
|
||||||
-- | Read transactions from the terminal, prompting for each field,
|
-- | Read multiple transactions from the console, prompting for each
|
||||||
-- and append them to the journal file. If the journal came from stdin, this
|
-- field, and append them to the journal file. If the journal came
|
||||||
-- command has no effect.
|
-- from stdin, this command has no effect.
|
||||||
add :: CliOpts -> Journal -> IO ()
|
add :: CliOpts -> Journal -> IO ()
|
||||||
add opts j
|
add opts j
|
||||||
| f == "-" = return ()
|
| f == "-" = return ()
|
||||||
@ -65,23 +65,23 @@ add opts j
|
|||||||
`C.catch` (\e -> unless (isEOFError e) $ ioError e)
|
`C.catch` (\e -> unless (isEOFError e) $ ioError e)
|
||||||
where f = journalFilePath j
|
where f = journalFilePath j
|
||||||
|
|
||||||
headTailDef :: a -> [a] -> (a,[a])
|
-- | Loop reading transactions from the console, prompting for,
|
||||||
headTailDef defhead as = (headDef defhead as, tailDef [] as)
|
-- validating, displaying and appending each one to the journal file,
|
||||||
|
-- until end of input or ctrl-c (then raise an EOF exception).
|
||||||
-- | Read a number of transactions from the command line, prompting,
|
-- If provided, command-line arguments are used as defaults for the
|
||||||
-- validating, displaying and appending them to the journal file, until
|
-- first transaction; otherwise defaults come from the most similar
|
||||||
-- end of input (then raise an EOF exception). Any command-line arguments
|
-- recent transaction.
|
||||||
-- are used as the first transaction's description.
|
|
||||||
getAndAddTransactions :: Journal -> CliOpts -> String -> [String] -> IO ()
|
getAndAddTransactions :: Journal -> CliOpts -> String -> [String] -> IO ()
|
||||||
getAndAddTransactions j opts defdate moredefs = do
|
getAndAddTransactions j opts defdate moredefs = do
|
||||||
(t, defdate') <- getTransaction j opts defdate moredefs
|
t <- getTransaction j opts defdate moredefs
|
||||||
j <- journalAddTransaction j opts t
|
j <- journalAddTransaction j opts t
|
||||||
hPrintf stderr "\nRecorded transaction:\n%s" (show t)
|
hPrintf stderr "\nRecorded transaction:\n%s" (show t)
|
||||||
|
let defdate' = showDate $ tdate t
|
||||||
getAndAddTransactions j opts defdate' []
|
getAndAddTransactions j opts defdate' []
|
||||||
|
|
||||||
-- | Read a transaction from the command line, with history-aware prompting.
|
-- | Read a single transaction from the console, with history-aware prompting.
|
||||||
-- A default date, and zero or more defaults for subsequent fields, are provided.
|
-- A default date, and zero or more defaults for subsequent fields, are provided.
|
||||||
getTransaction :: Journal -> CliOpts -> String -> [String] -> IO (Transaction,String)
|
getTransaction :: Journal -> CliOpts -> String -> [String] -> IO Transaction
|
||||||
getTransaction j opts defdate moredefs = do
|
getTransaction j opts defdate moredefs = do
|
||||||
datestr <- runInteractionDefault $ askFor "date"
|
datestr <- runInteractionDefault $ askFor "date"
|
||||||
(Just defdate)
|
(Just defdate)
|
||||||
@ -96,12 +96,14 @@ getTransaction j opts defdate moredefs = do
|
|||||||
if description == "<"
|
if description == "<"
|
||||||
then restart
|
then restart
|
||||||
else do
|
else do
|
||||||
mr <- getPostingsAndValidateTransaction j opts datestr description moredefs'
|
mt <- getPostingsAndValidateTransaction j opts datestr description moredefs'
|
||||||
case mr of
|
case mt of
|
||||||
Nothing -> restart
|
Nothing -> restart
|
||||||
Just r -> return r
|
Just t -> return t
|
||||||
|
|
||||||
getPostingsAndValidateTransaction :: Journal -> CliOpts -> String -> String -> [String] -> IO (Maybe (Transaction,String))
|
-- | Loop reading postings from the console, until two or more valid balanced
|
||||||
|
-- postings have been entered, then return the final transaction.
|
||||||
|
getPostingsAndValidateTransaction :: Journal -> CliOpts -> String -> String -> [String] -> IO (Maybe Transaction)
|
||||||
getPostingsAndValidateTransaction j opts datestr description defargs = do
|
getPostingsAndValidateTransaction j opts datestr description defargs = do
|
||||||
today <- getCurrentDay
|
today <- getCurrentDay
|
||||||
let historymatches = transactionsSimilarTo j (queryFromOpts today $ reportopts_ opts) description
|
let historymatches = transactionsSimilarTo j (queryFromOpts today $ reportopts_ opts) description
|
||||||
@ -125,14 +127,13 @@ getPostingsAndValidateTransaction j opts datestr description defargs = do
|
|||||||
let msg' = capitalize msg
|
let msg' = capitalize msg
|
||||||
liftIO $ hPutStrLn stderr $ "\n" ++ msg' ++ "please re-enter."
|
liftIO $ hPutStrLn stderr $ "\n" ++ msg' ++ "please re-enter."
|
||||||
getpostingsandvalidate
|
getpostingsandvalidate
|
||||||
either retry (return . Just . flip (,) (showDate date)) $ balanceTransaction Nothing t -- imprecise balancing
|
either retry (return . Just) $ balanceTransaction Nothing t -- imprecise balancing
|
||||||
when (isJust bestmatch) $ liftIO $ hPrintf stderr "\nUsing this similar transaction for defaults:\n%s" (show $ fromJust bestmatch)
|
when (isJust bestmatch) $ liftIO $ hPrintf stderr "\nUsing this similar transaction for defaults:\n%s" (show $ fromJust bestmatch)
|
||||||
getpostingsandvalidate `catch` \(_::RestartEntryException) -> return Nothing
|
getpostingsandvalidate `catch` \(_::RestartEntryException) -> return Nothing
|
||||||
|
|
||||||
data RestartEntryException = RestartEntryException deriving (Typeable,Show)
|
data RestartEntryException = RestartEntryException deriving (Typeable,Show)
|
||||||
instance Exception RestartEntryException
|
instance Exception RestartEntryException
|
||||||
|
|
||||||
-- 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.
|
||||||
getPostingsWithState :: PostingState -> [Posting] -> [String] -> IO [Posting]
|
getPostingsWithState :: PostingState -> [Posting] -> [String] -> IO [Posting]
|
||||||
@ -326,3 +327,7 @@ accountCompletion cc = completeWord Nothing
|
|||||||
capitalize :: String -> String
|
capitalize :: String -> String
|
||||||
capitalize "" = ""
|
capitalize "" = ""
|
||||||
capitalize (c:cs) = toUpper c : cs
|
capitalize (c:cs) = toUpper c : cs
|
||||||
|
|
||||||
|
headTailDef :: a -> [a] -> (a,[a])
|
||||||
|
headTailDef defhead as = (headDef defhead as, tailDef [] as)
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user