Skip to content

Keep visible screen cells and pending_wrap intact on CSI 3 J #6

Closed

Description

@tony

Summary

Terminal._erase_display() in src/libvt/terminal.py (#2) routes CSI 3 J (ED 3, Erase Saved Lines) into elif mode in (2, 3): and finishes with self._set_cursor(scr, c_row, c_col, pending_wrap=False), wiping all visible viewport rows (keep1, keep2) and clearing pending_wrap instead of erasing scrollback history alone.

Reproduction

  1. Scroll old into history with keep1 and keep2 on a 5x2 viewport and feed CSI 3 J:
$ uv run python -c 'from libvt import Terminal; vt = Terminal(rows=2, cols=5, scrollback=100); vt.feed(b"old\r\nkeep1\r\nkeep2\x1b[3J"); print("history:", [r.text for r in vt.history], "lines:", vt.lines, "wrap:", vt.cursor.pending_wrap)'

Expected

  • Primary screen CSI 3 J clears scrollback (history: []) while leaving visible rows ('keep1', 'keep2'), cursor coordinates, and pending_wrap=True untouched, matching xterm (ctlseqs, xterm-412), Ghostty (vt-1.0.1), and tmux (3.8).
  • Alternate screen (?1049h) CSI 3 J leaves visible alternate cells untouched and follows profile policy on primary history: TMUX_PROFILE and XTERM_PROFILE clear primary scrollback (matching tmux screen_alternate_on retaining history in s->grid and xterm util.c), while DEFAULT_PROFILE clears only active.clear_history() (preserving primary scrollback, matching Ghostty eraseDisplay(.scrollback)).

Actual

history: [] lines: () wrap: False — visible rows keep1 and keep2 are erased (matching Linux console_codes(4) and pyte's how in (2, 3) bug) and pending_wrap is cleared.

Environment
  • libvt: #2 (feat/v2-architecture, 0.1.0a0)
  • Python: 3.15.0 (unicodedata 17.0.0)
  • Reference tmux: 3.8a (tmux -f /dev/null)
  • OS: Linux x86_64
Affected code in src/libvt/terminal.py (#2)
elif mode in (2, 3):
    for r in range(scr.rows):
        scr._rows[r].clear_range(0, scr.cols, bce_w, bce_e, selective=selective)
    if mode == 3 and not selective:
        scr.clear_history()
    self._notify_erase(Rect(0, 0, scr.rows, scr.cols), cause)

Proposal

Invariants

The invariants below are load-bearing; everything under Intent is open.

  • CSI 3 J (mode == 3, selective=False) never mutates visible _rows, cursor (row, col), or pending_wrap.
  • DECSED 3 (CSI ? 3 J, selective=True) does not wipe visible rows or unguarded history unless specified.

Intent (non-binding)

  • Separate elif mode == 2: from elif mode == 3 and not selective: in _erase_display(), returning before self._set_cursor(scr, c_row, c_col, pending_wrap=False) when mode == 3.
  • Add a profile attribute (such as ed3_clears_primary_on_alt: bool, False on DEFAULT_PROFILE and True on XTERM_PROFILE and TMUX_PROFILE) controlling whether CSI 3 J on ?1049h clears self._screens.primary.clear_history().

Not doing

  • Altering CSI 2 J (ED 2) or CSI 22 J scroll-complete behavior.

References

Activity

  1. added
    bug
    on Nov 22, 2026
  2. tony commented on Nov 23, 2026

    @tony
    MemberAuthor

    Resolved in #2 (0c7ee41).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions