Expand description
Live terminal capability queries (UX §12.10), done once, only when stdin and stdout are both a TTY:
| Query | Sent | Answer |
|---|---|---|
| background color | OSC 11 ; ? ST | OSC 11 ; rgb:RRRR/GGGG/BBBB (BEL or ST) |
| Kitty keyboard protocol | CSI ? u | CSI ? <flags> u |
| synchronized output | CSI ? 2026 $ p (DECRQM) | CSI ? 2026 ; <1-4> $ y |
| SGR mouse | CSI ? 1006 $ p | CSI ? 1006 ; <1-4> $ y |
| focus events | CSI ? 1004 $ p | CSI ? 1004 ; <1-4> $ y |
| truecolor | CSI 38;2;1;2;3 m + DCS $ q m ST (DECRQSS) + CSI 0 m | DCS 1 $ r …38:2:1:2:3 m ST if kept |
| sentinel | CSI 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§
- Mode
State - 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 — theppp$qmseen beforeoomtop doctoroutput. 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_PROGRAMisn’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.
Nonewhen not interactive,TERMisdumb/linux/unset, or/dev/ttycan’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:
dumbanswers nothing, and the Linux VT console (linux) parsesESC P/ESC ]as palette commands. - truecolor_
from_ decrqss - DECRQSS reply body (between
ESC Pand ST) for the SGR query:1$r<sgr>m(valid) or0$r(refused). Accepts both38;2;1;2;3and the colon forms38:2:1:2:3/38:2::1:2:3(xterm’s empty color space).