silently 0.0.3 → 1.1.1
raw patch · 5 files changed
+183/−81 lines, 5 filesdep ~base
Dependency ranges changed: base
Files
- silently.cabal +20/−30
- src/System/IO/Silently.hs +0/−51
- src/other/System/IO/Silently.hs +55/−0
- src/unix/System/IO/Silently.hs +53/−0
- src/windows/System/IO/Silently.hs +55/−0
silently.cabal view
@@ -1,12 +1,11 @@ name: silently-version: 0.0.3-cabal-version: -any+version: 1.1.1+cabal-version: >= 1.2 build-type: Simple license: BSD3 license-file: LICENSE copyright: (c) Trystan Spangler 2011 maintainer: trystan.s@comcast.net-build-depends: base >=4 && <=5, directory -any, ghc -any stability: homepage: https://github.com/trystan/silently package-url: https://github.com/trystan/silently@@ -16,31 +15,22 @@ category: author: Trystan Spangler tested-with: GHC ==7.0-data-files:-data-dir: "" extra-source-files:-extra-tmp-files:-exposed-modules: System.IO.Silently-exposed: True-buildable: True-build-tools:-cpp-options:-cc-options:-ld-options:-pkgconfig-depends:-frameworks:-c-sources:-extensions:-extra-libraries:-extra-lib-dirs:-includes:-install-includes:-include-dirs:-hs-source-dirs: src-other-modules:-ghc-prof-options:-ghc-shared-options:-ghc-options: -Wall-hugs-options:-nhc98-options:-jhc-options:+ src/windows/System/IO/Silently.hs+ src/unix/System/IO/Silently.hs+ src/other/System/IO/Silently.hs++Library+ build-depends: base >=4 && <=5, directory -any, ghc -any+ exposed-modules: System.IO.Silently+ exposed: True+ buildable: True++ if os(windows)+ hs-source-dirs: src/windows+ else+ if os(linux) || os(osx) || os(freebsd) || os(openbsd) || os(netbsd)+ hs-source-dirs: src/unix+ else+ hs-source-dirs: src/other+
− src/System/IO/Silently.hs
@@ -1,51 +0,0 @@---- | 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 (- silence, hSilence,- capture, hCapture-) where--import GHC.IO.Handle (hDuplicate, hDuplicateTo)-import System.IO (Handle, stdout, hClose, openTempFile)-import Control.Exception (bracket)-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.-silence :: IO a -> IO a-silence = hSilence [stdout]---- | 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.-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 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]---- | 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 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 mapM_ (hDuplicateTo tmpHandle) handles- a <- action- hClose tmpHandle- str <- readFile tmpFile- return (str, a))
+ src/other/System/IO/Silently.hs view
@@ -0,0 +1,55 @@++-- | 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 (+ silence, hSilence,+ capture, hCapture+) where++import GHC.IO.Handle (hDuplicate, hDuplicateTo)+import System.IO (Handle, stdout, hClose, openTempFile)+import Control.Exception (bracket)+import System.Directory (removeFile,getTemporaryDirectory)++-- | 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.+silence :: IO a -> IO a+silence = hSilence [stdout]++-- | 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.+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 mapM_ (hDuplicateTo tmpHandle) handles+ action)+++getTempOrCurrentDirectory :: IO String+getTempOrCurrentDirectory = getTemporaryDirectory `Prelude.catch` (\ex -> return ".")++-- | 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 temp directory or current directory if there is no temp directory.+capture :: IO a -> IO (String, a)+capture = hCapture [stdout]++-- | 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 temp directory or current directory if there is no temp directory.+hCapture :: [Handle] -> IO a -> IO (String, a)+hCapture handles action = do+ oldHandles <- mapM hDuplicate handles+ tmpDir <- getTempOrCurrentDirectory+ bracket (openTempFile tmpDir "capture")+ (\(tmpFile, tmpHandle) -> do sequence_ $ zipWith hDuplicateTo oldHandles handles+ hClose tmpHandle+ removeFile tmpFile)+ (\(tmpFile, tmpHandle) -> do mapM_ (hDuplicateTo tmpHandle) handles+ a <- action+ hClose tmpHandle+ str <- readFile tmpFile+ return (str, a))
+ src/unix/System/IO/Silently.hs view
@@ -0,0 +1,53 @@++-- | 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 (+ silence, hSilence,+ capture, hCapture+) where++import GHC.IO.Handle (hDuplicate, hDuplicateTo)+import System.IO (Handle, stdout, hClose, openTempFile, openFile, IOMode(..))+import Control.Exception (bracket)+import System.Directory (removeFile,getTemporaryDirectory)++-- | Run an IO action while preventing all output to stdout.+silence :: IO a -> IO a+silence = hSilence [stdout]++-- | Run an IO action while preventing all output to the given handles.+hSilence :: [Handle] -> IO a -> IO a+hSilence handles action = do+ oldHandles <- mapM hDuplicate handles+ bracket (openFile "/dev/null" AppendMode)+ (\ tmpHandle -> do sequence_ $ zipWith hDuplicateTo oldHandles handles+ hClose tmpHandle)+ (\ tmpHandle -> do mapM_ (hDuplicateTo tmpHandle) handles+ action)++++getTempOrCurrentDirectory :: IO String+getTempOrCurrentDirectory = getTemporaryDirectory `Prelude.catch` (\ex -> return ".")++-- | 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 temp directory or current directory if there is no temp directory.+capture :: IO a -> IO (String, a)+capture = hCapture [stdout]++-- | 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 temp directory or current directory if there is no temp directory.+hCapture :: [Handle] -> IO a -> IO (String, a)+hCapture handles action = do+ oldHandles <- mapM hDuplicate handles+ tmpDir <- getTempOrCurrentDirectory+ bracket (openTempFile tmpDir "capture")+ (\(tmpFile, tmpHandle) -> do sequence_ $ zipWith hDuplicateTo oldHandles handles+ hClose tmpHandle+ removeFile tmpFile)+ (\(tmpFile, tmpHandle) -> do mapM_ (hDuplicateTo tmpHandle) handles+ a <- action+ hClose tmpHandle+ str <- readFile tmpFile+ return (str, a))
+ src/windows/System/IO/Silently.hs view
@@ -0,0 +1,55 @@++-- | 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 (+ silence, hSilence,+ capture, hCapture+) where++import GHC.IO.Handle (hDuplicate, hDuplicateTo)+import System.IO (Handle, stdout, hClose, openTempFile, openFile, IOMode(..))+import Control.Exception (bracket)+import System.Directory (removeFile,getTemporaryDirectory)++-- | Run an IO action while preventing all output to stdout.+silence :: IO a -> IO a+silence = hSilence [stdout]++-- | Run an IO action while preventing all output to the given handles.+hSilence :: [Handle] -> IO a -> IO a+hSilence handles action = do+ oldHandles <- mapM hDuplicate handles+ bracket (openFile "NUL" AppendMode)+ (\ tmpHandle -> do sequence_ $ zipWith hDuplicateTo oldHandles handles+ hClose tmpHandle)+ (\ tmpHandle -> do mapM_ (hDuplicateTo tmpHandle) handles+ action)+++getTempOrCurrentDirectory :: IO String+getTempOrCurrentDirectory = getTemporaryDirectory `Prelude.catch` (\ex -> return ".")++-- | 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 temp directory or current directory if there is no temp directory.+capture :: IO a -> IO (String, a)+capture = hCapture [stdout]++-- | 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 temp directory or current directory if there is no temp directory.+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 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