ADR 0004: Zero-based (row, col) coordinates, resize reflow, and ?2026 stepping¶
Status¶
Accepted. 2026-11-21.
Context¶
Three coordinate and frame-lifecycle problems recur across terminal APIs:
Axis inversion: Legacy Python libraries pass
(cols, rows)to constructors and(x, y)on cursors while indexingscreen[row][col], inverting horizontal and vertical axes between construction and indexing.Cursor allocation churn and
pending_wrapedge cases: StoringScreen’s live cursor as a frozen dataclass re-allocated on every character,CR, andLFconsumed28%of total CPU incProfileruns (90,000+allocations per30,000lines). In addition,xterm/Ghosttyandtmux(s->cx == sxinscreen-write.c) differ subtly whenCUB(CSI D) orBS(0x08) arrives duringpending_wrapat the right margin.Torn
?2026frames, timer races, and resize reflow: Eagerly cloning all 24 rows on everyCSI ? 2026 hwastes work whenCSI ? 2026 larrives in the samefeed()buffer, while untagged async timeouts clearing a held snapshot can clobber a subsequent?2026batch. Onresize(), cropping rows without rejoining soft-wrapped (wrapped=True) primary lines breaks shell prompts and wrapped CLI output compared totmuxandGhostty.
Decision¶
Universal 0-based
(row, col)order:Every constructor, helper, and
resize()method accepts keyword-only(*, rows: int = 24, cols: int = 80). Zero(col, row)types exist inlibvt;Terminal.from_size(os.terminal_size)convertsos.terminal_size(columns,lines) explicitly.Screenstores_cur_row: int,_cur_col: int,_pending_wrap: bool,_cur_visible: bool,_cur_shape: CursorShape, and_cur_blinking: boolas scalars on the hot path, materializing a frozenCursordataclass on demand in@property def cursor.Indexing uses NumPy-style 2D syntax on
Terminal,Screen, andSnapshot:term[row] -> RowView,term[row, col] -> Cell, andterm[r1:r2, c1:c2] -> RegionView(supporting negative indices such asterm[-1]for status lines).Profile.pending_wrap_phantom_col+Mode.REVERSE_WRAP(?45) model bothxterm/Ghostty(DEFAULT_PROFILE) andtmux(TMUX_PROFILE, wherepending_wrapacts as phantom columncolsonCUB/BSandBSat column0reverse-wraps acrosswrappedlines).
Copy-on-write
?2026render hold and generation-tagged expiry:On
CSI ? 2026 h,Terminalincrements_sync_generation: intand marks_sync_hold_pending = Truewithout cloning rows immediately; only ifterm.committedis read while?2026remains active (or before the first post-hold mutation when a waiter is attached) is the pre-batchSnapshotmaterialized, andCSI ? 2026 lin the same chunk skips cloning altogether.term.expire_synchronized_output(sync_generation: int | None = None) -> boolreleases a stalled?2026hold, clearsMode.SYNCHRONIZED_OUTPUT, and emitsRenderHoldChanged(active=False, expired=True)only whensync_generationmatches_sync_generation, preventing stale async timers from releasing a newer frame.term.feed_until(data, *, offset=0, stop=...)bulk-scansGROUNDand control-string interiors (OSC/DCS/APC) to\x07/\x1bboundaries and checksnot parser.utf8_pendingbefore reportingStopOn.GROUND.
Primary-screen soft-wrap resize reflow (
resize(*, rows, cols, reflow=True)):Resizing
primarywhencolschanges andreflow=Truerejoinswrapped=Truelogical lines acrosshistory + viewport(elidingWidthRole.SPACER_HEADwrap-padding cells), re-wraps them to the new column width while tracking cursor(row, col), and pushes excess top rows intoFrozenRowscrollback rather than truncating bottom lines.Resizing
alternate(?1049) crops/pads in place without reflow, matching full-screen TUISIGWINCHredraw expectations; whenMode.IN_BAND_RESIZE(?2048) is enabled,resize()also queuesCSI 48 ; rows ; cols ; 0 ; 0 tand emitsInBandResize.
Alternatives considered¶
Positional
Terminal(80, 24)orTerminal(columns=80, rows=24): Rejected because mixing(X, Y)construction with[row, col]indexing invites silent axis transposition.Exposing four
CoordSpaceenums andStaleGridRefErrorcell handles: Rejected because immutableCellvalues,Snapshot, andterm.historyprovide safe inspection without runtime borrow-checker exceptions in Python.
Consequences¶
Positive¶
Eliminating per-character
Cursorallocation andfeed_until1-byte slicing removes the primary pure-Python CPU bottlenecks.Primary-screen
resize()matchestmuxandGhosttysoft-wrap reflow.Generation-tagged
?2026expiry eliminates async timer races.
Tradeoffs¶
reflow=Trueunpacks wrapped lines duringcolschanges (O(history + rows)), which callers can bypass withreflow=Falsewhen batch-resizing.
Risks¶
Wide characters landing on
cols - 1during reflow. Mitigation: emitSPACER_HEADatcols - 1and wrap the 2-column glyph cleanly to column0of the next reflowed row.