parent
cb5ccab636
commit
23910e70e3
|
@ -14,7 +14,6 @@ import Options.Applicative (Parser, ParserInfo)
|
|||
import qualified Control.Exception
|
||||
import qualified Data.Aeson
|
||||
import qualified Data.Aeson.Encode.Pretty
|
||||
import qualified Data.ByteString.Char8
|
||||
import qualified Data.ByteString.Lazy
|
||||
import qualified Data.Text.IO as Text.IO
|
||||
import qualified Dhall
|
||||
|
@ -33,6 +32,7 @@ data Options
|
|||
, conversion :: Conversion
|
||||
, approximateSpecialDoubles :: Bool
|
||||
, file :: Maybe FilePath
|
||||
, output :: Maybe FilePath
|
||||
}
|
||||
| Version
|
||||
|
||||
|
@ -45,6 +45,7 @@ parseOptions =
|
|||
<*> Dhall.JSON.parseConversion
|
||||
<*> parseApproximateSpecialDoubles
|
||||
<*> optional parseFile
|
||||
<*> optional parseOutput
|
||||
)
|
||||
<|> parseVersion
|
||||
where
|
||||
|
@ -94,6 +95,13 @@ parseOptions =
|
|||
<> Options.metavar "FILE"
|
||||
)
|
||||
|
||||
parseOutput =
|
||||
Options.strOption
|
||||
( Options.long "output"
|
||||
<> Options.help "Write JSON to a file instead of standard output"
|
||||
<> Options.metavar "FILE"
|
||||
)
|
||||
|
||||
parserInfo :: ParserInfo Options
|
||||
parserInfo =
|
||||
Options.info
|
||||
|
@ -137,7 +145,12 @@ main = do
|
|||
|
||||
json <- omission <$> explaining (Dhall.JSON.codeToValue conversion specialDoubleMode file text)
|
||||
|
||||
Data.ByteString.Char8.putStrLn $ Data.ByteString.Lazy.toStrict $ encode json
|
||||
let write =
|
||||
case output of
|
||||
Nothing -> Data.ByteString.Lazy.putStr
|
||||
Just file_ -> Data.ByteString.Lazy.writeFile file_
|
||||
|
||||
write (encode json <> "\n")
|
||||
|
||||
handle :: IO a -> IO a
|
||||
handle = Control.Exception.handle handler
|
||||
|
|
|
@ -29,6 +29,7 @@ parseOptions =
|
|||
<*> parseQuoted
|
||||
<*> Dhall.JSON.parseConversion
|
||||
<*> optional parseFile
|
||||
<*> optional parseOutput
|
||||
)
|
||||
<|> parseVersion
|
||||
where
|
||||
|
@ -52,6 +53,13 @@ parseOptions =
|
|||
<> Options.help "Display version"
|
||||
)
|
||||
|
||||
parseOutput =
|
||||
Options.strOption
|
||||
( Options.long "output"
|
||||
<> Options.help "Write YAML to a file instead of standard output"
|
||||
<> Options.metavar "FILE"
|
||||
)
|
||||
|
||||
parserInfo :: ParserInfo (Maybe Options)
|
||||
parserInfo =
|
||||
Options.info
|
||||
|
@ -76,7 +84,12 @@ main = do
|
|||
Nothing -> Text.IO.getContents
|
||||
Just path -> Text.IO.readFile path
|
||||
|
||||
Data.ByteString.putStr =<< dhallToYaml options file contents
|
||||
let write =
|
||||
case output of
|
||||
Nothing -> Data.ByteString.putStr
|
||||
Just file_ -> Data.ByteString.writeFile file_
|
||||
|
||||
write =<< dhallToYaml options file contents
|
||||
|
||||
handle :: IO a -> IO a
|
||||
handle = Control.Exception.handle handler
|
||||
|
|
|
@ -23,6 +23,7 @@ import qualified Data.ByteString.Lazy.Char8 as ByteString
|
|||
import qualified Data.Text.IO as Text.IO
|
||||
import qualified Data.Text.Prettyprint.Doc as Pretty
|
||||
import qualified Data.Text.Prettyprint.Doc.Render.Terminal as Pretty.Terminal
|
||||
import qualified Data.Text.Prettyprint.Doc.Render.Text as Pretty.Text
|
||||
import qualified GHC.IO.Encoding
|
||||
import qualified Options.Applicative as Options
|
||||
import qualified System.Console.ANSI as ANSI
|
||||
|
@ -49,6 +50,7 @@ data Options
|
|||
{ schema :: Text
|
||||
, conversion :: Conversion
|
||||
, file :: Maybe FilePath
|
||||
, output :: Maybe FilePath
|
||||
, ascii :: Bool
|
||||
, plain :: Bool
|
||||
}
|
||||
|
@ -62,6 +64,7 @@ parseOptions =
|
|||
<$> parseSchema
|
||||
<*> parseConversion
|
||||
<*> optional parseFile
|
||||
<*> optional parseOutput
|
||||
<*> parseASCII
|
||||
<*> parsePlain
|
||||
)
|
||||
|
@ -88,6 +91,13 @@ parseOptions =
|
|||
<> Options.metavar "FILE"
|
||||
)
|
||||
|
||||
parseOutput =
|
||||
Options.strOption
|
||||
( Options.long "output"
|
||||
<> Options.help "Write Dhall expression to a file instead of standard output"
|
||||
<> Options.metavar "FILE"
|
||||
)
|
||||
|
||||
parseASCII =
|
||||
Options.switch
|
||||
( Options.long "ascii"
|
||||
|
@ -138,16 +148,24 @@ main = do
|
|||
|
||||
let stream = Pretty.layoutSmart Dhall.Pretty.layoutOpts document
|
||||
|
||||
supportsANSI <- ANSI.hSupportsANSI IO.stdout
|
||||
case output of
|
||||
Nothing -> do
|
||||
supportsANSI <- ANSI.hSupportsANSI IO.stdout
|
||||
|
||||
let ansiStream =
|
||||
if supportsANSI && not plain
|
||||
then fmap Dhall.Pretty.annToAnsiStyle stream
|
||||
else Pretty.unAnnotateS stream
|
||||
let ansiStream =
|
||||
if supportsANSI && not plain
|
||||
then fmap Dhall.Pretty.annToAnsiStyle stream
|
||||
else Pretty.unAnnotateS stream
|
||||
|
||||
Pretty.Terminal.renderIO IO.stdout ansiStream
|
||||
Pretty.Terminal.renderIO IO.stdout ansiStream
|
||||
|
||||
Text.IO.putStrLn ""
|
||||
Text.IO.putStrLn ""
|
||||
|
||||
Just file_ ->
|
||||
IO.withFile file_ IO.WriteMode $ \h -> do
|
||||
Pretty.Text.renderIO h stream
|
||||
|
||||
Text.IO.hPutStrLn h ""
|
||||
|
||||
handle :: IO a -> IO a
|
||||
handle = Control.Exception.handle handler
|
||||
|
|
|
@ -38,6 +38,7 @@ data Options = Options
|
|||
, quoted :: Bool
|
||||
, conversion :: Conversion
|
||||
, file :: Maybe FilePath
|
||||
, output :: Maybe FilePath
|
||||
}
|
||||
|
||||
defaultOptions :: Options
|
||||
|
@ -48,6 +49,7 @@ defaultOptions =
|
|||
, quoted = False
|
||||
, conversion = NoConversion
|
||||
, file = Nothing
|
||||
, output = Nothing
|
||||
}
|
||||
|
||||
parseDocuments :: Parser Bool
|
||||
|
|
|
@ -23,6 +23,7 @@ import qualified Data.ByteString.Char8 as BSL8
|
|||
import qualified Data.Text.IO as Text.IO
|
||||
import qualified Data.Text.Prettyprint.Doc as Pretty
|
||||
import qualified Data.Text.Prettyprint.Doc.Render.Terminal as Pretty.Terminal
|
||||
import qualified Data.Text.Prettyprint.Doc.Render.Text as Pretty.Text
|
||||
import qualified Dhall.Pretty
|
||||
import qualified GHC.IO.Encoding
|
||||
import qualified Options.Applicative as Options
|
||||
|
@ -40,6 +41,7 @@ data CommandOptions
|
|||
{ schema :: Text
|
||||
, conversion :: Conversion
|
||||
, file :: Maybe FilePath
|
||||
, output :: Maybe FilePath
|
||||
, ascii :: Bool
|
||||
, plain :: Bool
|
||||
}
|
||||
|
@ -61,6 +63,7 @@ parseOptions =
|
|||
<$> parseSchema
|
||||
<*> parseConversion
|
||||
<*> optional parseFile
|
||||
<*> optional parseOutput
|
||||
<*> parseASCII
|
||||
<*> parsePlain
|
||||
)
|
||||
|
@ -87,6 +90,13 @@ parseOptions =
|
|||
<> Options.metavar "FILE"
|
||||
)
|
||||
|
||||
parseOutput =
|
||||
Options.strOption
|
||||
( Options.long "output"
|
||||
<> Options.help "Write Dhall expression to a file instead of standard output"
|
||||
<> Options.metavar "FILE"
|
||||
)
|
||||
|
||||
parseASCII =
|
||||
Options.switch
|
||||
( Options.long "ascii"
|
||||
|
@ -129,17 +139,24 @@ main = do
|
|||
|
||||
let stream = Pretty.layoutSmart Dhall.Pretty.layoutOpts document
|
||||
|
||||
supportsANSI <- ANSI.hSupportsANSI IO.stdout
|
||||
case output of
|
||||
Nothing -> do
|
||||
supportsANSI <- ANSI.hSupportsANSI IO.stdout
|
||||
|
||||
let ansiStream =
|
||||
if supportsANSI && not plain
|
||||
then fmap Dhall.Pretty.annToAnsiStyle stream
|
||||
else Pretty.unAnnotateS stream
|
||||
let ansiStream =
|
||||
if supportsANSI && not plain
|
||||
then fmap Dhall.Pretty.annToAnsiStyle stream
|
||||
else Pretty.unAnnotateS stream
|
||||
|
||||
Pretty.Terminal.renderIO IO.stdout ansiStream
|
||||
Pretty.Terminal.renderIO IO.stdout ansiStream
|
||||
|
||||
Text.IO.putStrLn ""
|
||||
Text.IO.putStrLn ""
|
||||
|
||||
Just file_ ->
|
||||
IO.withFile file_ IO.WriteMode $ \h -> do
|
||||
Pretty.Text.renderIO h stream
|
||||
|
||||
Text.IO.hPutStrLn h ""
|
||||
|
||||
handle :: IO a -> IO a
|
||||
handle = Control.Exception.handle handler
|
||||
|
|
Loading…
Reference in New Issue