module Streamly.Internal.Console.Stdio
(
read
, getBytes
, getChars
, readChunks
, getChunks
, write
, writeErr
, putBytes
, putChars
, writeChunks
, writeErrChunks
, putChunks
, putStringsWith
, putStrings
, putStringsLn
)
where
#include "inline.hs"
import Control.Monad.IO.Class (MonadIO(..))
import Data.Word (Word8)
import System.IO (stdin, stdout, stderr)
import Prelude hiding (read)
import Streamly.Internal.Data.Array.Foreign.Type (Array(..))
import Streamly.Internal.Data.Stream.Serial (SerialT)
import Streamly.Internal.Data.Unfold (Unfold)
import Streamly.Internal.Data.Fold (Fold)
import qualified Streamly.Internal.Data.Array.Foreign as Array
import qualified Streamly.Internal.Data.Stream.IsStream as Stream
import qualified Streamly.Internal.Data.Unfold as Unfold
import qualified Streamly.Internal.FileSystem.Handle as Handle
import qualified Streamly.Internal.Unicode.Stream as Unicode
{-# INLINE read #-}
read :: MonadIO m => Unfold m () Word8
read :: Unfold m () Word8
read = (() -> Handle) -> Unfold m Handle Word8 -> Unfold m () Word8
forall a c (m :: * -> *) b.
(a -> c) -> Unfold m c b -> Unfold m a b
Unfold.lmap (\() -> Handle
stdin) Unfold m Handle Word8
forall (m :: * -> *). MonadIO m => Unfold m Handle Word8
Handle.read
{-# INLINE getBytes #-}
getBytes :: MonadIO m => SerialT m Word8
getBytes :: SerialT m Word8
getBytes = Handle -> SerialT m Word8
forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, MonadIO m) =>
Handle -> t m Word8
Handle.toBytes Handle
stdin
{-# INLINE getChars #-}
getChars :: MonadIO m => SerialT m Char
getChars :: SerialT m Char
getChars = SerialT m Word8 -> SerialT m Char
forall (m :: * -> *) (t :: (* -> *) -> * -> *).
(Monad m, IsStream t) =>
t m Word8 -> t m Char
Unicode.decodeUtf8 SerialT m Word8
forall (m :: * -> *). MonadIO m => SerialT m Word8
getBytes
{-# INLINE readChunks #-}
readChunks :: MonadIO m => Unfold m () (Array Word8)
readChunks :: Unfold m () (Array Word8)
readChunks = (() -> Handle)
-> Unfold m Handle (Array Word8) -> Unfold m () (Array Word8)
forall a c (m :: * -> *) b.
(a -> c) -> Unfold m c b -> Unfold m a b
Unfold.lmap (\() -> Handle
stdin) Unfold m Handle (Array Word8)
forall (m :: * -> *). MonadIO m => Unfold m Handle (Array Word8)
Handle.readChunks
{-# INLINE getChunks #-}
getChunks :: MonadIO m => SerialT m (Array Word8)
getChunks :: SerialT m (Array Word8)
getChunks = Handle -> SerialT m (Array Word8)
forall (t :: (* -> *) -> * -> *) (m :: * -> *).
(IsStream t, MonadIO m) =>
Handle -> t m (Array Word8)
Handle.toChunks Handle
stdin
{-# INLINE write #-}
write :: MonadIO m => Fold m Word8 ()
write :: Fold m Word8 ()
write = Handle -> Fold m Word8 ()
forall (m :: * -> *). MonadIO m => Handle -> Fold m Word8 ()
Handle.write Handle
stdout
{-# INLINE writeErr #-}
writeErr :: MonadIO m => Fold m Word8 ()
writeErr :: Fold m Word8 ()
writeErr = Handle -> Fold m Word8 ()
forall (m :: * -> *). MonadIO m => Handle -> Fold m Word8 ()
Handle.write Handle
stderr
{-# INLINE putBytes #-}
putBytes :: MonadIO m => SerialT m Word8 -> m ()
putBytes :: SerialT m Word8 -> m ()
putBytes = Handle -> SerialT m Word8 -> m ()
forall (m :: * -> *).
MonadIO m =>
Handle -> SerialT m Word8 -> m ()
Handle.putBytes Handle
stdout
{-# INLINE putChars #-}
putChars :: MonadIO m => SerialT m Char -> m ()
putChars :: SerialT m Char -> m ()
putChars = SerialT m Word8 -> m ()
forall (m :: * -> *). MonadIO m => SerialT m Word8 -> m ()
putBytes (SerialT m Word8 -> m ())
-> (SerialT m Char -> SerialT m Word8) -> SerialT m Char -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SerialT m Char -> SerialT m Word8
forall (m :: * -> *) (t :: (* -> *) -> * -> *).
(Monad m, IsStream t) =>
t m Char -> t m Word8
Unicode.encodeUtf8
{-# INLINE writeChunks #-}
writeChunks :: MonadIO m => Fold m (Array Word8) ()
writeChunks :: Fold m (Array Word8) ()
writeChunks = Handle -> Fold m (Array Word8) ()
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Handle -> Fold m (Array a) ()
Handle.writeChunks Handle
stdout
{-# INLINE writeErrChunks #-}
writeErrChunks :: MonadIO m => Fold m (Array Word8) ()
writeErrChunks :: Fold m (Array Word8) ()
writeErrChunks = Handle -> Fold m (Array Word8) ()
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Handle -> Fold m (Array a) ()
Handle.writeChunks Handle
stderr
{-# INLINE putChunks #-}
putChunks :: MonadIO m => SerialT m (Array Word8) -> m ()
putChunks :: SerialT m (Array Word8) -> m ()
putChunks = Handle -> SerialT m (Array Word8) -> m ()
forall (m :: * -> *) a.
(MonadIO m, Storable a) =>
Handle -> SerialT m (Array a) -> m ()
Handle.putChunks Handle
stdout
{-# INLINE putStringsWith #-}
putStringsWith :: MonadIO m
=> (SerialT m Char -> SerialT m Word8) -> SerialT m String -> m ()
putStringsWith :: (SerialT m Char -> SerialT m Word8) -> SerialT m String -> m ()
putStringsWith SerialT m Char -> SerialT m Word8
encode = SerialT m (Array Word8) -> m ()
forall (m :: * -> *). MonadIO m => SerialT m (Array Word8) -> m ()
putChunks (SerialT m (Array Word8) -> m ())
-> (SerialT m String -> SerialT m (Array Word8))
-> SerialT m String
-> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SerialT m Char -> SerialT m Word8)
-> SerialT m String -> SerialT m (Array Word8)
forall (m :: * -> *) (t :: (* -> *) -> * -> *).
(MonadIO m, IsStream t) =>
(SerialT m Char -> SerialT m Word8)
-> t m String -> t m (Array Word8)
Unicode.encodeStrings SerialT m Char -> SerialT m Word8
encode
{-# INLINE putStrings #-}
putStrings :: MonadIO m => SerialT m String -> m ()
putStrings :: SerialT m String -> m ()
putStrings = (SerialT m Char -> SerialT m Word8) -> SerialT m String -> m ()
forall (m :: * -> *).
MonadIO m =>
(SerialT m Char -> SerialT m Word8) -> SerialT m String -> m ()
putStringsWith SerialT m Char -> SerialT m Word8
forall (m :: * -> *) (t :: (* -> *) -> * -> *).
(Monad m, IsStream t) =>
t m Char -> t m Word8
Unicode.encodeUtf8
{-# INLINE putStringsLn #-}
putStringsLn :: MonadIO m => SerialT m String -> m ()
putStringsLn :: SerialT m String -> m ()
putStringsLn =
SerialT m (Array Word8) -> m ()
forall (m :: * -> *). MonadIO m => SerialT m (Array Word8) -> m ()
putChunks
(SerialT m (Array Word8) -> m ())
-> (SerialT m String -> SerialT m (Array Word8))
-> SerialT m String
-> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m (Array Word8)
-> SerialT m (Array Word8) -> SerialT m (Array Word8)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(IsStream t, Monad m) =>
m a -> t m a -> t m a
Stream.intersperseSuffix (Array Word8 -> m (Array Word8)
forall (m :: * -> *) a. Monad m => a -> m a
return (Array Word8 -> m (Array Word8)) -> Array Word8 -> m (Array Word8)
forall a b. (a -> b) -> a -> b
$ [Word8] -> Array Word8
forall a. Storable a => [a] -> Array a
Array.fromList [Word8
10])
(SerialT m (Array Word8) -> SerialT m (Array Word8))
-> (SerialT m String -> SerialT m (Array Word8))
-> SerialT m String
-> SerialT m (Array Word8)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SerialT m Char -> SerialT m Word8)
-> SerialT m String -> SerialT m (Array Word8)
forall (m :: * -> *) (t :: (* -> *) -> * -> *).
(MonadIO m, IsStream t) =>
(SerialT m Char -> SerialT m Word8)
-> t m String -> t m (Array Word8)
Unicode.encodeStrings SerialT m Char -> SerialT m Word8
forall (m :: * -> *) (t :: (* -> *) -> * -> *).
(Monad m, IsStream t) =>
t m Char -> t m Word8
Unicode.encodeUtf8