Skip to content

Commit

Permalink
made it work on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
trystan committed Apr 11, 2011
1 parent ad91745 commit 025153f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
34 changes: 16 additions & 18 deletions README
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
Silently is a package that allows you to run an IO action and prevent it from writing to stdout, or any other handle, by using "silence". Or you can capture the output for yourself using "capture".

For example, in GHCI
For example; the program

Prelude> :m + System.IO.Silently
Prelude System.IO.Silently> :m + System.IO
Prelude System.IO.Silently System.IO>
Prelude System.IO.Silently System.IO> print 1 >> silence (print 2 >> return "kittens!") >> print 3
1
3
Prelude System.IO.Silently System.IO> print 1 >> capture (hPutStrLn stdout "2" >> hPutStrLn stderr "3" >> return "puppies!")
1
3
("2\n","puppies!")
Prelude System.IO.Silently System.IO> print 1 >> hSilence [stderr] (hPutStrLn stdout "2" >> hPutStrLn stderr "3" >> return "kittens!") >> print 4
1
2
4
Prelude System.IO.Silently System.IO> print 1 >> hCapture [stdout,stderr] (hPutStrLn stdout "2" >> hPutStrLn stderr "3" >> return "puppies!")
1
("2\n3\n","puppies!")
import System.IO.Silently

main = do
putStr "putStrLn: " >> putStrLn "puppies!"
putStr "silenced: " >> silence (putStrLn "kittens!")
putStrLn ""
(captured, result) <- capture (putStr "wookies!" >> return 123)
putStr "captured: " >> putStrLn captured
putStr "returned: " >> putStrLn (show result)

will print

putStrLn: puppies!
silenced:
captured: wookies!
returned: 123
7 changes: 5 additions & 2 deletions src/windows/System/IO/Silently.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ capture = hCapture [stdout]
hCapture :: [Handle] -> IO a -> IO (String, a)
hCapture handles action = do
oldHandles <- mapM hDuplicate handles
tmpDir <- getTempOrCurrentDirectory
bracket (openTempFile tmpDir "capture")
bracket (openTempFile "." "capture")
(\(tmpFile, tmpHandle) -> do sequence_ $ zipWith hDuplicateTo oldHandles handles
hClose tmpHandle
removeFile tmpFile)
(\(tmpFile, tmpHandle) -> do mapM_ (hDuplicateTo tmpHandle) handles
a <- action
hClose tmpHandle
sequence_ $ zipWith hDuplicateTo oldHandles handles
str <- readFile tmpFile
forceList str
return (str, a))
forceList [] = return ()
forceList (x:xs) = forceList xs

0 comments on commit 025153f

Please sign in to comment.