@@ -23,6 +23,7 @@ import qualified Data.ByteString.Lazy.Char8 as ByteString
2323import qualified Data.Text.IO as Text.IO
2424import qualified Data.Text.Prettyprint.Doc as Pretty
2525import qualified Data.Text.Prettyprint.Doc.Render.Terminal as Pretty.Terminal
26+ import qualified Data.Text.Prettyprint.Doc.Render.Text as Pretty.Text
2627import qualified GHC.IO.Encoding
2728import qualified Options.Applicative as Options
2829import qualified System.Console.ANSI as ANSI
@@ -49,6 +50,7 @@ data Options
4950 { schema :: Text
5051 , conversion :: Conversion
5152 , file :: Maybe FilePath
53+ , output :: Maybe FilePath
5254 , ascii :: Bool
5355 , plain :: Bool
5456 }
@@ -62,6 +64,7 @@ parseOptions =
6264 <$> parseSchema
6365 <*> parseConversion
6466 <*> optional parseFile
67+ <*> optional parseOutput
6568 <*> parseASCII
6669 <*> parsePlain
6770 )
@@ -88,6 +91,13 @@ parseOptions =
8891 <> Options. metavar " FILE"
8992 )
9093
94+ parseOutput =
95+ Options. strOption
96+ ( Options. long " output"
97+ <> Options. help " Write Dhall expression to a file instead of standard output"
98+ <> Options. metavar " FILE"
99+ )
100+
91101 parseASCII =
92102 Options. switch
93103 ( Options. long " ascii"
@@ -138,16 +148,24 @@ main = do
138148
139149 let stream = Pretty. layoutSmart Dhall.Pretty. layoutOpts document
140150
141- supportsANSI <- ANSI. hSupportsANSI IO. stdout
151+ case output of
152+ Nothing -> do
153+ supportsANSI <- ANSI. hSupportsANSI IO. stdout
154+
155+ let ansiStream =
156+ if supportsANSI && not plain
157+ then fmap Dhall.Pretty. annToAnsiStyle stream
158+ else Pretty. unAnnotateS stream
159+
160+ Pretty.Terminal. renderIO IO. stdout ansiStream
142161
143- let ansiStream =
144- if supportsANSI && not plain
145- then fmap Dhall.Pretty. annToAnsiStyle stream
146- else Pretty. unAnnotateS stream
162+ Text.IO. putStrLn " "
147163
148- Pretty.Terminal. renderIO IO. stdout ansiStream
164+ Just file_ ->
165+ IO. withFile file_ IO. WriteMode $ \ h -> do
166+ Pretty.Text. renderIO h stream
149167
150- Text.IO. putStrLn " "
168+ Text.IO. hPutStrLn h " "
151169
152170handle :: IO a -> IO a
153171handle = Control.Exception. handle handler
0 commit comments