szn

Bug 540 — status bar remnant on exit

540. Status bar remnant persists on screen after szn exits (#539 follow-up)

Severity: MEDIUM Status: ✅ FIXED Files: src/main.zig, src/server/render.zig

Problem

After the #539 fix, exiting szn still left a remnant of the status bar painted at the bottom of the screen. Detailed investigation uncovered three contributing defects:

  1. Duplicate display.exitAltScreen() execution (Commit 344b7f9 / rev 782): When moving the stdout drain and exitAltScreen() into a defer block, the existing defer display.exitAltScreen() catch ...; on line 702 was not removed. Because Zig executes defers in LIFO order, the inner defer drained out_buf and called exitAltScreen(), and then the outer defer called exitAltScreen() a second time. This sent duplicate \x1b[<1u and \x1b[?1049l sequences, corrupting keyboard protocol pop counts and restoring the cursor on the primary screen twice without a matching DECSC.

  2. Missing SGR reset and display clear in Display.exitAltScreen: Display.exitAltScreen sent only \x1b[?7h\x1b[<1u\x1b[?1000l\x1b[?1002l\x1b[?1006l\x1b[?2004l\x1b[?25h\x1b[?1049l. Unlike upstream tmux (which emits \x1b[m\x1b[H\x1b[2J...\x1b[?1049l), it lacked:
    • \x1b[m (SGR reset): The status bar applies background colours (e.g. \x1b[48;2;...m); without reset, text rendition attributes bleed into subsequent terminal output.
    • \x1b[H\x1b[2J (home + erase display): For terminal environments that do not isolate alternate screen buffers cleanly (or where alternate screen is disabled/unsupported), omitting display clear leaves the status bar intact on row sy.
    • \x1b[?2026l (synchronized update end): Any pending synchronized frame was left unterminated.
  3. Non-blocking stdout_fd during enterAltScreen(): setNonBlocking(stdout_fd) was called before display.enterAltScreen(). If writeBytes hit EAGAIN during startup, enterAltScreen() failed silently, causing szn to render entirely on the primary screen buffer where its status bar remained permanently after exit.

Fix

  1. src/main.zig:
    • Keep stdout_fd blocking until after display.enterAltScreen() completes.
    • Remove the duplicate outer defer display.exitAltScreen().
    • Use a single, unified defer block that restores blocking mode, drains out_buf, and runs display.exitAltScreen().
    • Activate setNonBlocking(stdout_fd) only after alternate screen entry.
  2. src/server/render.zig:
    • Update Display.exitAltScreen to emit \x1b[m\x1b[?2026l\x1b[H\x1b[2J\x1b[?7h\x1b[<1u\x1b[?1000l\x1b[?1002l\x1b[?1006l\x1b[?2004l\x1b[?25h\x1b[?1049l, matching upstream tmux behaviour.
    • Add unit test verifying enterAltScreen and exitAltScreen byte sequences.

Verification