lib: after reloading a journal, also refilter it (#314)

After reloading a journal due to a file change, we now also re-apply any
query specified by the options.
This commit is contained in:
Simon Michael 2016-04-28 21:46:10 -07:00
parent 7b1b1f5f8d
commit f3360c8cfe

View File

@ -25,6 +25,7 @@ where
import Control.Exception as C import Control.Exception as C
import Data.List import Data.List
import Data.Maybe import Data.Maybe
import Data.Time (Day)
import Safe (readMay) import Safe (readMay)
import System.Console.CmdArgs import System.Console.CmdArgs
import System.Directory (getModificationTime, getDirectoryContents, copyFile) import System.Directory (getModificationTime, getDirectoryContents, copyFile)
@ -56,6 +57,7 @@ import Data.Time.Clock.POSIX (utcTimeToPOSIXSeconds)
import Hledger.Cli.CliOptions import Hledger.Cli.CliOptions
import Hledger.Data import Hledger.Data
import Hledger.Read import Hledger.Read
import Hledger.Reports (queryFromOpts)
import Hledger.Utils import Hledger.Utils
@ -87,19 +89,23 @@ journalReload j = readJournalFile Nothing Nothing True $ journalFilePath j
-- | Re-read a journal from its data file mostly, only if the file has -- | Re-read a journal from its data file mostly, only if the file has
-- changed since last read (or if there is no file, ie data read from -- changed since last read (or if there is no file, ie data read from
-- stdin). The provided options are mostly ignored. Return a journal or -- stdin). The provided options and current date are used to filter
-- the error message while reading it, and a flag indicating whether it -- the re-read journal; this is intended to reapply the same filter as
-- was re-read or not. -- at program startup (though, the current date may not be what it was
journalReloadIfChanged :: CliOpts -> Journal -> IO (Either String Journal, Bool) -- then, so results may differ). Returns a journal or error message,
journalReloadIfChanged _ j = do -- and a flag indicating whether it was re-read or not.
journalReloadIfChanged :: CliOpts -> Day -> Journal -> IO (Either String Journal, Bool)
journalReloadIfChanged opts d j = do
let maybeChangedFilename f = do newer <- journalSpecifiedFileIsNewer j f let maybeChangedFilename f = do newer <- journalSpecifiedFileIsNewer j f
return $ if newer then Just f else Nothing return $ if newer then Just f else Nothing
changedfiles <- catMaybes `fmap` mapM maybeChangedFilename (journalFilePaths j) changedfiles <- catMaybes `fmap` mapM maybeChangedFilename (journalFilePaths j)
if not $ null changedfiles if not $ null changedfiles
then do then do
whenLoud $ printf "%s has changed, reloading\n" (head changedfiles) whenLoud $ printf "%s has changed, reloading\n" (head changedfiles)
jE <- journalReload j ej <- journalReload j
return (jE, True) let initq = queryFromOpts d $ reportopts_ opts
ej' = filterJournalTransactions initq <$> ej
return (ej', True)
else else
return (Right j, False) return (Right j, False)