on master help sign in
Allow reading from stdin
commitcad7e40ae59141319f705d06ee6f479fea7705c8 authorTuncer Ayaz <tuncer.ayaz@gmail.com> dateThu 4 Jul 2019, 00:52 (7 years ago) parent3be1adb
2 files changed+36−8
app/Main.hs+22−8 src/Ormolu.hs+14
app/Main.hs+22−8
@@ -13,6 +13,7 @@ import Options.Applicative
1313import Ormolu
1414import Paths_ormolu (version)
1515import System.Exit (ExitCode (..), exitWith)
16+import System.IO (hPutStrLn, stderr)
1617import qualified Data.Text.IO as TIO
1718import qualified Data.Yaml as Yaml
1819
@@ -26,18 +27,30 @@ main = withPrettyOrmoluExceptions $ do
2627 Just path -> do
2728 config <- Yaml.decodeFileThrow path
2829 return (config <> optConfig)
29− r <- ormoluFile config optInputFile
30+ r <- case optInputFile of
31+ "-" -> ormoluStdin config
32+ inputFile -> ormoluFile config inputFile
33+ let notForStdin = do
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)
3040 case optMode of
3141 Stdout ->
3242 TIO.putStr r
33− InPlace ->
43+ InPlace -> do
44+ notForStdin
3445 TIO.writeFile optInputFile r
3546 Check -> do
47+ notForStdin
3648 r' <- TIO.readFile optInputFile
37− when (r /= r') . exitWith $
38− ExitFailure 100 -- 100 is different to all the other exit code that
39− -- are emitted either from an 'OrmoluException' or
40− -- from 'error' and 'notImplemented'.
49+ when (r /= r') $
50+ -- 100 is different to all the other exit code that are emitted
51+ -- either from an 'OrmoluException' or from 'error' and
52+ -- 'notImplemented'.
53+ exitWith (ExitFailure 100)
4154
4255----------------------------------------------------------------------------
4356-- Command line options parsing.
@@ -50,7 +63,7 @@ data Opts = Opts
5063 , optConfig :: !Config
5164 -- ^ Ormolu 'Config'
5265 , optInputFile :: !FilePath
53− -- ^ Input source file
66+ -- ^ Input source file or stdin ("-")
5467 }
5568
5669-- | Mode of operation.
@@ -103,7 +116,8 @@ optsParser = Opts
103116 <*> configParser
104117 <*> (strArgument . mconcat)
105118 [ metavar "FILE"
106− , help "Haskell source file to format"
119+ , value "-"
120+ , help "Haskell source file to format or stdin (default)"
107121 ]
108122
109123configParser :: Parser Config
src/Ormolu.hs+14
@@ -5,6 +5,7 @@
55module Ormolu
66 ( ormolu
77 , ormoluFile
8+ , ormoluStdin
89 , Config (..)
910 , defaultConfig
1011 , DynOption (..)
@@ -28,6 +29,7 @@ import Ormolu.Utils (showOutputable)
2829import qualified CmdLineParser as GHC
2930import qualified Data.Text as T
3031import qualified GHC
32+import System.IO (hGetContents, stdin)
3133
3234-- | Format a 'String', return formatted version as 'Text'.
3335--
@@ -77,6 +79,18 @@ ormoluFile
7779ormoluFile cfg path =
7880 liftIO (readFile path) >>= ormolu cfg path
7981
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+
8094----------------------------------------------------------------------------
8195-- Helpers
8296