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 = forall a c (m :: * -> *) b.
(a -> c) -> Unfold m c b -> Unfold m a b
Unfold.lmap (\() -> Handle
stdin) 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 = 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 = forall (m :: * -> *). Monad m => Stream m Word8 -> Stream m Char
Unicode.decodeUtf8 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 = forall a c (m :: * -> *) b.
(a -> c) -> Unfold m c b -> Unfold m a b
Unfold.lmap (\() -> Handle
stdin) 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 = 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 = 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 = 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 = 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 = forall (m :: * -> *). MonadIO m => Stream m Word8 -> m ()
putBytes forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = 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 = 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 = 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 = forall (m :: * -> *). MonadIO m => Stream m (Array Word8) -> m ()
putChunks forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall (m :: * -> *).
MonadIO m =>
(Stream m Char -> Stream m Word8) -> Stream m String -> m ()
putStringsWith 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 =
forall (m :: * -> *). MonadIO m => Stream m (Array Word8) -> m ()
putChunks
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => m a -> Stream m a -> Stream m a
Stream.intersperseMSuffix (forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Unbox a => [a] -> Array a
Array.fromList [Word8
10])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
MonadIO m =>
(Stream m Char -> Stream m Word8)
-> Stream m String -> Stream m (Array Word8)
Unicode.encodeStrings forall (m :: * -> *). Monad m => Stream m Char -> Stream m Word8
Unicode.encodeUtf8