Page
Library
Module
Module type
Parameter
Class
Class type
Source
TerminalSourceTerminal protocol state machine.
Terminal manages terminal protocol state and writes escape sequences through a caller-provided output callback. The caller (runtime) owns all I/O resources: no Unix.select, no threads, no wakeup pipe, no signal handling.
Capabilities are seeded from environment heuristics at creation time (make). The runtime refines them by calling probe with I/O callbacks, or incrementally via apply_capability_event as the input parser produces capability responses.
Every protocol enabled through the handle is unwound by close or reset_state. Protocol operations are idempotent: requesting a mode that is already active is a no-op. For non-TTY handles (tty = false) escape sequences are suppressed but internal state tracking remains active.
The type for mouse tracking protocols.
`Off disables mouse tracking.`X10 is basic press-only tracking (legacy; coordinates limited to 223x223 cells).`Normal reports press and release events with standard xterm encoding (mode 1000).`Button reports press, release, and drag events (mode 1002).`Any reports all motion events, including motion with no buttons pressed (mode 1003).`Sgr_normal is SGR-extended press and release only (modes 1006 + 1000).`Sgr_button is SGR-extended with drag events (modes 1006 + 1000 + 1002).`Sgr_any is SGR-extended with all motion (modes 1006 + 1000 + 1002 + 1003).The type for Unicode width calculation methods.
`Wcwidth uses platform wcwidth()-like behaviour. Compatible with older setups but inaccurate for some emoji and combining characters.`Unicode uses modern Unicode width tables.The type for cursor visual styles. Corresponds to standard DECSCUSR shapes.
The type for tracked cursor positions. x and y are one-based coordinates; (1, 1) is the top-left corner. visible reflects the last requested visibility.
type capabilities = {term : string;Raw $TERM value used for heuristics.
rgb : bool;true if 24-bit colour is considered safe.
kitty_keyboard : bool;true if the Kitty keyboard protocol is supported.
kitty_graphics : bool;true if the Kitty graphics protocol is supported.
bracketed_paste : bool;true if bracketed paste (mode 2004) is supported.
focus_tracking : bool;true if focus tracking (mode 1004) is supported.
unicode_width : unicode_width;Current Unicode width mode.
*)sgr_pixels : bool;true if SGR pixel-position mouse reports (mode 1016) are supported.
color_scheme_updates : bool;true if colour-scheme update notifications (mode 2031) are supported.
explicit_width : bool;true if the terminal reported explicit cell-width information via proprietary cursor-position queries.
explicit_cursor_positioning : bool;true if the terminal may miscalculate grapheme widths but supports reliable cursor positioning. When set, the renderer repositions the cursor after each wide grapheme to prevent column drift. Only used when explicit_width is false.
scaled_text : bool;true if scaled text sizing is supported.
sixel : bool;true if SIXEL graphics are supported.
sync : bool;true if synchronised output (mode 2026) is supported.
hyperlinks : bool;true if OSC 8 hyperlinks are supported.
}The type for terminal capabilities.
Detection happens in two stages:
make inspects $TERM, $COLORTERM, $KITTY_WINDOW_ID, platform quirks, and overrides such as MATRIX_FORCE_UNICODE. No escape sequences are emitted.probe with I/O callbacks and the results are folded into the capability record.Boolean fields from probing are monotonic: once confirmed, subsequent probes do not disable them. Environment policy overrides may still disable features in specific contexts (e.g. tmux forces wcwidth mode).
The type for terminal emulator identity. name and version are populated from XTVersion when available; otherwise derived from $TERM_PROGRAM and $TERM_PROGRAM_VERSION. from_xtversion is true iff name and version came from an XTVersion response.
The type for terminal protocol state. A value of type t encapsulates an output callback, capability and terminal metadata, and protocol state (mouse mode, cursor, alternate screen, etc.).
Close with close to unwind all active protocols.
make ~output ~tty ~initial_caps () is a terminal handle with:
output receives escape sequence strings.tty controls whether escape sequences are emitted. Defaults to true. Set to false for non-TTY outputs or testing.initial_caps seeds the capability record. When omitted, capabilities are built from $TERM and environment heuristics. When provided, the record is used as-is and environment overrides are not applied.The handle starts with no protocols enabled.
close t unwinds all active protocols via output.
Disables mouse tracking, bracketed paste, focus reporting, Kitty keyboard, modifyOtherKeys, Unicode mode, and alternate screen. Resets cursor visibility, SGR attributes, cursor colour and style, and window title.
Note. Does not restore termios or close file descriptors; that is the runtime's responsibility.
capabilities t is t's current capability record.
set_capabilities t caps replaces t's capability record with caps.
terminal_info t is t's current terminal identity.
set_terminal_info t info replaces t's terminal identity with info.
pixel_resolution t is the last known pixel resolution as Some (width, height), or None if unknown.
set_pixel_resolution t res updates t's cached pixel resolution.
apply_capability_event t event folds a single capability response into t's state. Updates capabilities, terminal_info, and pixel_resolution as appropriate.
Called by the runtime as Input.Parser produces capability events.
val probe :
?timeout:float ->
on_event:(Input.t -> unit) ->
read_into:(bytes -> int -> int -> int) ->
wait_readable:(timeout:float -> bool) ->
parser:Input.Parser.t ->
t ->
unitprobe ~timeout ~on_event ~read_into ~wait_readable ~parser t actively probes terminal capabilities.
Sends a compound query payload through output and processes responses using the provided I/O callbacks and parser. Updates capabilities and terminal_info with the results.
Using the caller's parser ensures that partial escape sequences spanning the probe boundary are preserved. User input events received during probing are forwarded to on_event.
timeout is the maximum probe duration in seconds. Defaults to 0.2.
All protocol operations are idempotent. For TTY handles, escape sequences are emitted through output. For non-TTY handles, only internal state is updated.
restore_modes ?skip_focus t unconditionally re-sends enable sequences for every currently-active protocol mode (mouse tracking, focus reporting, bracketed paste, Kitty keyboard, modifyOtherKeys).
When skip_focus is true, the focus-tracking enable sequence is not re-sent. This avoids a feedback loop when restore_modes is called from a Focus event handler: some terminals re-report focus state upon receiving the enable sequence, creating an infinite cycle.
Intended for focus-in recovery: some terminal emulators (notably Windows Terminal / ConPTY) strip DEC private modes when the window loses focus.
Note. For the Kitty keyboard protocol, the existing stack entry is popped before re-pushing to avoid unbounded stack growth.
set_mouse_mode t mode configures mouse event tracking to mode. Conflicting modes are disabled first.
mouse_mode t is t's current mouse tracking mode. Initially `Off.
enable_bracketed_paste t b enables or disables bracketed paste mode (DECSET/DECRST 2004). Idempotent.
bracketed_paste_enabled t is true iff bracketed paste mode is active.
enable_focus_reporting t b enables or disables focus event reporting (DECSET/DECRST 1004). Idempotent.
focus_reporting_enabled t is true iff focus event reporting is active.
enable_kitty_keyboard ~flags t b enables or disables the Kitty keyboard protocol with:
flags is a bitmask controlling reported features. Defaults to 0b00101 (disambiguate escape codes + report alternate keys). The bits are:
Enabling pushes flags onto the terminal's keyboard stack; disabling pops. Changing flags while enabled pushes the new value.
kitty_keyboard_enabled t is true iff the Kitty keyboard protocol is active.
enable_modify_other_keys t b enables or disables xterm modifyOtherKeys mode. Idempotent.
modify_other_keys_enabled t is true iff modifyOtherKeys mode is active.
set_unicode_width t w sets the Unicode width mode to w and updates capabilities accordingly. Idempotent.
enter_alternate_screen t switches to the alternate screen buffer (DECSET 1049). Idempotent.
leave_alternate_screen t returns to the primary screen buffer (DECRST 1049). Idempotent.
set_scroll_region t ~top ~bottom sets the scrolling region (DECSTBM). top and bottom are one-based, inclusive. Idempotent.
clear_scroll_region t resets the scrolling region to the full screen. Idempotent.
scroll_region t is Some (top, bottom) if a scrolling region is set, or None if the full screen is used.
move_cursor ~visible t ~row ~col moves the cursor to one-based coordinates row, col. Coordinates are clamped to >= 1. visible defaults to true.
set_cursor_visible t b shows or hides the cursor (DECTCEM). Idempotent.
cursor_position t is t's tracked cursor position and visibility.
set_cursor_style t style ~blinking sets the cursor shape and blinking behaviour (DECSCUSR).
cursor_style_state t is the current cursor style and blinking flag as (style, blinking).
set_cursor_color t ~r ~g ~b ~a sets the cursor colour. Components are clamped to [0.0; 1.0]. NaN components are treated as 0.0. The alpha channel is tracked but not sent to the terminal.
cursor_color t is the last set cursor colour as (r, g, b, a).
reset_cursor_color t restores the terminal's default cursor colour.
query_pixel_resolution t sends a pixel resolution query (CSI 14 t) through output. The response arrives asynchronously and should be processed via apply_capability_event.
send t s writes s through the output callback. For non-TTY handles this is a no-op.
reset_state t resets all protocol state to defaults.
Disables mouse tracking, bracketed paste, focus events, Kitty keyboard, modifyOtherKeys, Unicode mode, scroll region, and alternate screen. Resets SGR attributes, cursor colour and style, and window title.
Stateless functions for terminal I/O. These do not require a t session handle.
set_raw fd puts fd into raw mode (no echo, no canonical processing, no signals, non-blocking) and returns the original termios for later restoration with restore.
restore fd termios restores fd to the given termios settings and clears non-blocking mode.
size fd is the terminal dimensions as (cols, rows). Falls back to (80, 24) on failure.
is_tty fd is true iff fd is a terminal.
flush_input fd discards unread input on fd via tcflush.
enable_vt fd enables VT100 processing on Windows consoles. No-op on Unix. Failures are silently ignored.