packages feed

tldr 0.8.0 → 0.9.0

raw patch · 9 files changed

+95/−40 lines, 9 filesdep +http-conduitdep +timedep +zip-archivedep −typed-processsetup-changed

Dependencies added: http-conduit, time, zip-archive

Dependencies removed: typed-process

Files

CHANGELOG.md view
@@ -1,3 +1,8 @@+# 0.9.0++* When pages are updated, the client now shows the download location.+* Add optional auto-update functionality (`--auto-update-interval`)+ # 0.8.0  * Split the library into more parts.
README.md view
@@ -8,6 +8,18 @@  Haskell client for tldr +<!-- markdown-toc start - Don't edit this section. Run M-x markdown-toc-refresh-toc -->+**Table of Contents**++- [tldr](#tldr)+    - [Installation](#installation)+    - [Usage](#usage)+    - [Offline caching](#offline-caching)+    - [Snapshot](#snapshot)++<!-- markdown-toc end -->++ ## Installation  See Github releases: https://github.com/psibi/tldr-hs/releases@@ -38,6 +50,9 @@   -L,--language LOCALE     Preferred language for the page returned   COMMAND                  name of the command   -a,--about               About this program+  --auto-update-interval DAYS+                           Perform an automatic update if the cache is older+                           than DAYS ```  Or a much better example of the usage:@@ -45,7 +60,7 @@ ``` shellsession $ tldr tldr tldr-Simplified man pages.More information: https://tldr.sh.+Simplified man pages. More information: https://tldr.sh.   - Get typical usages of a command (hint: this is how you got here!):    tldr {{command}}@@ -56,6 +71,24 @@  - Get help for a git subcommand:    tldr {{git checkout}} ```++## Offline caching++On the first run, this program caches all available tldr pages. Since+the number of available tldr pages rises quickly, it is recommended to+regularly update the cache.  Such an update can be run manually with:++``` shellsession+$ tldr --update+```++Users of this client can enable automatic updates by running it with+the option `--auto-update-interval DAYS` specified.  The client will+then check whether the cached version of the tldr pages is older than+`DAYS` days and perform an update in that case.  To enable this+functionality permanently, users can put the line `alias tldr="tldr+--auto-update-interval DAYS"` in their shell configuration file+(e.g. `.bashrc`, `.zshrc`) with the desired update interval specified.  ## Snapshot 
Setup.hs view
@@ -1,2 +1,2 @@-import Distribution.Simple+import           Distribution.Simple main = defaultMain
app/Main.hs view
@@ -1,6 +1,6 @@ module Main where -import Tldr.App (appMain)+import           Tldr.App                       ( appMain )  main :: IO () main = appMain
src/Tldr/App.hs view
@@ -17,13 +17,21 @@  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")
src/Tldr/App/Constant.hs view
@@ -3,8 +3,8 @@ tldrDirName :: String tldrDirName = "tldr" -repoHttpsUrl :: String-repoHttpsUrl = "https://github.com/tldr-pages/tldr.git"+pagesUrl :: String+pagesUrl = "https://tldr.sh/assets/tldr.zip"  checkDirs :: [String] checkDirs = "common" : platformDirs
src/Tldr/App/Handler.hs view
@@ -1,5 +1,5 @@-{-#LANGUAGE RecordWildCards#-}-{-# LANGUAGE BangPatterns #-}+{-# LANGUAGE RecordWildCards #-}+{-# LANGUAGE BangPatterns    #-}  module Tldr.App.Handler   ( handleAboutFlag@@ -7,33 +7,36 @@   , checkLocale   , englishViewOptions   , getCheckDirs-  , initializeTldrPages   , pageExists   , getPagePath   , updateTldrPages   , handleTldrOpts   ) where -import Control.Monad (unless) import Data.Char (toLower) import Data.List (intercalate) import Data.Semigroup ((<>)) import qualified Data.Set as Set import Data.Version (showVersion)+import Data.Time.Clock+import Control.Monad (when) import Options.Applicative import Paths_tldr (version) import System.Directory   ( XdgDirectory(..)-  , createDirectoryIfMissing-  , doesDirectoryExist+  , createDirectory+  , removePathForcibly   , doesFileExist+  , doesDirectoryExist+  , getModificationTime   , getXdgDirectory   ) import System.Environment (lookupEnv, getExecutablePath) import System.Exit (exitFailure) import System.FilePath ((<.>), (</>)) import System.IO (hPutStrLn, stderr, stdout)-import System.Process.Typed+import Network.HTTP.Simple+import Codec.Archive.Zip import Tldr import Tldr.App.Constant import Tldr.Types@@ -67,6 +70,8 @@     UpdateIndex -> updateTldrPages     About -> handleAboutFlag     ViewPage voptions pages -> do+      shouldPerformUpdate <- updateNecessary opts+      when shouldPerformUpdate updateTldrPages        let npage = intercalate "-" pages       locale <-         case languageOption voptions of@@ -86,15 +91,29 @@                           ViewPage (englishViewOptions voptions) pages                       }) +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+      let diffExceedsLimit limit +            = currentTime `diffUTCTime` lastCachedTime +              > fromIntegral limit * nominalDay+      return $ maybe False diffExceedsLimit autoUpdateInterval+ updateTldrPages :: IO () updateTldrPages = do   dataDir <- getXdgDirectory XdgData tldrDirName-  let repoDir = dataDir </> "tldr"-  repoExists <- doesDirectoryExist repoDir-  if repoExists-    then runProcess_ $-         setWorkingDir repoDir $ proc "git" ["pull", "origin", "master"]-    else initializeTldrPages+  removePathForcibly dataDir+  createDirectory dataDir+  putStrLn $ "Downloading tldr pages to " ++ dataDir+  response <- httpLBS $ parseRequest_ pagesUrl+  let zipArchive = toArchive $ getResponseBody response+  extractFilesFromArchive [OptDestination dataDir] zipArchive  computeLocale :: Maybe String -> Locale computeLocale lang = case map toLower <$> lang of@@ -112,7 +131,7 @@                         Other xs -> "pages." <> xs                         Unknown xs -> "pages." <> xs                         Missing -> "pages"-      pageDir = dataDir </> "tldr" </> currentLocale+      pageDir = dataDir </> currentLocale       paths = map (\x -> pageDir </> x </> page <.> "md") pDirs   foldr1 (<|>) <$> mapM pageExists paths @@ -123,21 +142,6 @@     then return $ Just fname     else return Nothing -tldrInitialized :: IO Bool-tldrInitialized = do-  dataDir <- getXdgDirectory XdgData tldrDirName-  let dir2 = dataDir </> "tldr"-      pages = dataDir </> "tldr" </> "pages"-  exists <- mapM doesDirectoryExist [dataDir, dir2, pages]-  return $ all (== True) exists--initializeTldrPages :: IO ()-initializeTldrPages = do-  initialized <- tldrInitialized-  unless initialized $ do-    dataDir <- getXdgDirectory XdgData tldrDirName-    createDirectoryIfMissing False dataDir-    runProcess_ $ setWorkingDir dataDir $ proc "git" ["clone", repoHttpsUrl]  getCheckDirs :: ViewOptions -> [String] getCheckDirs voptions =
src/Tldr/Types.hs view
@@ -15,8 +15,9 @@     , consoleIntensity :: ConsoleIntensity     } -newtype TldrOpts = TldrOpts+data TldrOpts = TldrOpts   { tldrAction :: TldrCommand+  , autoUpdateInterval :: Maybe Int   } deriving (Show)  data TldrCommand
tldr.cabal view
@@ -4,10 +4,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: 6227f2af49b8d2a6bce7e1b2f3b3dd1df2bfe9922b13aaa8e10824c73f2d1b51+-- hash: 8f1a3267eb79f7615c0d4cc731807ebde3d23a3dea7fc605e0dd77b39c458d9e  name:           tldr-version:        0.8.0+version:        0.9.0 synopsis:       Haskell tldr client description:    Haskell tldr client with support for viewing tldr pages. Has offline                 cache for accessing pages. Visit https://tldr.sh for more details.@@ -50,6 +50,7 @@       Paths_tldr   hs-source-dirs:       src+  ghc-options: -Wall -O2   build-depends:       ansi-terminal     , base >=4.7 && <5@@ -58,10 +59,12 @@     , containers     , directory     , filepath+    , http-conduit     , optparse-applicative     , semigroups     , text-    , typed-process+    , time+    , zip-archive   default-language: Haskell2010  executable tldr@@ -70,6 +73,7 @@       Paths_tldr   hs-source-dirs:       app+  ghc-options: -Wall -O2   build-depends:       base     , tldr@@ -87,7 +91,7 @@       Paths_tldr   hs-source-dirs:       test-  ghc-options: -threaded -rtsopts -with-rtsopts=-N+  ghc-options: -Wall -O2 -threaded -rtsopts -with-rtsopts=-N   build-depends:       base     , tasty