Skip to content

Commit deff89e

Browse files
committed
Implement --about flag
Resolves #19
1 parent ec0f029 commit deff89e

1 file changed

Lines changed: 27 additions & 13 deletions

File tree

app/Main.hs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,39 @@ import GHC.IO.Handle.FD (stdout)
1515
import Options.Applicative
1616
import Paths_tldr (version)
1717
import System.Directory
18-
import System.Environment (getArgs)
18+
import System.Environment (getArgs, getExecutablePath)
1919
import System.FilePath
2020
import System.Process.Typed
2121
import Tldr
2222

23-
data TldrOpts =
24-
TldrOpts
25-
{ tldrAction :: TldrCommand
26-
}
27-
deriving (Show)
23+
data TldrOpts = TldrOpts
24+
{ tldrAction :: TldrCommand
25+
} deriving (Show)
2826

2927
data TldrCommand
3028
= UpdateIndex
31-
| ViewPage ViewOptions [String]
29+
| ViewPage ViewOptions
30+
[String]
31+
| About
3232
deriving (Show, Eq, Ord)
3333

34-
data ViewOptions =
35-
ViewOptions
36-
{ platformOption :: Maybe String
37-
}
38-
deriving (Show, Eq, Ord)
34+
data ViewOptions = ViewOptions
35+
{ platformOption :: Maybe String
36+
} deriving (Show, Eq, Ord)
3937

4038
programOptions :: Parser TldrOpts
41-
programOptions = (TldrOpts <$> (updateIndexCommand <|> viewPageCommand))
39+
programOptions =
40+
(TldrOpts <$> (updateIndexCommand <|> viewPageCommand <|> aboutFlag))
4241

4342
updateIndexCommand :: Parser TldrCommand
4443
updateIndexCommand =
4544
flag'
4645
UpdateIndex
4746
(long "update" <> short 'u' <> help "Update offline cache of tldr pages")
4847

48+
aboutFlag :: Parser TldrCommand
49+
aboutFlag = flag' About (long "about" <> short 'a' <> help "About this program")
50+
4951
viewOptionsParser :: Parser ViewOptions
5052
viewOptionsParser = ViewOptions <$> platformFlag
5153

@@ -147,10 +149,22 @@ nubOrd = loop mempty
147149
| a `Set.member` s = loop s as
148150
| otherwise = a : loop (Set.insert a s) as
149151

152+
handleAboutFlag :: IO ()
153+
handleAboutFlag = do
154+
path <- getExecutablePath
155+
let content =
156+
unlines
157+
[ path <> " v" <> (showVersion version)
158+
, "Copyright (C) 2017 Sibi Prabakaran"
159+
, "Source available at https://github.com/psibi/tldr-hs"
160+
]
161+
putStr content
162+
150163
handleTldrOpts :: TldrOpts -> IO ()
151164
handleTldrOpts TldrOpts {..} = do
152165
case tldrAction of
153166
UpdateIndex -> updateTldrPages
167+
About -> handleAboutFlag
154168
ViewPage voptions pages -> do
155169
let npage = intercalate "-" pages
156170
fname <- getPagePath npage (getCheckDirs voptions)

0 commit comments

Comments
 (0)