Skip to content

Commit 59c82b7

Browse files
committed
Send file-watch text to stderr #1635
1 parent 7a20ce7 commit 59c82b7

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

src/Stack/FileWatch.hs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,42 @@ import Path
2525
import System.Console.ANSI
2626
import System.Exit
2727
import System.FSNotify
28-
import System.IO (stdout, stderr)
28+
import System.IO (Handle, stdout, stderr, hPutStrLn)
2929

3030
-- | Print an exception to stderr
3131
printExceptionStderr :: Exception e => e -> IO ()
3232
printExceptionStderr 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 ()
3738
fileWatch = 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 ()
4143
fileWatchPoll = 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.
4749
fileWatchConf :: 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."

src/main/Main.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -882,8 +882,8 @@ buildCmd opts go = do
882882
hPutStrLn stderr "See: https://github.com/commercialhaskell/stack/issues/1015"
883883
error "-prof GHC option submitted"
884884
case boptsFileWatch opts of
885-
FileWatchPoll -> fileWatchPoll inner
886-
FileWatch -> fileWatch inner
885+
FileWatchPoll -> fileWatchPoll stderr inner
886+
FileWatch -> fileWatch stderr inner
887887
NoFileWatch -> inner $ const $ return ()
888888
where
889889
inner setLocalFiles = withBuildConfigAndLock go $ \lk ->

0 commit comments

Comments
 (0)