tldr 0.6.4 → 0.7.0
raw patch · 3 files changed
+78/−18 lines, 3 files
Files
- CHANGELOG.md +4/−0
- app/Main.hs +63/−13
- tldr.cabal +11/−5
CHANGELOG.md view
@@ -1,3 +1,7 @@+# 0.7.0++* Make it obey --language (-L) option.+ # 0.6.4 * Fix cabal file
app/Main.hs view
@@ -11,13 +11,14 @@ import Data.Semigroup ((<>)) import qualified Data.Set as Set import Data.Version (showVersion)-import GHC.IO.Handle.FD (stdout)+import System.IO (stdout) import Options.Applicative import Paths_tldr (version) import System.Directory-import System.Environment (getArgs, getExecutablePath)+import System.Environment (getArgs, getExecutablePath, lookupEnv) import System.FilePath import System.Process.Typed+import Data.Char (toLower) import Tldr data TldrOpts = TldrOpts@@ -31,10 +32,16 @@ | About deriving (Show, Eq, Ord) -data ViewOptions = ViewOptions- { platformOption :: Maybe String- } deriving (Show, Eq, Ord)+data ViewOptions =+ ViewOptions+ { platformOption :: Maybe String+ , languageOption :: Maybe String+ }+ deriving (Show, Eq, Ord) +englishViewOptions :: ViewOptions -> ViewOptions+englishViewOptions xs = xs { languageOption = Just "en_US.utf8" }+ programOptions :: Parser TldrOpts programOptions = (TldrOpts <$> (updateIndexCommand <|> viewPageCommand <|> aboutFlag))@@ -49,7 +56,7 @@ aboutFlag = flag' About (long "about" <> short 'a' <> help "About this program") viewOptionsParser :: Parser ViewOptions-viewOptionsParser = ViewOptions <$> platformFlag+viewOptionsParser = ViewOptions <$> platformFlag <*> languageFlag viewPageCommand :: Parser TldrCommand viewPageCommand =@@ -68,6 +75,14 @@ platformHelpValue :: String platformHelpValue = intercalate ", " platformDirs +languageFlag :: Parser (Maybe String)+languageFlag =+ optional+ (strOption+ (long "language" <> short 'L' <> metavar "LOCALE" <>+ help+ ("Preferred language for the page returned")))+ tldrDirName :: String tldrDirName = "tldr" @@ -127,10 +142,15 @@ then return $ Just fname else return Nothing -getPagePath :: String -> [String] -> IO (Maybe FilePath)-getPagePath page platformDirs = do+getPagePath :: Locale -> String -> [String] -> IO (Maybe FilePath)+getPagePath locale page platformDirs = do dataDir <- getXdgDirectory XdgData tldrDirName- let pageDir = dataDir </> "tldr" </> "pages"+ let currentLocale = case locale of+ English -> "pages"+ Other xs -> "pages." <> xs+ Unknown xs -> "pages." <> xs+ Missing -> "pages"+ pageDir = dataDir </> "tldr" </> currentLocale paths = map (\x -> pageDir </> x </> page <.> "md") platformDirs foldr1 (<|>) <$> mapM pageExists paths @@ -161,16 +181,46 @@ putStr content handleTldrOpts :: TldrOpts -> IO ()-handleTldrOpts TldrOpts {..} = do+handleTldrOpts opts@TldrOpts {..} = do case tldrAction of UpdateIndex -> updateTldrPages About -> handleAboutFlag- ViewPage voptions pages -> do+ vopts@(ViewPage voptions pages) -> do let npage = intercalate "-" pages- fname <- getPagePath npage (getCheckDirs voptions)+ locale <-+ case (languageOption voptions) of+ Nothing -> retriveLocale+ Just lg -> pure $ computeLocale (Just lg)+ fname <- getPagePath locale npage (getCheckDirs voptions) case fname of Just path -> renderPage path stdout- Nothing -> putStrLn ("No tldr entry for " <> (intercalate " " pages))+ Nothing -> do+ if checkLocale locale+ then putStrLn ("No tldr entry for " <> (intercalate " " pages))+ else handleTldrOpts+ (opts+ { tldrAction =+ ViewPage (englishViewOptions voptions) pages+ })++checkLocale :: Locale -> Bool+checkLocale English = True+checkLocale _ = False++data Locale = English | Missing | Other String | Unknown String++retriveLocale :: IO Locale+retriveLocale = do+ lang <- lookupEnv "LANG"+ pure $ computeLocale lang+ +computeLocale :: Maybe String -> Locale+computeLocale lang = case map toLower <$> lang of+ Nothing -> Missing+ Just ('e':'n':_) -> English+ Just (a:b:'_':_) -> Other (a:b:[])+ Just (a:b:c:'_':_) -> Other (a:b:c:[])+ Just str -> Unknown str main :: IO () main = do
tldr.cabal view
@@ -4,10 +4,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: 6d6a28bd0b56fd00a272f305ae388900bd3a5e235b44afb32bd4e758846bf2f7+-- hash: 88d378cdcf8422ec2651e3d350ffcb2f0a4f7a46dce43516286864b209522d78 name: tldr-version: 0.6.4+version: 0.7.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.@@ -34,6 +34,11 @@ type: git location: https://github.com/psibi/tldr-hs +flag static+ description: Statically link executables.+ manual: True+ default: False+ library exposed-modules: Tldr@@ -64,10 +69,11 @@ , semigroups , tldr , typed-process- if os(linux)- ghc-options: -threaded -optl-pthread -rtsopts -with-rtsopts=-N+ if flag(static) && os(linux)+ ghc-options: -rtsopts -threaded -optc-Os -optl=-pthread -optl=-static -fPIC+ ld-options: -static else- ghc-options: -threaded -rtsopts -with-rtsopts=-N+ ghc-options: -rtsopts -threaded default-language: Haskell2010 test-suite tldr-test