Z-IO-0.6.4.0: Simple and high performance IO toolkit for Haskell
Copyright(c) Dong Han 2018-2020
LicenseBSD
Maintainerwinterland1989@gmail.com
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

Z.IO.StdStream

Description

This module provides stdin/stderr/stdout reading and writings. Usually you don't have to use stderr or stderrBuf directly, Logger provides more logging utilities through stderr. While stdinBuf and stdoutBuf is useful when you write interactive programs, Buffered module provide many reading and writing operations. Example:

import Control.Concurrent.MVar
import Z.IO.LowResTimer
import Z.IO.Buffered
import Z.IO.StdStream
import qualified Z.Data.Vector as V
import qualified Z.Data.Builder as B
main = do
    -- read by 'n'
    b1 <- readStd
    -- read whatever user input in 3s, otherwise get Nothing
    b2 <- timeoutLowRes 30 $ withMVar stdinBuf readBuffer
    ...
    putStd "hello world!"

    -- Raw mode
    setStdinTTYMode UV_TTY_MODE_RAW
    forever $ do
        withMVar stdinBuf $  i -> withMVar stdoutBuf $  o -> do
            bs <- readBuffer i
            let Just key = V.headMaybe bs
            writeBuilder o (B.hex key)
            flushBuffer o
Synopsis

Standard input & output streams

data StdStream Source #

Standard input and output streams

We support both regular file and TTY based streams, when initialized uv_guess_handle is called to decide which type of devices are connected to standard streams.

Note StdStream is not thread safe, you shouldn't use them without lock. For the same reason you shouldn't use stderr directly, use Logger module instead.

Instances

Instances details
Show StdStream Source # 
Instance details

Defined in Z.IO.StdStream

Print StdStream Source # 
Instance details

Defined in Z.IO.StdStream

Methods

toUTF8BuilderP :: Int -> StdStream -> Builder () #

Output StdStream Source # 
Instance details

Defined in Z.IO.StdStream

Methods

writeOutput :: StdStream -> Ptr Word8 -> Int -> IO () Source #

Input StdStream Source # 
Instance details

Defined in Z.IO.StdStream

getStdStreamFD :: StdStream -> IO FD Source #

Get the standard stream's OS file descriptor.

isStdStreamTTY :: StdStream -> Bool Source #

Is this standard stream connected to a TTY device?

setStdinTTYMode :: TTYMode -> IO () Source #

Change terminal's mode if stdin is connected to a terminal, do nothing if stdout is not connected to TTY.

getStdoutWinSize :: HasCallStack => IO (CInt, CInt) Source #

Get terminal's output window size in (width, height) format, return (-1, -1) if stdout is not connected to TTY.

stdin :: StdStream Source #

The global stdin stream.

stdout :: StdStream Source #

The global stdout stream.

If you want a buffered device, consider use the stdoutBuf first. If you want to write logs, don't use stdout directly, use Logger instead.

stderr :: StdStream Source #

The global stderr stream.

If you want a buffered device, consider use the stderrBuf first. | If you want to write logs, don't use stderr directly, use Logger instead.

stdinBuf :: MVar BufferedInput Source #

A global buffered stdin stream protected by MVar.

If you want a buffered device, consider use the stdinBuf first.

stdoutBuf :: MVar BufferedOutput Source #

A global buffered stdout stream protected by MVar.

If you want to write logs, don't use stdoutBuf directly, use Logger instead.

stderrBuf :: MVar BufferedOutput Source #

A global buffered stderr stream protected by MVar.

If you want to write logs, don't use stderrBuf directly, use Logger instead.

utils

readStd :: HasCallStack => IO Bytes Source #

Read a line from stdin

This function will throw ECLOSED when meet EOF, which may cause trouble if stdin is connected to a file, use readLine instead.

printStd :: (HasCallStack, Print a) => a -> IO () Source #

Print a Print and flush to stdout, with a linefeed.

putStd :: HasCallStack => Builder a -> IO () Source #

Print a Builder and flush to stdout, with a linefeed.

re-export

withMVar :: MVar a -> (a -> IO b) -> IO b #

withMVar is an exception-safe wrapper for operating on the contents of an MVar. This operation is exception-safe: it will replace the original contents of the MVar if an exception is raised (see Control.Exception). However, it is only atomic if there are no other producers for this MVar.

Constant

TTYMode

type TTYMode = CInt Source #

Terminal mode.

When in TTY_MODE_RAW mode, input is always available character-by-character, not including modifiers. Additionally, all special processing of characters by the terminal is disabled, including echoing input characters. Note that CTRL+C will no longer cause a SIGINT when in this mode.