module Streamly.Internal.Console.Stdio
(
read
, readChars
, readChunks
, reader
, chunkReader
, write
, writeChunks
, writeErr
, writeErrChunks
, putBytes
, putChars
, 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.Type (Array(..))
import Streamly.Internal.Data.Stream (Stream)
import Streamly.Internal.Data.Unfold (Unfold)
import Streamly.Internal.Data.Fold (Fold)
import qualified Streamly.Internal.Data.Array as Array
import qualified Streamly.Internal.Data.Stream as Stream
(intersperseMSuffix)
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 reader #-}
reader :: MonadIO m => Unfold m () Word8
reader :: forall (m :: * -> *). MonadIO m => Unfold m () Word8
reader = (() -> 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.reader
{-# INLINE read #-}
read :: MonadIO m => Stream m Word8
read :: forall (m :: * -> *). MonadIO m => Stream m Word8
read = Handle -> Stream m Word8
forall (m :: * -> *). MonadIO m => Handle -> Stream m Word8
Handle.read Handle
stdin
{-# INLINE readChars #-}
readChars :: MonadIO m => Stream m Char
readChars :: forall (m :: * -> *). MonadIO m => Stream m Char
readChars = Stream m Word8 -> Stream m Char
forall (m :: * -> *). Monad m => Stream m Word8 -> Stream m Char
Unicode.decodeUtf8 Stream m Word8
forall (m :: * -> *). MonadIO m => Stream m Word8
read
{-# INLINE chunkReader #-}
chunkReader :: MonadIO m => Unfold m () (Array Word8)
chunkReader :: forall (m :: * -> *). MonadIO m => Unfold m () (Array Word8)
chunkReader = (() -> 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.chunkReader
{-# INLINE readChunks #-}
readChunks :: MonadIO m => Stream m (Array Word8)
readChunks :: forall (m :: * -> *). MonadIO m => Stream m (Array Word8)
readChunks = Handle -> Stream m (Array Word8)
forall (m :: * -> *). MonadIO m => Handle -> Stream m (Array Word8)
Handle.readChunks Handle
stdin
{-# INLINE write #-}
write :: MonadIO m => Fold m Word8 ()
write :: forall (m :: * -> *). MonadIO m => 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 :: forall (m :: * -> *). MonadIO m => 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 => Stream m Word8 -> m ()
putBytes :: forall (m :: * -> *). MonadIO m => Stream m Word8 -> m ()
putBytes = Handle -> Stream m Word8 -> m ()
forall (m :: * -> *). MonadIO m => Handle -> Stream m Word8 -> m ()
Handle.putBytes Handle
stdout
{-# INLINE putChars #-}
putChars :: MonadIO m => Stream m Char -> m ()
putChars :: forall (m :: * -> *). MonadIO m => Stream m Char -> m ()
putChars = Stream m Word8 -> m ()
forall (m :: * -> *). MonadIO m => Stream m Word8 -> m ()
putBytes (Stream m Word8 -> m ())
-> (Stream m Char -> Stream m Word8) -> Stream m Char -> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Stream m Char -> Stream m Word8
forall (m :: * -> *). Monad m => Stream m Char -> Stream m Word8
Unicode.encodeUtf8
{-# INLINE writeChunks #-}
writeChunks :: MonadIO m => Fold m (Array Word8) ()
writeChunks :: forall (m :: * -> *). MonadIO m => Fold m (Array Word8) ()
writeChunks = Handle -> Fold m (Array Word8) ()
forall (m :: * -> *) a. MonadIO m => Handle -> Fold m (Array a) ()
Handle.writeChunks Handle
stdout
{-# INLINE writeErrChunks #-}
writeErrChunks :: MonadIO m => Fold m (Array Word8) ()
writeErrChunks :: forall (m :: * -> *). MonadIO m => Fold m (Array Word8) ()
writeErrChunks = Handle -> Fold m (Array Word8) ()
forall (m :: * -> *) a. MonadIO m => Handle -> Fold m (Array a) ()
Handle.writeChunks Handle
stderr
{-# INLINE putChunks #-}
putChunks :: MonadIO m => Stream m (Array Word8) -> m ()
putChunks :: forall (m :: * -> *). MonadIO m => Stream m (Array Word8) -> m ()
putChunks = Handle -> Stream m (Array Word8) -> m ()
forall (m :: * -> *) a.
MonadIO m =>
Handle -> Stream m (Array a) -> m ()
Handle.putChunks Handle
stdout
{-# INLINE putStringsWith #-}
putStringsWith :: MonadIO m
=> (Stream m Char -> Stream m Word8) -> Stream m String -> m ()
putStringsWith :: forall (m :: * -> *).
MonadIO m =>
(Stream m Char -> Stream m Word8) -> Stream m String -> m ()
putStringsWith Stream m Char -> Stream m Word8
encode = Stream m (Array Word8) -> m ()
forall (m :: * -> *). MonadIO m => Stream m (Array Word8) -> m ()
putChunks (Stream m (Array Word8) -> m ())
-> (Stream m String -> Stream m (Array Word8))
-> Stream m String
-> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Stream m Char -> Stream m Word8)
-> Stream m String -> Stream m (Array Word8)
forall (m :: * -> *).
MonadIO m =>
(Stream m Char -> Stream m Word8)
-> Stream m String -> Stream m (Array Word8)
Unicode.encodeStrings Stream m Char -> Stream m Word8
encode
{-# INLINE putStrings #-}
putStrings :: MonadIO m => Stream m String -> m ()
putStrings :: forall (m :: * -> *). MonadIO m => Stream m String -> m ()
putStrings = (Stream m Char -> Stream m Word8) -> Stream m String -> m ()
forall (m :: * -> *).
MonadIO m =>
(Stream m Char -> Stream m Word8) -> Stream m String -> m ()
putStringsWith Stream m Char -> Stream m Word8
forall (m :: * -> *). Monad m => Stream m Char -> Stream m Word8
Unicode.encodeUtf8
{-# INLINE putStringsLn #-}
putStringsLn :: MonadIO m => Stream m String -> m ()
putStringsLn :: forall (m :: * -> *). MonadIO m => Stream m String -> m ()
putStringsLn =
Stream m (Array Word8) -> m ()
forall (m :: * -> *). MonadIO m => Stream m (Array Word8) -> m ()
putChunks
(Stream m (Array Word8) -> m ())
-> (Stream m String -> Stream m (Array Word8))
-> Stream m String
-> m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. m (Array Word8) -> Stream m (Array Word8) -> Stream m (Array Word8)
forall (m :: * -> *) a. Monad m => m a -> Stream m a -> Stream m a
Stream.intersperseMSuffix (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. Unbox a => [a] -> Array a
Array.fromList [Word8
10])
(Stream m (Array Word8) -> Stream m (Array Word8))
-> (Stream m String -> Stream m (Array Word8))
-> Stream m String
-> Stream m (Array Word8)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Stream m Char -> Stream m Word8)
-> Stream m String -> Stream m (Array Word8)
forall (m :: * -> *).
MonadIO m =>
(Stream m Char -> Stream m Word8)
-> Stream m String -> Stream m (Array Word8)
Unicode.encodeStrings Stream m Char -> Stream m Word8
forall (m :: * -> *). Monad m => Stream m Char -> Stream m Word8
Unicode.encodeUtf8