silently 0.0.2 → 0.0.3
raw patch · 2 files changed
+28/−25 lines, 2 files
Files
- silently.cabal +3/−3
- src/System/IO/Silently.hs +25/−22
silently.cabal view
@@ -1,5 +1,5 @@ name: silently-version: 0.0.2+version: 0.0.3 cabal-version: -any build-type: Simple license: BSD3@@ -11,8 +11,8 @@ homepage: https://github.com/trystan/silently package-url: https://github.com/trystan/silently bug-reports: https://github.com/trystan/silently/issues-synopsis: Prevent or capture writing to stdout.-description: Prevent or capture writing to stdout, or any given handle.+synopsis: Prevent or capture writing to stdout and other handles.+description: Prevent or capture writing to stdout and other handles. category: author: Trystan Spangler tested-with: GHC ==7.0
src/System/IO/Silently.hs view
@@ -1,47 +1,50 @@ --- | Need to prevent output to the terminal or stderr? Need to capture it and use it for your own means? Now you can, with 'silently' and 'capture'.+-- | Need to prevent output to the terminal, a file, or stderr? Need to capture it and use it for+-- your own means? Now you can, with 'silence' and 'capture'. module System.IO.Silently (- silently, hSilently, capture, hCapture+ silence, hSilence,+ capture, hCapture ) where -import GHC.IO.Handle-import System.IO+import GHC.IO.Handle (hDuplicate, hDuplicateTo)+import System.IO (Handle, stdout, hClose, openTempFile) import Control.Exception (bracket)-import System.Directory+import System.Directory (removeFile) -- | Run an IO action while preventing all output to stdout. -- This will, as a side effect, create and delete a temp file in the current directory.-silently :: IO a -> IO a-silently = hSilently stdout+silence :: IO a -> IO a+silence = hSilence [stdout] --- | Run an IO action while preventing all output to the given handle.+-- | Run an IO action while preventing all output to the given handles. -- This will, as a side effect, create and delete a temp file in the current directory.-hSilently :: Handle -> IO a -> IO a-hSilently handle action = do- oldHandle <- hDuplicate handle- bracket (openTempFile "." "silently")- (\(tmpFile, tmpHandle) -> do hDuplicateTo oldHandle handle+hSilence :: [Handle] -> IO a -> IO a+hSilence handles action = do+ oldHandles <- mapM hDuplicate handles+ bracket (openTempFile "." "silence")+ (\(tmpFile, tmpHandle) -> do sequence_ $ zipWith hDuplicateTo oldHandles handles hClose tmpHandle removeFile tmpFile)- (\(_, tmpHandle) -> do hDuplicateTo tmpHandle handle+ (\(_, tmpHandle) -> do mapM_ (hDuplicateTo tmpHandle) handles action) + -- | Run an IO action while preventing and capturing all output to stdout. -- This will, as a side effect, create and delete a temp file in the current directory. capture :: IO a -> IO (String, a)-capture = hCapture stdout+capture = hCapture [stdout] --- | Run an IO action while preventing and capturing all output to the given handle.+-- | Run an IO action while preventing and capturing all output to the given handles. -- This will, as a side effect, create and delete a temp file in the current directory.-hCapture :: Handle -> IO a -> IO (String, a)-hCapture handle action = do- oldHandle <- hDuplicate handle- bracket (openTempFile "." "silently")- (\(tmpFile, tmpHandle) -> do hDuplicateTo oldHandle handle+hCapture :: [Handle] -> IO a -> IO (String, a)+hCapture handles action = do+ oldHandles <- mapM hDuplicate handles+ bracket (openTempFile "." "capture")+ (\(tmpFile, tmpHandle) -> do sequence_ $ zipWith hDuplicateTo oldHandles handles hClose tmpHandle removeFile tmpFile)- (\(tmpFile, tmpHandle) -> do hDuplicateTo tmpHandle handle+ (\(tmpFile, tmpHandle) -> do mapM_ (hDuplicateTo tmpHandle) handles a <- action hClose tmpHandle str <- readFile tmpFile