Skip to content

Commit bfad30d

Browse files
committed
Update when no cached version is available
1 parent eb1df29 commit bfad30d

3 files changed

Lines changed: 28 additions & 4 deletions

File tree

package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ library:
3838
- containers
3939
- http-conduit
4040
- zip-archive
41+
- time
4142

4243
ghc-options:
4344
- -Wall

src/Tldr/App/Handler.hs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import Data.List (intercalate)
1818
import Data.Semigroup ((<>))
1919
import qualified Data.Set as Set
2020
import Data.Version (showVersion)
21+
import Data.Time.Clock
22+
23+
import Control.Monad (when)
2124

2225
import Options.Applicative
2326
import Paths_tldr (version)
@@ -26,6 +29,8 @@ import System.Directory
2629
, createDirectory
2730
, removePathForcibly
2831
, doesFileExist
32+
, doesDirectoryExist
33+
, getModificationTime
2934
, getXdgDirectory
3035
)
3136
import System.Environment (lookupEnv, getExecutablePath)
@@ -68,6 +73,8 @@ handleTldrOpts opts@TldrOpts {..} =
6873
UpdateIndex -> updateTldrPages
6974
About -> handleAboutFlag
7075
ViewPage voptions pages -> do
76+
performUpdate <- updateNecessary
77+
when performUpdate updateTldrPages
7178
let npage = intercalate "-" pages
7279
locale <-
7380
case languageOption voptions of
@@ -87,6 +94,21 @@ handleTldrOpts opts@TldrOpts {..} =
8794
ViewPage (englishViewOptions voptions) pages
8895
})
8996

97+
-- We update if the data directory does not exist.
98+
-- We also update if the cached pages version is older than 7 days.
99+
-- TODO: Make the auto-update interval configurable.
100+
-- TODO: Add command line option to skip auto update.
101+
updateNecessary :: IO Bool
102+
updateNecessary = do
103+
dataDir <- getXdgDirectory XdgData tldrDirName
104+
dataDirExists <- doesDirectoryExist dataDir
105+
if not dataDirExists
106+
then return True
107+
else do
108+
lastCachedTime <- getModificationTime dataDir
109+
currentTime <- getCurrentTime
110+
return $ currentTime `diffUTCTime` lastCachedTime > 7 * nominalDay
111+
90112
updateTldrPages :: IO ()
91113
updateTldrPages = do
92114
dataDir <- getXdgDirectory XdgData tldrDirName

tldr.cabal

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cabal-version: 1.12
44
--
55
-- see: https://github.com/sol/hpack
66
--
7-
-- hash: 21691ba2cc19cdb8ae6d8a3bd0e84803e28b8866530a0da9fe44e6187df4fec1
7+
-- hash: 15cea8fa6220a4fef978ba47b6907236174a550cef396d6b25755d1549b6a5a5
88

99
name: tldr
1010
version: 0.8.1
@@ -50,7 +50,7 @@ library
5050
Paths_tldr
5151
hs-source-dirs:
5252
src
53-
ghc-options: -Wall
53+
ghc-options: -Wall -O2
5454
build-depends:
5555
ansi-terminal
5656
, base >=4.7 && <5
@@ -63,6 +63,7 @@ library
6363
, optparse-applicative
6464
, semigroups
6565
, text
66+
, time
6667
, zip-archive
6768
default-language: Haskell2010
6869

@@ -72,7 +73,7 @@ executable tldr
7273
Paths_tldr
7374
hs-source-dirs:
7475
app
75-
ghc-options: -Wall
76+
ghc-options: -Wall -O2
7677
build-depends:
7778
base
7879
, tldr
@@ -90,7 +91,7 @@ test-suite tldr-test
9091
Paths_tldr
9192
hs-source-dirs:
9293
test
93-
ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N
94+
ghc-options: -Wall -O2 -threaded -rtsopts -with-rtsopts=-N
9495
build-depends:
9596
base
9697
, tasty

0 commit comments

Comments
 (0)