szn

Bug 348 — #[default] in window-status-current-format resets terminal background and drops status bar background color

348. Status bar style resets drop baseline status-bg and status-fg

File: src/format.zig, src/status.zig, src/colour.zig, src/server/render.zig, src/server/server.zig
Severity: MEDIUM
Status: ✅ FIXED

Problem

When setting a custom active window format such as:

set -g window-status-current-format "#[bg=blue,fg=white]#I:#W#[default]"

the active window title rendered with the requested highlight, but all status bar text and background after the active window title (subsequent inactive window entries, padding, right status, and trailing fill spaces) reverted to the terminal emulator’s default background (black / transparent) instead of the configured status-bg.

Root Cause

  1. Raw SGR 0 Reset: In src/format.zig, #[default] and #[none] directly emitted \x1b[m (ANSI SGR 0 reset). When processed by the terminal emulator, SGR 0 clears all text styling and resets foreground and background to terminal defaults (\x1b[39m / \x1b[49m).
  2. Missing Baseline Style Propagation: format.zig’s Context, status.zig’s buildLine and packLine, and server.zig did not propagate the status bar’s baseline fg and bg (status-fg / status-bg) to format expansion. After #[default] ran in the active window format, the status bar never restored status-bg, causing the remainder of the line to be drawn with the terminal’s default background.
  3. Style Leakage Between Window Entries: Window formats that changed colors without an explicit #[default] at the end leaked their style attributes into subsequent window entries.
  4. Copy-Mode Status Line Cache Latching: DisplayClient cached the rendered status line by width (status_line_width) without tracking whether it was rendered in copy mode (status_line_copy_mode). Exiting copy mode left the yellow copy-mode status line cached in dc.status_line on non-dirty frames.

Fix

  1. src/colour.zig:
    • Added Colour.writeSgr and Colour.appendSgr to generate ANSI SGR escape sequences.
    • Added appendReset(allocator, out, fg, bg) to reset text attributes via \x1b[m and re-apply non-default baseline fg and bg colors.
  2. src/format.zig:
    • Extended Context with default_fg and default_bg baseline colors.
    • Updated applyStyleAttr and appendColourSgr so that #[default], #[none], #[fg=default], and #[bg=default] restore default_fg and default_bg.
    • Updated style attribute tokenizer to split on commas or whitespace (#[bold fg=yellow] and #[bold,fg=yellow]).
  3. src/status.zig:
    • Added fg and bg fields to BuildInput.
    • Updated buildLine and packLine to propagate baseline colors to Context and call appendReset on segment/window boundaries containing escape sequences.
  4. src/server/server.zig & src/server/render.zig:
    • Passed active status_fg and status_bg (including copy-mode overrides) into buildStatusLine.
    • Tracked status_line_copy_mode in DisplayClient to invalidate the cached status line on copy-mode enter/exit, and marked self.dirty = true on copy-mode transitions.
    • Updated renderStatusBar to reset any leftover SGR state before applying status bar theme colors.