{-# LANGUAGE RankNTypes #-}
module Pipes.Prelude.Text
(
fromHandleLn,
toHandleLn,
stdinLn,
stdoutLn,
stdoutLn',
readFileLn,
writeFileLn,
)
where
import Control.Exception (throwIO, try)
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Foreign.C.Error (Errno (Errno), ePIPE)
import qualified GHC.IO.Exception as G
import Pipes
import Pipes.Safe (MonadSafe (..))
import qualified Pipes.Safe.Prelude as Safe
import qualified System.IO as IO
import Prelude hiding (readFile, writeFile)
stdinLn :: MonadIO m => Producer' T.Text m ()
stdinLn :: Producer' Text m ()
stdinLn = Handle -> Producer' Text m ()
forall (m :: * -> *). MonadIO m => Handle -> Producer' Text m ()
fromHandleLn Handle
IO.stdin
{-# INLINEABLE stdinLn #-}
stdoutLn :: MonadIO m => Consumer' T.Text m ()
stdoutLn :: Consumer' Text m ()
stdoutLn = Proxy () Text y' y m ()
Consumer' Text m ()
go
where
go :: Proxy () Text y' y m ()
go = do
Text
str <- Proxy () Text y' y m Text
forall (m :: * -> *) a. Functor m => Consumer' a m a
await
Either IOException ()
x <- IO (Either IOException ())
-> Proxy () Text y' y m (Either IOException ())
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either IOException ())
-> Proxy () Text y' y m (Either IOException ()))
-> IO (Either IOException ())
-> Proxy () Text y' y m (Either IOException ())
forall a b. (a -> b) -> a -> b
$ IO () -> IO (Either IOException ())
forall e a. Exception e => IO a -> IO (Either e a)
try (Text -> IO ()
T.putStrLn Text
str)
case Either IOException ()
x of
Left
G.IOError
{ ioe_type :: IOException -> IOErrorType
G.ioe_type = IOErrorType
G.ResourceVanished,
ioe_errno :: IOException -> Maybe CInt
G.ioe_errno = Just CInt
ioe
}
| CInt -> Errno
Errno CInt
ioe Errno -> Errno -> Bool
forall a. Eq a => a -> a -> Bool
== Errno
ePIPE ->
() -> Proxy () Text y' y m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Left IOException
e -> IO () -> Proxy () Text y' y m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IOException -> IO ()
forall e a. Exception e => e -> IO a
throwIO IOException
e)
Right () -> Proxy () Text y' y m ()
go
{-# INLINEABLE stdoutLn #-}
stdoutLn' :: MonadIO m => Consumer' T.Text m r
stdoutLn' :: Consumer' Text m r
stdoutLn' = Proxy () Text () Text m r
-> (Text -> Proxy () Text y' y m ()) -> Proxy () Text y' y m r
forall (m :: * -> *) x' x b' b a' c' c.
Functor m =>
Proxy x' x b' b m a'
-> (b -> Proxy x' x c' c m b') -> Proxy x' x c' c m a'
for Proxy () Text () Text m r
forall (m :: * -> *) a r. Functor m => Pipe a a m r
cat (IO () -> Proxy () Text y' y m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> Proxy () Text y' y m ())
-> (Text -> IO ()) -> Text -> Proxy () Text y' y m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> IO ()
T.putStrLn)
{-# INLINE [1] stdoutLn' #-}
{-# RULES
"p >-> stdoutLn'" forall p.
p >-> stdoutLn' =
for p (liftIO . T.putStrLn)
#-}
fromHandleLn :: MonadIO m => IO.Handle -> Producer' Text m ()
fromHandleLn :: Handle -> Producer' Text m ()
fromHandleLn Handle
h = Proxy x' x () Text m ()
Producer' Text m ()
go
where
getLine :: IO (Either G.IOException Text)
getLine :: IO (Either IOException Text)
getLine = IO Text -> IO (Either IOException Text)
forall e a. Exception e => IO a -> IO (Either e a)
try (Handle -> IO Text
T.hGetLine Handle
h)
go :: Proxy x' x () Text m ()
go = do
Either IOException Text
txt <- IO (Either IOException Text)
-> Proxy x' x () Text m (Either IOException Text)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO (Either IOException Text)
getLine
case Either IOException Text
txt of
Left IOException
_ -> () -> Proxy x' x () Text m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Right Text
y -> do
Text -> Proxy x' x () Text m ()
forall (m :: * -> *) a x' x. Functor m => a -> Proxy x' x () a m ()
yield Text
y
Proxy x' x () Text m ()
go
{-# INLINEABLE fromHandleLn #-}
toHandleLn :: MonadIO m => IO.Handle -> Consumer' T.Text m r
toHandleLn :: Handle -> Consumer' Text m r
toHandleLn Handle
handle = Proxy () Text () Text m r
-> (Text -> Proxy () Text y' y m ()) -> Proxy () Text y' y m r
forall (m :: * -> *) x' x b' b a' c' c.
Functor m =>
Proxy x' x b' b m a'
-> (b -> Proxy x' x c' c m b') -> Proxy x' x c' c m a'
for Proxy () Text () Text m r
forall (m :: * -> *) a r. Functor m => Pipe a a m r
cat (IO () -> Proxy () Text y' y m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> Proxy () Text y' y m ())
-> (Text -> IO ()) -> Text -> Proxy () Text y' y m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> Text -> IO ()
T.hPutStrLn Handle
handle)
{-# INLINE [1] toHandleLn #-}
{-# RULES
"p >-> toHandleLn handle" forall p handle.
p >-> toHandleLn handle =
for p (liftIO . T.hPutStrLn handle)
#-}
readFileLn :: MonadSafe m => FilePath -> Producer Text m ()
readFileLn :: FilePath -> Producer Text m ()
readFileLn FilePath
file = FilePath
-> IOMode -> (Handle -> Producer Text m ()) -> Producer Text m ()
forall (m :: * -> *) r.
MonadSafe m =>
FilePath -> IOMode -> (Handle -> m r) -> m r
Safe.withFile FilePath
file IOMode
IO.ReadMode (\Handle
h -> Handle -> Producer' Text m ()
forall (m :: * -> *). MonadIO m => Handle -> Producer' Text m ()
fromHandleLn Handle
h)
{-# INLINE readFileLn #-}
writeFileLn :: (MonadSafe m) => FilePath -> Consumer' Text m r
writeFileLn :: FilePath -> Consumer' Text m r
writeFileLn FilePath
file = FilePath
-> IOMode
-> (Handle -> Proxy () Text y' y m r)
-> Proxy () Text y' y m r
forall (m :: * -> *) r.
MonadSafe m =>
FilePath -> IOMode -> (Handle -> m r) -> m r
Safe.withFile FilePath
file IOMode
IO.WriteMode (\Handle
h -> Handle -> Consumer' Text m r
forall (m :: * -> *) r. MonadIO m => Handle -> Consumer' Text m r
toHandleLn Handle
h)
{-# INLINEABLE writeFileLn #-}