Skip to content

Commit

Permalink
make auto update optional
Browse files Browse the repository at this point in the history
  • Loading branch information
gutjuri committed Oct 14, 2020
1 parent bfad30d commit 042b12d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
10 changes: 9 additions & 1 deletion src/Tldr/App.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,22 @@ import Control.Monad (void)

programOptions :: Parser TldrOpts
programOptions =
TldrOpts <$> (updateIndexCommand <|> viewPageCommand <|> aboutFlag)
TldrOpts <$> (updateIndexCommand <|> viewPageCommand <|> aboutFlag) <*> autoUpdateIntervalOpt

updateIndexCommand :: Parser TldrCommand
updateIndexCommand =
flag'
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")

Expand Down
17 changes: 8 additions & 9 deletions src/Tldr/App/Handler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -94,20 +94,19 @@ 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
then return True
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
Expand Down
3 changes: 2 additions & 1 deletion src/Tldr/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ data ConsoleSetting =
, consoleIntensity :: ConsoleIntensity
}

newtype TldrOpts = TldrOpts
data TldrOpts = TldrOpts
{ tldrAction :: TldrCommand
, autoUpdateInterval :: Maybe Int
} deriving (Show)

data TldrCommand
Expand Down

0 comments on commit 042b12d

Please sign in to comment.