szn

Bug 452 — Grid.resize drops bottom rows via pop() on height reduction instead of scrolling top rows into history

452. Height reduction pops lines from screen bottom instead of scrolling top lines into history

Severity: MEDIUM
Status: Fixed
Files: src/grid.zig, src/screen.zig


Symptoms

  1. When splitting a pane horizontally (e.g. split-window -v or prefix + "), the original pane’s height is halved (e.g. from 24 rows to 11 rows).
  2. The user’s active prompt and latest command output (which were located at the bottom of the pane) disappear immediately.
  3. Instead of preserving the prompt and recent activity, the shrunk pane shows only the top rows of the previous screen, and the bottom rows are completely deleted rather than placed in scrollback.

Root Cause Analysis

In Grid.resize:

pub fn resize(self: *Grid, new_height: u32) Error!void {
    if (new_height == 0) return;
    try self.normalize();
    while (self.lines.items.len < new_height) {
        var line = GridLine{};
        try line.cells.resize(self.allocator, self.width);
        @memset(line.cells.items, Cell.empty());
        try self.lines.append(self.allocator, line);
    }
    while (self.lines.items.len > new_height) {
        var line = self.lines.pop().?;
        self.notifyEvict(line.cells.items);
        line.deinit(self.allocator);
    }
    self.height = new_height;
}
  1. lines.pop() discards newest/bottom rows: self.normalize() rotates the ring buffer so that row 0 is at index 0 and row height - 1 is at the end of self.lines.items. When new_height < self.lines.items.len, while (self.lines.items.len > new_height) self.lines.pop() removes rows from the end (the bottom of the screen).
  2. Standard terminal / tmux behavior violates: When terminal or pane height decreases, the rows at the top of the screen should scroll off the top into the scrollback history buffer (Grid.history), leaving the bottom rows (where the prompt and cursor reside) in view.
  3. Cursor clamping lands on stale rows: In Screen.resize, self.cursor.y is clamped to new_height - 1. Because the bottom lines were deleted rather than shifted up, the cursor is placed on top of whatever older text was at row new_height - 1, clobbering display state.

Proposed Fix

  1. When shrinking height in Grid.resize (new_height < self.lines.items.len):
    • Calculate const excess = self.lines.items.len - new_height;.
    • Scroll the first excess rows off the top of the visible screen into self.history (respecting history_limit and ring buffer indexing, identical to scrollUp).
    • Shift the remaining new_height rows to the front of self.lines.items and truncate self.lines.items.len = new_height.
  2. Add regression tests in src/grid.zig verifying that shrinking height pushes top lines into scrollback history and preserves the bottom lines on the visible screen.