@@ -25,29 +25,42 @@ import Path
2525import System.Console.ANSI
2626import System.Exit
2727import System.FSNotify
28- import System.IO (stdout , stderr )
28+ import System.IO (Handle , stdout , stderr , hPutStrLn )
2929
3030-- | Print an exception to stderr
3131printExceptionStderr :: Exception e => e -> IO ()
3232printExceptionStderr e =
3333 L. hPut stderr $ toLazyByteString $ fromShow e <> copyByteString " \n "
3434
35- fileWatch :: ((Set (Path Abs File ) -> IO () ) -> IO () )
35+ fileWatch :: Handle
36+ -> ((Set (Path Abs File ) -> IO () ) -> IO () )
3637 -> IO ()
3738fileWatch = fileWatchConf defaultConfig
3839
39- fileWatchPoll :: ((Set (Path Abs File ) -> IO () ) -> IO () )
40- -> IO ()
40+ fileWatchPoll :: Handle
41+ -> ((Set (Path Abs File ) -> IO () ) -> IO () )
42+ -> IO ()
4143fileWatchPoll = fileWatchConf $ defaultConfig { confUsePolling = True }
4244
4345-- | Run an action, watching for file changes
4446--
4547-- The action provided takes a callback that is used to set the files to be
4648-- watched. When any of those files are changed, we rerun the action again.
4749fileWatchConf :: WatchConfig
50+ -> Handle
4851 -> ((Set (Path Abs File ) -> IO () ) -> IO () )
4952 -> IO ()
50- fileWatchConf cfg inner = withManagerConf cfg $ \ manager -> do
53+ fileWatchConf cfg out inner = withManagerConf cfg $ \ manager -> do
54+ let putLn = hPutStrLn out
55+ let withColor color action = do
56+ outputIsTerminal <- hIsTerminalDevice stdout
57+ if outputIsTerminal
58+ then do
59+ setSGR [SetColor Foreground Dull color]
60+ action
61+ setSGR [Reset ]
62+ else action
63+
5164 allFiles <- newTVarIO Set. empty
5265 dirtyVar <- newTVarIO True
5366 watchVar <- newTVarIO Map. empty
@@ -87,22 +100,23 @@ fileWatchConf cfg inner = withManagerConf cfg $ \manager -> do
87100 listen <- watchDir manager dir' (const True ) onChange
88101 return $ Just listen
89102
103+
90104 let watchInput = do
91105 line <- getLine
92106 unless (line == " quit" ) $ do
93107 case line of
94108 " help" -> do
95- putStrLn " "
96- putStrLn " help: display this help"
97- putStrLn " quit: exit"
98- putStrLn " build: force a rebuild"
99- putStrLn " watched: display watched files"
109+ putLn " "
110+ putLn " help: display this help"
111+ putLn " quit: exit"
112+ putLn " build: force a rebuild"
113+ putLn " watched: display watched files"
100114 " build" -> atomically $ writeTVar dirtyVar True
101115 " watched" -> do
102116 watch <- readTVarIO allFiles
103- mapM_ putStrLn (Set. toList watch)
117+ mapM_ putLn (Set. toList watch)
104118 " " -> atomically $ writeTVar dirtyVar True
105- _ -> putStrLn $ concat
119+ _ -> putLn $ concat
106120 [ " Unknown command: "
107121 , show line
108122 , " . Try 'help'"
@@ -125,22 +139,13 @@ fileWatchConf cfg inner = withManagerConf cfg $ \manager -> do
125139 -- https://github.com/commercialhaskell/stack/issues/822
126140 atomically $ writeTVar dirtyVar False
127141
128- let withColor color action = do
129- outputIsTerminal <- hIsTerminalDevice stdout
130- if outputIsTerminal
131- then do
132- setSGR [SetColor Foreground Dull color]
133- action
134- setSGR [Reset ]
135- else action
136-
137142 case eres of
138143 Left e -> do
139144 let color = case fromException e of
140145 Just ExitSuccess -> Green
141146 _ -> Red
142147 withColor color $ printExceptionStderr e
143148 _ -> withColor Green $
144- putStrLn " Success! Waiting for next file change."
149+ putLn " Success! Waiting for next file change."
145150
146- putStrLn " Type help for available commands. Press enter to force a rebuild."
151+ putLn " Type help for available commands. Press enter to force a rebuild."
0 commit comments