Skip to main content

Module termprobe

Module termprobe 

Source
Expand description

Live terminal capability queries (UX §12.10), done once, only when stdin and stdout are both a TTY:

QuerySentAnswer
background colorOSC 11 ; ? STOSC 11 ; rgb:RRRR/GGGG/BBBB (BEL or ST)
Kitty keyboard protocolCSI ? uCSI ? <flags> u
synchronized outputCSI ? 2026 $ p (DECRQM)CSI ? 2026 ; <1-4> $ y
SGR mouseCSI ? 1006 $ pCSI ? 1006 ; <1-4> $ y
focus eventsCSI ? 1004 $ pCSI ? 1004 ; <1-4> $ y
truecolorCSI 38;2;1;2;3 m + DCS $ q m ST (DECRQSS) + CSI 0 mDCS 1 $ r …38:2:1:2:3 m ST if kept
sentinelCSI c (DA1)CSI ? … c — every terminal answers, so we stop waiting early

The truecolor query sets a foreground color, asks the terminal to report the current SGR, and resets it; a terminal that can’t show 24-bit color reports a downsampled 38;5;n (or nothing). No text is printed while the color is set, so nothing visible changes. Not sent on the Linux VT console (TERM=linux), whose parser treats ESC P as a palette command.

The terminal is put in non-canonical, no-echo mode on /dev/tty for at most TIMEOUT and always restored. Parsing is a pure function (parse_replies) so it is unit-tested without a terminal.

Structs§

Probe
What the terminal answered. None = no answer (unsupported, or swallowed by a multiplexer).

Enums§

ModeState
DECRQM answer for one private mode.

Constants§

QUERY
The full query, sentinel last.
QUERY_MINIMAL
For terminals that print queries they don’t parse. Apple Terminal renders the final byte of each DECRQM (CSI ? … $ p → “p”) and the body of DECRQSS (DCS $ q m ST → “$qm”) as text — the ppp$qm seen before oomtop doctor output. It answers OSC 11 and DA1, so only those are asked.
TIMEOUT
Upper bound for the whole probe (terminals answer in < 10 ms locally; SSH adds a round trip). 200 ms keeps the first frame under 300 ms (SPEC §14) even when the terminal never answers; a later answer is flushed.
WIPE_LINE
Returned to column 0 and cleared after the probe: wipes anything a terminal printed for a query it didn’t understand (a terminal we don’t know about, or Apple Terminal reached over SSH, where TERM_PROGRAM isn’t forwarded). Runs before anything else is written, so the line holds nothing else.

Functions§

interactive
True when both stdin and stdout are terminals (the only case in which we query).
parse_osc_color
Parses rgb:RRRR/GGGG/BBBB (1–4 hex digits per component) or #rrggbb.
parse_replies
Parses everything the terminal wrote back (in any order, possibly interleaved with nothing else).
probe
Queries the controlling terminal. None when not interactive, TERM is dumb/linux/unset, or /dev/tty can’t be used.
query_for
The query to send for TERM_PROGRAM, and whether it asks the DECRQM/kitty/DECRQSS questions.
skip_probe_for
Terminals that must not be queried: dumb answers nothing, and the Linux VT console (linux) parses ESC P / ESC ] as palette commands.
truecolor_from_decrqss
DECRQSS reply body (between ESC P and ST) for the SGR query: 1$r<sgr>m (valid) or 0$r (refused). Accepts both 38;2;1;2;3 and the colon forms 38:2:1:2:3 / 38:2::1:2:3 (xterm’s empty color space).