summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpsibi <>2020-06-28 16:17:00 (GMT)
committerhdiff <hdiff@hdiff.luite.com>2020-06-28 16:17:00 (GMT)
commitbde1e026b8e41d1913b62824d58fd0703c47a614 (patch)
tree2203fe2dddf810a389209ac06245248590ef0792
parent5579a7452c4ea97450283c373ee8a698ebee89e8 (diff)
version 0.7.00.7.0
-rw-r--r--CHANGELOG.md4
-rw-r--r--app/Main.hs76
-rw-r--r--tldr.cabal16
3 files changed, 78 insertions, 18 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 346f8f1..37f2b19 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,7 @@
+# 0.7.0
+
+* Make it obey --language (-L) option.
+
# 0.6.4
* Fix cabal file
diff --git a/app/Main.hs b/app/Main.hs
index e55cde0..2cff22a 100644
--- a/app/Main.hs
+++ b/app/Main.hs
@@ -11,13 +11,14 @@ import Data.List (intercalate)
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,9 +32,15 @@ data TldrCommand
| 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 =
@@ -49,7 +56,7 @@ aboutFlag :: Parser TldrCommand
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 @@ platformFlag =
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 @@ pageExists fname = do
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 @@ handleAboutFlag = do
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
diff --git a/tldr.cabal b/tldr.cabal
index 649a274..97a1ad8 100644
--- a/tldr.cabal
+++ b/tldr.cabal
@@ -4,10 +4,10 @@ cabal-version: 1.12
--
-- 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 @@ source-repository head
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 @@ executable tldr
, 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