Copyright | (c) Dong Han 2018-2020 |
---|---|
License | BSD |
Maintainer | winterland1989@gmail.com |
Stability | experimental |
Portability | non-portable |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
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 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
- data StdStream
- getStdStreamFD :: StdStream -> IO FD
- isStdStreamTTY :: StdStream -> Bool
- setStdinTTYMode :: TTYMode -> IO ()
- withRawStdin :: IO a -> IO a
- getStdoutWinSize :: HasCallStack => IO (Int, Int)
- stdin :: StdStream
- stdout :: StdStream
- stderr :: StdStream
- stdinBuf :: MVar BufferedInput
- stdoutBuf :: MVar BufferedOutput
- stderrBuf :: MVar BufferedOutput
- readStd :: HasCallStack => IO Bytes
- printStd :: (HasCallStack, Print a) => a -> IO ()
- putStd :: HasCallStack => Builder a -> IO ()
- printStdLn :: (HasCallStack, Print a) => a -> IO ()
- printStdLnP :: (HasCallStack, Print a) => a -> Parser ()
- putStdLn :: HasCallStack => Builder a -> IO ()
- withMVar :: MVar a -> (a -> IO b) -> IO b
- type TTYMode = CInt
- pattern TTY_MODE_NORMAL :: TTYMode
- pattern TTY_MODE_RAW :: TTYMode
Standard input & output streams
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.
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.
withRawStdin :: IO a -> IO a Source #
Set stdin to raw mode before run IO, set back to normal after.
getStdoutWinSize :: HasCallStack => IO (Int, Int) Source #
Get terminal's output window size in (width, height) format, return (-1, -1) if stdout is not connected to TTY.
utils
readStd :: HasCallStack => IO Bytes Source #
Read a line from stdin(in normal mode).
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, without linefeed.
putStd :: HasCallStack => Builder a -> IO () Source #
Print a Builder
and flush to stdout, without linefeed.
printStdLn :: (HasCallStack, Print a) => a -> IO () Source #
Print a Print
and flush to stdout, with a linefeed.
printStdLnP :: (HasCallStack, Print a) => a -> Parser () Source #
Similar to printStdLn
, Parser
debug tool.
putStdLn :: 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
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.
pattern TTY_MODE_NORMAL :: TTYMode Source #
pattern TTY_MODE_RAW :: TTYMode Source #