]> git.immae.eu Git - github/fretlink/text-pipes.git/commitdiff
Add readFileLines function
authorSidharth Kapur <sidharthkapur1@gmail.com>
Sat, 6 Feb 2016 05:39:37 +0000 (23:39 -0600)
committerSidharth Kapur <sidharthkapur1@gmail.com>
Sat, 6 Feb 2016 05:39:37 +0000 (23:39 -0600)
Pipes/Text/IO.hs

index b7ef5cdcaea2d06e3ab92b10e5603a65cb944581..bacb2073ed70def68e6a67ff9a25ffb4e7967693 100644 (file)
@@ -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.