From 3b27b572ed0bd65e0e5a2d8d1debf78a17048c52 Mon Sep 17 00:00:00 2001 From: Sidharth Kapur Date: Fri, 5 Feb 2016 23:39:37 -0600 Subject: Add readFileLines function --- Pipes/Text/IO.hs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Pipes/Text/IO.hs b/Pipes/Text/IO.hs index b7ef5cd..bacb207 100644 --- a/Pipes/Text/IO.hs +++ b/Pipes/Text/IO.hs @@ -136,6 +136,24 @@ readFile file = Safe.withFile file IO.ReadMode fromHandle {-# INLINE readFile #-} +{-| 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 #-} + + {-| Stream text to 'stdout' Unlike 'toHandle', 'stdout' gracefully terminates on a broken output pipe. -- cgit v1.2.3