From 9e9aec957fe321c299d8e94cc71b88034b5c00d3 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Fri, 28 Aug 2015 15:10:51 -0700 Subject: [PATCH] print: --match shows the best match for a description This was originally an add-on I used to guess account names for ledger-autosync. It's nice for quickly looking up a recent transaction with a guessed or partial description. --- hledger/Hledger/Cli/Print.hs | 41 ++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/hledger/Hledger/Cli/Print.hs b/hledger/Hledger/Cli/Print.hs index 4dd22fbcc..1088f7199 100644 --- a/hledger/Hledger/Cli/Print.hs +++ b/hledger/Hledger/Cli/Print.hs @@ -20,12 +20,20 @@ import Text.CSV import Hledger import Hledger.Cli.CliOptions import Hledger.Cli.Utils +import Hledger.Cli.Add ( transactionsSimilarTo ) printmode = (defCommandMode $ ["print"] ++ aliases) { modeHelp = "show transaction entries" `withAliases` aliases ,modeGroupFlags = Group { - groupUnnamed = outputflags + groupUnnamed = [ + let matcharg = "STR" + in + flagReq ["match","m"] (\s opts -> Right $ setopt "match" s opts) matcharg + ("show the transaction whose description is most similar to "++matcharg + ++ ", and is most recent") + ] + ++ outputflags ,groupHidden = [] ,groupNamed = [generalflagsgroup1] } @@ -34,7 +42,13 @@ printmode = (defCommandMode $ ["print"] ++ aliases) { -- | Print journal transactions in standard format. print' :: CliOpts -> Journal -> IO () -print' opts@CliOpts{reportopts_=ropts} j = do +print' opts j = do + case maybestringopt "match" $ rawopts_ opts of + Nothing -> printEntries opts j + Just desc -> printMatch opts j desc + +printEntries :: CliOpts -> Journal -> IO () +printEntries opts@CliOpts{reportopts_=ropts} j = do d <- getCurrentDay let q = queryFromOpts d ropts fmt = outputFormatFromOpts opts @@ -121,5 +135,28 @@ postingToCSV p = account = showAccountName Nothing (ptype p) (paccount p) comment = chomp $ strip $ pcomment p +-- --match + +-- | Print the transaction most closely and recently matching a description +-- (and the query, if any). +printMatch :: CliOpts -> Journal -> String -> IO () +printMatch CliOpts{reportopts_=ropts} j desc = do + d <- getCurrentDay + let q = queryFromOpts d ropts + case similarTransaction' j q desc of + Nothing -> putStrLn "no matches found." + Just t -> putStr $ showTransactionUnelided t + + where + -- Identify the closest recent match for this description in past transactions. + similarTransaction' :: Journal -> Query -> String -> Maybe Transaction + similarTransaction' j q desc + | null historymatches = Nothing + | otherwise = Just $ snd $ head historymatches + where + historymatches = transactionsSimilarTo j q desc + +-- tests + tests_Hledger_Cli_Print = TestList [] -- tests_showTransactions