on master help sign in
Allow reading from stdin
cad7e40Tuncer Ayaz
app/Main.hs+22−8
@@ -13,6 +13,7 @@ import Options.Applicative
13import Ormolu
13import Ormolu
14import Paths_ormolu (version)
14import Paths_ormolu (version)
15import System.Exit (ExitCode (..), exitWith)
15import System.Exit (ExitCode (..), exitWith)
16+import System.IO (hPutStrLn, stderr)
16import qualified Data.Text.IO as TIO
17import qualified Data.Text.IO as TIO
17import qualified Data.Yaml as Yaml
18import qualified Data.Yaml as Yaml
18
19
@@ -26,18 +27,30 @@ main = withPrettyOrmoluExceptions $ do
26 Just path -> do
27 Just path -> do
27 config <- Yaml.decodeFileThrow path
28 config <- Yaml.decodeFileThrow path
28 return (config <> optConfig)
29 return (config <> optConfig)
30+ r <- case optInputFile of
31+ "-" -> ormoluStdin config
32+ inputFile -> ormoluFile config inputFile
33+ let notForStdin = do
29− r <- ormoluFile config optInputFile
34+ when (optInputFile == "-") $ do
35+ hPutStrLn
36+ stderr
37+ "This feature is not supported when input comes from stdin."
38+ -- 101 is different from all the other exit codes we already use.
39+ exitWith (ExitFailure 101)
30 case optMode of
40 case optMode of
31 Stdout ->
41 Stdout ->
32 TIO.putStr r
42 TIO.putStr r
33− InPlace ->
43+ InPlace -> do
44+ notForStdin
34 TIO.writeFile optInputFile r
45 TIO.writeFile optInputFile r
35 Check -> do
46 Check -> do
47+ notForStdin
36 r' <- TIO.readFile optInputFile
48 r' <- TIO.readFile optInputFile
37− when (r /= r') . exitWith $
49+ when (r /= r') $
38− ExitFailure 100 -- 100 is different to all the other exit code that
50+ -- 100 is different to all the other exit code that are emitted
39− -- are emitted either from an 'OrmoluException' or
51+ -- either from an 'OrmoluException' or from 'error' and
40− -- from 'error' and 'notImplemented'.
52+ -- 'notImplemented'.
53+ exitWith (ExitFailure 100)
41
54
42----------------------------------------------------------------------------
55----------------------------------------------------------------------------
43-- Command line options parsing.
56-- Command line options parsing.
@@ -50,7 +63,7 @@ data Opts = Opts
50 , optConfig :: !Config
63 , optConfig :: !Config
51 -- ^ Ormolu 'Config'
64 -- ^ Ormolu 'Config'
52 , optInputFile :: !FilePath
65 , optInputFile :: !FilePath
53− -- ^ Input source file
66+ -- ^ Input source file or stdin ("-")
54 }
67 }
55
68
56-- | Mode of operation.
69-- | Mode of operation.
@@ -103,7 +116,8 @@ optsParser = Opts
103 <*> configParser
116 <*> configParser
104 <*> (strArgument . mconcat)
117 <*> (strArgument . mconcat)
105 [ metavar "FILE"
118 [ metavar "FILE"
119+ , value "-"
106− , help "Haskell source file to format"
120+ , help "Haskell source file to format or stdin (default)"
107 ]
121 ]
108
122
109configParser :: Parser Config
123configParser :: Parser Config
src/Ormolu.hs+14
@@ -5,6 +5,7 @@
5module Ormolu
5module Ormolu
6 ( ormolu
6 ( ormolu
7 , ormoluFile
7 , ormoluFile
8+ , ormoluStdin
8 , Config (..)
9 , Config (..)
9 , defaultConfig
10 , defaultConfig
10 , DynOption (..)
11 , DynOption (..)
@@ -28,6 +29,7 @@ import Ormolu.Utils (showOutputable)
28import qualified CmdLineParser as GHC
29import qualified CmdLineParser as GHC
29import qualified Data.Text as T
30import qualified Data.Text as T
30import qualified GHC
31import qualified GHC
32+import System.IO (hGetContents, stdin)
31
33
32-- | Format a 'String', return formatted version as 'Text'.
34-- | Format a 'String', return formatted version as 'Text'.
33--
35--
@@ -77,6 +79,18 @@ ormoluFile
77ormoluFile cfg path =
79ormoluFile cfg path =
78 liftIO (readFile path) >>= ormolu cfg path
80 liftIO (readFile path) >>= ormolu cfg path
79
81
82+-- | Read input from stdin and format it.
83+--
84+-- > ormoluStdin cfg =
85+-- > liftIO (hGetContents stdin) >>= ormolu cfg "<stdin>"
86+
87+ormoluStdin
88+ :: MonadIO m
89+ => Config -- ^ Ormolu configuration
90+ -> m Text -- ^ Resulting rendition
91+ormoluStdin cfg =
92+ liftIO (hGetContents stdin) >>= ormolu cfg "<stdin>"
93+
80----------------------------------------------------------------------------
94----------------------------------------------------------------------------
81-- Helpers
95-- Helpers
82
96