X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=Pipes%2FText%2FIO.hs;h=51c69265a04ac49a9aaa047cf7b1952242efdc40;hb=bdc47ebc7bd24c7b123867072c825d42d26ca536;hp=4a092b1e1aea6516c61b7a66d354b20909f11742;hpb=b091cbebda25acfac35c19748c607d26c01b68ec;p=github%2Ffretlink%2Ftext-pipes.git diff --git a/Pipes/Text/IO.hs b/Pipes/Text/IO.hs index 4a092b1..51c6926 100644 --- a/Pipes/Text/IO.hs +++ b/Pipes/Text/IO.hs @@ -11,8 +11,10 @@ module Pipes.Text.IO -- * Producers fromHandle + , fromHandleLn , stdin , readFile + , readFileLn -- * Consumers , toHandle , stdout @@ -28,8 +30,7 @@ import qualified Data.Text as T import qualified Data.Text.IO as T import Pipes import qualified Pipes.Safe.Prelude as Safe -import qualified Pipes.Safe as Safe -import Pipes.Safe (MonadSafe(..), Base(..)) +import Pipes.Safe (MonadSafe(..)) import Prelude hiding (readFile, writeFile) {- $textio @@ -120,6 +121,19 @@ fromHandle h = go where go {-# INLINABLE fromHandle#-} + +fromHandleLn :: MonadIO m => IO.Handle -> Producer Text m () +fromHandleLn h = go where + getLine :: IO (Either G.IOException Text) + getLine = try (T.hGetLine h) + + go = do txt <- liftIO getLine + case txt of + Left e -> return () + Right y -> do yield y + go +{-# INLINABLE fromHandleLn #-} + -- | Stream text from 'stdin' stdin :: MonadIO m => Producer Text m () stdin = fromHandle IO.stdin @@ -137,6 +151,13 @@ readFile file = Safe.withFile file IO.ReadMode fromHandle {-# INLINE readFile #-} +{-| Stream lines of text from a file +-} +readFileLn :: MonadSafe m => FilePath -> Producer Text m () +readFileLn file = Safe.withFile file IO.ReadMode fromHandleLn +{-# INLINE readFileLn #-} + + {-| Stream text to 'stdout' Unlike 'toHandle', 'stdout' gracefully terminates on a broken output pipe.