io.tty
import io.tty
iolifts part of this module out to its own top level; each name below is shown with the path that reaches it. Anything still spelledio.tty.*needsimport io.tty.
TTY: control over a terminal attached to a file handle.
Raw and cooked modes, echo, cursor visibility and terminal size all live
here. The underlying constants are read from the platform’s own libc
at load time rather than hard-coded, because they differ between
operating systems.
Classes
TTY
class io.TTY
class TTY is an interface to TTY terminals this class contains definitions to control TTY terminals
Termios flag words are a Unix concept, so get_attr() and set_attr()
raise on Windows and every flag constant below reads as 0 there. What
those two are usually wanted for is not Unix-only, though: set_raw()
and exit_raw() work on both, Windows reaching its console mode
directly instead of through the flags.
exit_raw() and flush() never raise on any platform. Each reports
whether it had anything to do, so cleanup code can call them without
first establishing what state the terminal was in.
Constructor
io.TTY(std)
Parameters
std(file)
Note: file must be one of stdout and stderr
TTY.get_attr()
io.TTY.get_attr() -> dict
Returns the attributes of the current tty session. The returned value is a dict keyed by the TTY_ group constants.
Returns dict
Raises Error on a platform without termios, or if the stream has
no terminal behind it.
Note: Unix only. Termios flag words have no console equivalent, so this raises on Windows.
set_raw()andexit_raw()cover what attributes are usually reached for and work on both.
TTY.set_attr()
io.TTY.set_attr(option: int, attrs: dict) -> bool
sets the attributes of the current tty session
NOTE: - option must be one ot the TCSA options above (see their description above) - attrs must be a dictionary keyed by the TTY_ group constants above (TTY_IFLAG, TTY_OFLAG, TTY_CFLAG, TTY_LFLAG, TTY_ISPEED, TTY_OSPEED hold a single bit-flag number each; TTY_CC holds a list of control character values, indexed by the VEOF/VERASE/etc constants above) - one can safely omit any of the TTY_ groups listed above and Zuri will fill in the default values as it exists.
- This flags will be merged and not overwritten
Parameters
option(number)attr(dict)
Returns bool
Raises Error on a platform without termios, or if the stream has
no terminal behind it.
Note: Unix only, for the same reason
get_attr()is: there is no console representation of a termios flag word to apply, and accepting one would mean reporting success for a change that never happened.
TTY.set_raw()
io.TTY.set_raw() -> bool
Sets the current tty to raw mode: input arrives a keystroke at a time, nothing is echoed back, and Ctrl+C reaches the program as a keystroke rather than being turned into a signal first.
Works on Windows as well as Unix.
Returns bool
Raises Error if the stream has no terminal behind it, which is
what a redirected or captured stream is.
TTY.exit_raw()
io.TTY.exit_raw() -> bool
Disables the raw mode flags on the current tty, putting it back the way
set_raw() found it.
Never raises, so it is safe to call on a cleanup path without knowing
whether raw mode was ever entered. The return value says whether
anything was actually restored: false for a stream this TTY never put
into raw mode, and false on Windows, which has no raw mode to leave.
Returns bool
TTY.get_size()
io.TTY.get_size() -> dict
Returns the size of the current TTY device as a dictionary of cols and rows.
cols: the number of text columns that fit into the TTY device.rows: the number of text rows that fit into the TTY device.
Works on Windows as well as Unix.
Returns dict
Raises Error if the stream has no terminal behind it, which is
what a redirected or captured stream is.
TTY.flush()
io.TTY.flush() -> bool
Discards whatever is still queued on this TTY’s stream, both input that has been typed but not yet read and output that has been written but not yet sent.
Note that this throws that data away rather than writing it out, which
is the opposite of what file.flush() does.
Never raises. The return value says whether anything was discarded:
false for a stream with no terminal behind it, and false on Windows,
which keeps no such queue.
Returns bool
2026, Richard Ore and Zuri contributors