diff options
author | Michael Thompson <what_is_it_to_do_anything@yahoo.com> | 2016-02-06 12:26:53 -0500 |
---|---|---|
committer | Michael Thompson <what_is_it_to_do_anything@yahoo.com> | 2016-02-06 12:26:53 -0500 |
commit | bdc47ebc7bd24c7b123867072c825d42d26ca536 (patch) | |
tree | 8c33b07541e7385da2618b9c69c3e58a2e58afc5 /Pipes | |
parent | 69fd54e3eab28c285aec3cb554fa9b801e7c024b (diff) | |
parent | 3412dff4538e0335d5703751efac5017ea02cabf (diff) | |
download | text-pipes-bdc47ebc7bd24c7b123867072c825d42d26ca536.tar.gz text-pipes-bdc47ebc7bd24c7b123867072c825d42d26ca536.tar.zst text-pipes-bdc47ebc7bd24c7b123867072c825d42d26ca536.zip |
Merge pull request #18 from sid-kap/text_lines
Add readFileLines function
Diffstat (limited to 'Pipes')
-rw-r--r-- | Pipes/Text/IO.hs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/Pipes/Text/IO.hs b/Pipes/Text/IO.hs index b7ef5cd..51c6926 100644 --- a/Pipes/Text/IO.hs +++ b/Pipes/Text/IO.hs | |||
@@ -11,8 +11,10 @@ module Pipes.Text.IO | |||
11 | 11 | ||
12 | -- * Producers | 12 | -- * Producers |
13 | fromHandle | 13 | fromHandle |
14 | , fromHandleLn | ||
14 | , stdin | 15 | , stdin |
15 | , readFile | 16 | , readFile |
17 | , readFileLn | ||
16 | -- * Consumers | 18 | -- * Consumers |
17 | , toHandle | 19 | , toHandle |
18 | , stdout | 20 | , stdout |
@@ -119,6 +121,19 @@ fromHandle h = go where | |||
119 | go | 121 | go |
120 | {-# INLINABLE fromHandle#-} | 122 | {-# INLINABLE fromHandle#-} |
121 | 123 | ||
124 | |||
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) | ||
129 | |||
130 | go = do txt <- liftIO getLine | ||
131 | case txt of | ||
132 | Left e -> return () | ||
133 | Right y -> do yield y | ||
134 | go | ||
135 | {-# INLINABLE fromHandleLn #-} | ||
136 | |||
122 | -- | Stream text from 'stdin' | 137 | -- | Stream text from 'stdin' |
123 | stdin :: MonadIO m => Producer Text m () | 138 | stdin :: MonadIO m => Producer Text m () |
124 | stdin = fromHandle IO.stdin | 139 | stdin = fromHandle IO.stdin |
@@ -136,6 +151,13 @@ readFile file = Safe.withFile file IO.ReadMode fromHandle | |||
136 | {-# INLINE readFile #-} | 151 | {-# INLINE readFile #-} |
137 | 152 | ||
138 | 153 | ||
154 | {-| Stream lines of text from a file | ||
155 | -} | ||
156 | readFileLn :: MonadSafe m => FilePath -> Producer Text m () | ||
157 | readFileLn file = Safe.withFile file IO.ReadMode fromHandleLn | ||
158 | {-# INLINE readFileLn #-} | ||
159 | |||
160 | |||
139 | {-| Stream text to 'stdout' | 161 | {-| Stream text to 'stdout' |
140 | 162 | ||
141 | Unlike 'toHandle', 'stdout' gracefully terminates on a broken output pipe. | 163 | Unlike 'toHandle', 'stdout' gracefully terminates on a broken output pipe. |