X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=Pipes%2FText%2FIO.hs;h=51c69265a04ac49a9aaa047cf7b1952242efdc40;hb=3412dff4538e0335d5703751efac5017ea02cabf;hp=4e0c4a9cc0d324d4783b5fea342a712e6a980516;hpb=0396816d948fcb60d3cb7664d5e532a0c64d5216;p=github%2Ffretlink%2Ftext-pipes.git diff --git a/Pipes/Text/IO.hs b/Pipes/Text/IO.hs index 4e0c4a9..51c6926 100644 --- a/Pipes/Text/IO.hs +++ b/Pipes/Text/IO.hs @@ -11,9 +11,10 @@ module Pipes.Text.IO -- * Producers fromHandle + , fromHandleLn , stdin , readFile - , readFileLines + , readFileLn -- * Consumers , toHandle , stdout @@ -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 @@ -139,20 +153,9 @@ readFile file = Safe.withFile file IO.ReadMode fromHandle {-| Stream lines of text from a file -} -readFileLines :: MonadSafe m => FilePath -> Producer Text m () -readFileLines file = Safe.withFile file IO.ReadMode fromHandleLines - where - fromHandleLines :: MonadIO m => IO.Handle -> Producer Text m () - fromHandleLines 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 -{-# INLINE readFileLines #-} +readFileLn :: MonadSafe m => FilePath -> Producer Text m () +readFileLn file = Safe.withFile file IO.ReadMode fromHandleLn +{-# INLINE readFileLn #-} {-| Stream text to 'stdout'