1 {-#LANGUAGE RankNTypes#-}
24 import qualified System.IO as IO
25 import Control.Exception (throwIO, try)
26 import Foreign.C.Error (Errno(Errno), ePIPE)
27 import qualified GHC.IO.Exception as G
28 import Data.Text (Text)
29 import qualified Data.Text as T
30 import qualified Data.Text.IO as T
32 import qualified Pipes.Safe.Prelude as Safe
33 import Pipes.Safe (MonadSafe(..))
34 import Prelude hiding (readFile, writeFile)
37 Where pipes @IO@ replaces lazy @IO@, @Producer Text IO r@ replaces lazy 'Text'.
38 This module exports some convenient functions for producing and consuming
39 pipes 'Text' in @IO@, namely, 'readFile', 'writeFile', 'fromHandle', 'toHandle',
40 'stdin' and 'stdout'. Some caveats described below.
42 The main points are as in
43 <https://hackage.haskell.org/package/pipes-bytestring-1.0.0/docs/Pipes-ByteString.html Pipes.ByteString>:
45 A 'Handle' can be associated with a 'Producer' or 'Consumer' according
46 as it is read or written to.
49 > import qualified Pipes.Text as Text
50 > import qualified Pipes.Text.IO as Text
54 > withFile "inFile.txt" ReadMode $ \hIn ->
55 > withFile "outFile.txt" WriteMode $ \hOut ->
56 > runEffect $ Text.fromHandle hIn >-> Text.toHandle hOut
58 To stream from files, the following is perhaps more Prelude-like (note that it uses Pipes.Safe):
61 > import qualified Pipes.Text as Text
62 > import qualified Pipes.Text.IO as Text
65 > main = runSafeT $ runEffect $ Text.readFile "inFile.txt" >-> Text.writeFile "outFile.txt"
67 Finally, you can stream to and from 'stdin' and 'stdout' using the predefined 'stdin'
68 and 'stdout' pipes, as with the following \"echo\" program:
70 > main = runEffect $ Text.stdin >-> Text.stdout
78 The operations exported here are a convenience, like the similar operations in
79 @Data.Text.IO@ (or rather, @Data.Text.Lazy.IO@, since, again, @Producer Text m r@ is
80 'effectful text' and something like the pipes equivalent of lazy Text.)
82 * Like the functions in @Data.Text.IO@, they attempt to work with the system encoding.
84 * Like the functions in @Data.Text.IO@, they significantly slower than ByteString operations. Where
85 you know what encoding you are working with, use @Pipes.ByteString@ and @Pipes.Text.Encoding@ instead,
86 e.g. @view utf8 Bytes.stdin@ instead of @Text.stdin@
88 * Like the functions in @Data.Text.IO@ , they use Text exceptions, not the standard Pipes protocols.
92 > view utf8 . Bytes.fromHandle :: Handle -> Producer Text IO (Producer ByteString m ())
94 yields a stream of Text, and follows
95 standard pipes protocols by reverting to (i.e. returning) the underlying byte stream
96 upon reaching any decoding error. (See especially the pipes-binary package.)
98 By contrast, something like
100 > Text.fromHandle :: Handle -> Producer Text IO ()
102 supplies a stream of text returning '()', which is convenient for many tasks,
103 but violates the pipes @pipes-binary@ approach to decoding errors and
104 throws an exception of the kind characteristic of the @text@ library instead.
109 {-| Convert a 'IO.Handle' into a text stream using a text size
110 determined by the good sense of the text library. Note with the remarks
111 at the head of this module that this
112 is slower than @view utf8 (Pipes.ByteString.fromHandle h)@
113 but uses the system encoding and has other nice @Data.Text.IO@ features
116 fromHandle :: MonadIO m => IO.Handle -> Producer Text m ()
117 fromHandle h = go where
118 go = do txt <- liftIO (T.hGetChunk h)
119 if T.null txt then return ()
122 {-# INLINABLE fromHandle#-}
125 fromHandleLn :: MonadIO m => IO.Handle -> Producer Text m ()
126 fromHandleLn h = go where
127 getLine :: IO (Either G.IOException Text)
128 getLine = try (T.hGetLine h)
130 go = do txt <- liftIO getLine
133 Right y -> do yield y
135 {-# INLINABLE fromHandleLn #-}
137 -- | Stream text from 'stdin'
138 stdin :: MonadIO m => Producer Text m ()
139 stdin = fromHandle IO.stdin
143 {-| Stream text from a file in the simple fashion of @Data.Text.IO@
145 >>> runSafeT $ runEffect $ Text.readFile "hello.hs" >-> Text.map toUpper >-> hoist lift Text.stdout
146 MAIN = PUTSTRLN "HELLO WORLD"
149 readFile :: MonadSafe m => FilePath -> Producer Text m ()
150 readFile file = Safe.withFile file IO.ReadMode fromHandle
151 {-# INLINE readFile #-}
154 {-| Stream lines of text from a file
156 readFileLn :: MonadSafe m => FilePath -> Producer Text m ()
157 readFileLn file = Safe.withFile file IO.ReadMode fromHandleLn
158 {-# INLINE readFileLn #-}
161 {-| Stream text to 'stdout'
163 Unlike 'toHandle', 'stdout' gracefully terminates on a broken output pipe.
165 Note: For best performance, it might be best just to use @(for source (liftIO . putStr))@
166 instead of @(source >-> stdout)@ .
168 stdout :: MonadIO m => Consumer' Text m ()
173 x <- liftIO $ try (T.putStr txt)
175 Left (G.IOError { G.ioe_type = G.ResourceVanished
176 , G.ioe_errno = Just ioe })
179 Left e -> liftIO (throwIO e)
181 {-# INLINABLE stdout #-}
184 {-| Convert a text stream into a 'Handle'
186 Note: again, for best performance, where possible use
187 @(for source (liftIO . hPutStr handle))@ instead of @(source >-> toHandle handle)@.
189 toHandle :: MonadIO m => IO.Handle -> Consumer' Text m r
190 toHandle h = for cat (liftIO . T.hPutStr h)
191 {-# INLINABLE toHandle #-}
195 -- | Stream text into a file. Uses @pipes-safe@.
196 writeFile :: (MonadSafe m) => FilePath -> Consumer' Text m ()
197 writeFile file = Safe.withFile file IO.WriteMode toHandle
198 {-# INLINE writeFile #-}