ADR 0006: G0-G3 charsets, selective erase, and single-truth mode reconciliation¶
Status¶
Accepted. 2026-11-21.
Context¶
Differential testing against tmux and ncurses applications exposed three
classic VT100/VT420 gaps that break real-world terminal UIs even when modern
sequences work:
DEC Special Graphics (
ESC ( 0/SO/SI):tmux(tty-acs.c),ncurses,dialog,make menuconfig, andhtopdesignateG0orG1as DEC Special Graphics (0) and printl q k x m j w u t v nto draw┌ ─ ┐ │ └ ┘ ┬ ┤ ├ ┴ ┼. Dropping charset designations renders literallqqqkASCII letters instead of box-drawing glyphs.Tabulation and selective erase:
CBT(CSI Ps Zbackward tab) andCHT(CSI Ps Iforward tab) are standard horizontal cursor primitives, and VT220/VT420 applications useDECSCA(CSI Ps " q) withDECSED(CSI ? Ps J) andDECSEL(CSI ? Ps K) to clear unprotected fields while preserving protected form labels. Kitty-compatible shells also emitCSI 22 Jto push the visible screen into scrollback before clearing.Alternate-screen
DECRQMdesynchronization (?47/?1047/?1049): TrackingALT_SCREEN_47,ALT_SCREEN_1047, andALT_SCREEN(?1049) as independent booleans inModeStatecausesCSI ? 47 hfollowed byCSI ? 1049 l(or?1049hfollowed by?47l) to return to the primary screen whileCSI ? 47 $ pfalsely replies1(SET) instead of2(RESET), andFormatter(OutputFormat.VT)re-emits\x1b[?47h!
Decision¶
Per-screen
G0-G3charset slots and shifts (libvt.terminal):Track four charset slots (
G0,G1,G2,G3, each'B'US-ASCII,'0'DEC Special Graphics, or'A'UK), active locking-shift slotgl: int(0viaSI0x0F,1viaSO0x0E,2viaLS2ESC n,3viaLS3ESC o), and one-shot single shiftsingle_shift: int | None(2viaSS2ESC N,3viaSS3ESC O).Designate slots via
ESC ( F(G0),ESC ) F(G1),ESC * F(G2), andESC + F(G3). When the active slot for the next printed character is'0', map0x5F..0x7Ethrough the 32-entry DEC Special Graphics table (┌,─,┐,│,└,┘,┼,◆,▒,°,±,≤,≥,π,≠,£,·, etc.) before writing to_Row.Save and restore
(g_sets, gl)inside_SavedCursor(DECSC/DECRC/?1049) and reset to('B', 'B', 'B', 'B'), gl=0onDECSTRandRIS.
Tab navigation, selective erase, and scroll-clear:
Implement
CHT(CSI Ps I: advancePstabstops clamped to right margin) andCBT(CSI Ps Z: retreatPstabstops clamped to left margin, clearingpending_wrap). Keep sorted tabstops cached onScreen.Implement
DECSCA(CSI 0 " q/CSI 2 " qunprotected,CSI 1 " qprotected) stored on_CellExt.protected, respected byDECSED(CSI ? Ps J) andDECSEL(CSI ? Ps K) whileED/ELerase unconditionally.Support
CSI 22 J(push non-blank primary viewport lines intoFrozenRowscrollback before clearing the viewport).
Single-truth alternate-screen reconciliation (
libvt.modes):ScreenSet.active_is_altis the sole source of truth for whether the alternate buffer is active. Exiting via any of?47l,?1047l, or?1049lclears all three alt-screen mode bits inModeState, andModeState.decrpm()reconciles?47,?1047, and?1049againstactive_is_altsoDECRQMandFormatter(OutputFormat.VT)never report stale alt-screen state.
Alternatives considered¶
Ignoring
ESC ( 0in UTF-8 mode: Rejected becausencurses,dialog, andtmuxstill emitESC ( 0wheneveracscterminfo capabilities are active.Reporting
SETfor?47whenever?1049is active: Keep?1049as the canonical bit set when entering via?1049h(and?47when entering via?47h), while clearing all three bits whenever any exit sequence returnsactive_is_alttoFalse.
Consequences¶
Positive¶
ncursesandtmuxACS box-drawing borders render as real Unicode box characters (┌───┐) interm.textandassert_screen().?47/?1047/?1049transitions never leave ghost mode flags inDECRQMreplies orVTsnapshot exports.
Tradeoffs¶
on_print’s ASCII slice fast path runs unhindered when the active charset is'B'(99%+ of traffic) and translates viastr.translatewhen'0'or'A'is active.
Risks¶
Applications leaving
SO(0x0E->G1 = '0') latched after crashing. Mitigation:DECSTR(CSI ! p),RIS(ESC c), andsgr0(ESC ( Bemitted bytput sgr0) restoreG0US-ASCII.