Yields the current coroutine until data is available on stdin,
then returns up to bytes bytes (default 4096).
Returns "" on EOF. Passing 0 also returns "".
Only one read may be pending at a time.
@eryx/stdio Module
Standard I/O module -- read/write access to stdin, stdout, and stderr.
Provides both synchronous (blocking) and asynchronous (yielding) APIs for interacting with the process's standard streams.
Quick start
local stdio = require("@eryx/stdio")
-- Prompt the user
stdio.write("Enter your name: ")
stdio.flush()
local name = stdio.readline()
stdio.write("Hello, " .. (name or "stranger") .. "!\n")
-- Check if running in a terminal
local tty = stdio.isatty()
if tty.stdin then
print("stdin is a terminal")
end
Summary
Functions
API Reference
Functions
stdio.read
⚠ Yields
stdio.readSync
Reads up to bytes bytes from stdin (default 4096).
Returns "" on EOF. Passing 0 also returns "".
stdio.readBuffer
⚠ Yields
Yields and reads from stdin as a buffer.
Returns an empty buffer on EOF.
stdio.readBufferSync
Reads from stdin synchronously as a buffer.
Returns an empty buffer on EOF.
stdio.readall
Reads stdin in chunks until EOF and returns all data as one string.
stdio.readline
Reads one line from stdin.
By default this reads until "\n" and strips the terminator.
Use options.terminator to change the delimiter and
options.keepTerminator=true to keep it.
Returns "" on EOF.
stdio.write
⚠ Yields
Yields, writes a string to stdout, and returns bytes written.
stdio.writeerr
⚠ Yields
Yields, writes a string to stderr, and returns bytes written.
stdio.flush
⚠ Yields
Yields and flushes the stdout buffer.
stdio.flusherr
⚠ Yields
Yields and flushes the stderr buffer.
stdio.writeSync
Writes a string to stdout synchronously. Returns the number of bytes written.
stdio.writeerrSync
Writes a string to stderr synchronously. Returns the number of bytes written.
stdio.flushSync
Flushes the stdout buffer synchronously.
stdio.flusherrSync
Flushes the stderr buffer synchronously.
stdio.isatty
Returns a table indicating whether each standard stream is connected to a terminal (TTY).
stdio.terminalSize
Returns the visible terminal size for stdout, when available.
stdio.setRawMode
Enables or disables terminal raw mode for stdin.
Raw mode is only available when stdin is a TTY. Returns the active raw-mode state after applying the change.
Types
TtyInfo
Result of isatty() -- indicates which streams are connected to a terminal.
StandardStreamControl
Sets whether this standard stream uses binary mode.
On Windows this controls CRT newline translation. On other platforms text and binary mode are equivalent, so this returns the requested state.
ReadLineOptions
Line terminator sequence to stop at. Defaults to "\n".
Whether to keep the matched terminator in the returned value.
Defaults to false.