From 042b12dcd7f17d1927927548487cafd477f1420d Mon Sep 17 00:00:00 2001 From: Juri Dispan Date: Wed, 14 Oct 2020 11:04:48 +0200 Subject: [PATCH] make auto update optional --- src/Tldr/App.hs | 10 +++++++++- src/Tldr/App/Handler.hs | 17 ++++++++--------- src/Tldr/Types.hs | 3 ++- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/Tldr/App.hs b/src/Tldr/App.hs index f92563b..e4cf7c3 100644 --- a/src/Tldr/App.hs +++ b/src/Tldr/App.hs @@ -17,7 +17,7 @@ import Control.Monad (void) programOptions :: Parser TldrOpts programOptions = - TldrOpts <$> (updateIndexCommand <|> viewPageCommand <|> aboutFlag) + TldrOpts <$> (updateIndexCommand <|> viewPageCommand <|> aboutFlag) <*> autoUpdateIntervalOpt updateIndexCommand :: Parser TldrCommand updateIndexCommand = @@ -25,6 +25,14 @@ updateIndexCommand = UpdateIndex (long "update" <> short 'u' <> help "Update offline cache of tldr pages") +autoUpdateIntervalOpt :: Parser (Maybe Int) +autoUpdateIntervalOpt = + optional + (option auto + (long "auto-update-interval" <> metavar "DAYS" <> + help + "Perform an automatic update if the cache is older than DAYS")) + aboutFlag :: Parser TldrCommand aboutFlag = flag' About (long "about" <> short 'a' <> help "About this program") diff --git a/src/Tldr/App/Handler.hs b/src/Tldr/App/Handler.hs index 731416d..c1abbe5 100644 --- a/src/Tldr/App/Handler.hs +++ b/src/Tldr/App/Handler.hs @@ -73,8 +73,8 @@ handleTldrOpts opts@TldrOpts {..} = UpdateIndex -> updateTldrPages About -> handleAboutFlag ViewPage voptions pages -> do - performUpdate <- updateNecessary - when performUpdate updateTldrPages + shouldPerformUpdate <- updateNecessary opts + when shouldPerformUpdate updateTldrPages let npage = intercalate "-" pages locale <- case languageOption voptions of @@ -94,12 +94,8 @@ handleTldrOpts opts@TldrOpts {..} = ViewPage (englishViewOptions voptions) pages }) --- We update if the data directory does not exist. --- We also update if the cached pages version is older than 7 days. --- TODO: Make the auto-update interval configurable. --- TODO: Add command line option to skip auto update. -updateNecessary :: IO Bool -updateNecessary = do +updateNecessary :: TldrOpts -> IO Bool +updateNecessary TldrOpts{..} = do dataDir <- getXdgDirectory XdgData tldrDirName dataDirExists <- doesDirectoryExist dataDir if not dataDirExists @@ -107,7 +103,10 @@ updateNecessary = do else do lastCachedTime <- getModificationTime dataDir currentTime <- getCurrentTime - return $ currentTime `diffUTCTime` lastCachedTime > 7 * nominalDay + let diffExceedsLimit limit + = currentTime `diffUTCTime` lastCachedTime + > fromIntegral limit * nominalDay + return $ maybe False diffExceedsLimit autoUpdateInterval updateTldrPages :: IO () updateTldrPages = do diff --git a/src/Tldr/Types.hs b/src/Tldr/Types.hs index e0d0d42..f21e9ed 100644 --- a/src/Tldr/Types.hs +++ b/src/Tldr/Types.hs @@ -15,8 +15,9 @@ data ConsoleSetting = , consoleIntensity :: ConsoleIntensity } -newtype TldrOpts = TldrOpts +data TldrOpts = TldrOpts { tldrAction :: TldrCommand + , autoUpdateInterval :: Maybe Int } deriving (Show) data TldrCommand