Skip to content

Commit 8bf79e2

Browse files
committed
Enhance command line for stdin
1 parent 10bbc53 commit 8bf79e2

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

app/Internal/Opts.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ data Pager
2626

2727
data Command
2828
= OneFile FilePath
29-
| Stdin (Maybe Highlighter)
29+
| Stdin (Maybe Highlighter) (Maybe FilePath)
3030
| MakeSCSS
3131
| Setup Bool Bool
3232

@@ -82,7 +82,8 @@ optsParser = info (optsParse <**> helper) idm
8282

8383
fileCmd = info (OneFile <$> (argument str (metavar "PATHNAME")))
8484
(progDesc "Take in a single file, given by a pathname")
85-
stdinCmd = info (Stdin <$> (fmap (fmap highlighterArg) $ (optional $ argument str (metavar "HIGHLIGHTER"))))
85+
stdinCmd = info (Stdin <$> (fmap (fmap highlighterArg) $ (optional (strOption (long "highlighter"))))
86+
<*> optional ( strOption (long "filename" )))
8687
(progDesc "Take from stdin")
8788
makeSCSS = info (pure MakeSCSS)
8889
(progDesc "Generate the SCSS for the requrested theme")

app/Main.hs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ import GHC.IO.Exception (IOErrorType(ResourceVanished), IOE
3838
import Paths_fancydiff (version)
3939
import Fancydiff.Lib (tryDiffWithSourceHighlight,
4040
commitHighlight,
41+
getHighlighterByFilename,
4142
getHighlighterFunc,
4243
getHighlighterFuncByFilename)
4344
import Fancydiff.Formatting (fshow)
45+
import qualified Fancydiff.SourceHighlight as SH
4446
import Fancydiff.AnsiFormatting (ansiFormatting)
4547
import Fancydiff.HTMLFormatting ( htmlFormatting
4648
, mkHtmlFormat
@@ -132,6 +134,10 @@ mainOpts opts@Opts{..} = do
132134
exitFailure
133135
Right ok -> liftIO $ T.hPutStr outHandle $ fmt ok
134136

137+
formatOneHighlighter fmt outHandle highlighter = do
138+
content <- B.hGetContents stdin
139+
formatOne fmt content outHandle (getHighlighterFunc highlighter)
140+
135141
onCmd _ (Setup onlyAliases isLocal) _ = do
136142
let git'print params = do
137143
T.putStrLn $ T.concat $ ["Running: "] ++ (intersperse " " $ map onParam params)
@@ -167,7 +173,7 @@ mainOpts opts@Opts{..} = do
167173
content <- B.readFile filepath
168174
formatOne fmt content outHandle (getHighlighterFuncByFilename (T.pack filepath))
169175

170-
onCmd fmt (Stdin Nothing) outHandle = do
176+
onCmd fmt (Stdin Nothing Nothing) outHandle = do
171177
let path = "."
172178
withRepository lgFactory path $ do
173179
let diffStart = B.isPrefixOf "diff "
@@ -183,9 +189,14 @@ mainOpts opts@Opts{..} = do
183189
liftIO $ T.hPutStr outHandle $ fmt res
184190
return ()
185191

186-
onCmd fmt (Stdin (Just highlighter)) outHandle = do
187-
content <- B.hGetContents stdin
188-
formatOne fmt content outHandle (getHighlighterFunc highlighter)
192+
onCmd fmt (Stdin (Just highlighter) Nothing) outHandle = do
193+
formatOneHighlighter fmt outHandle highlighter
194+
195+
onCmd fmt (Stdin _ (Just filename)) outHandle = do
196+
let highlighter = getHighlighterByFilename $ T.pack filename
197+
case highlighter of
198+
SH.Generic -> exitFailure
199+
_ -> formatOneHighlighter fmt outHandle highlighter
189200

190201
main :: IO ()
191202
main = void $ execParser optsParser >>= mainOpts

0 commit comments

Comments
 (0)