From 8e850e6c923dcd84b7a5483f73377bd192b94a2b Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sun, 22 Nov 2026 17:55:03 -0300 Subject: terminals: where a view stands in a terminal's history is the view's, not the emulator's (pardes-term-join-10, item 1; the note's point 3, the coordinator's choice): `Pane.scrolled_to` (core/panes.zig), a per-view field (views.zig `pane_fields`, `PaneView`: in the rollback copy, the digest and the dump, each view's own), the row its top shows or null on the output, written only by a step (`scrollBy`, `followOutput`), and carried with every open view's through each write of the emulator that moves rows, once, in the confirmed unit's apply, on every side alike (`Term.feed`/`Term.resize` take the places; core/term/terminal.zig `carry`/`carried` pin each place's row on the primary screen and read the pin back: up by the rows the history drops, 0 when its own row goes, null in the active area or after a reset: a function of the fed bytes); the draw reads the rows from the view's place (`viewport`, `paint`, `ImagesAt.place`, terminal.zig `shown`), and ghostty's own viewport is never moved, read, digested or dumped (`Grid.offset`/`view`, `Parts.view` and the digest's offset gone): two clients no longer scroll one viewport (the new views.zig test failed on the base, `expected 80, found 77`), and a speculation's scroll is in the copy, so `putViews`, the view half of `keepViews` (now `keepSerials`), `viewTo`/`scroll`/`follow` (ops, wrappers, bodies), the Debug screen hash's view exclusion, `reflow`'s column-0 cure and `feedOutput`'s follow-after-read are deleted, and S1's `calls` lists no view write (the rows `viewTo`, `scroll`, `follow` gone); tests: the two-view test (with a dump), the place at the history's cap (`setMaxBytes(1)`), a 300-seed fuzz of the carried place against ghostty's own pinned view, a client's wheel over a shared terminal through output and a rollback, round 6's order explorer (30 seeds a run); the wire at 31 (abi/wire.zig, test/daemon.py `WIRE`): an installed build's dump loads with every view on the output; IN THE WORKSPACE core-test 912/1072 (160 skipped), shell-test 1114/1117 (3 skipped), term-test 16/16, kitty-test 45/45, pane-test 181/182, behaviour-test 275/275, --replay 275/275, process-test 0 of 146 failed, as pairs 0 of 146; gui unit-test 1707/1711, its one error BaselineHarnessMismatch (every base's) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/panes.zig | 116 +++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 84 insertions(+), 32 deletions(-) (limited to 'core/panes.zig') diff --git a/core/panes.zig b/core/panes.zig index d9759d18..032cb4db 100644 --- a/core/panes.zig +++ b/core/panes.zig @@ -375,6 +375,13 @@ pub const Pane = struct { /// it): the rows its top is passed over by, so its cursor stays in /// sight (Pane.panFor); 0 everywhere else. pan: u16 = 0, + /// Where this view stands in its terminal's history: the row its top + /// shows, counted from the history's top; null, on the output, following + /// it. Each view's own (views.zig), so the copy a rollback puts back + /// holds it, and the digest and the dump: the emulator keeps no view. + /// Carried with every other view's through each write of the emulator + /// that moves rows (`Scrolled`); one the active area holds is null. + scrolled_to: ?usize = null, /// The view whose step a `.name` prompt is (tagline.draftName): only /// that view's focus leaving the pane drops it. prompt_view: u8 = 0, @@ -597,7 +604,15 @@ pub const Pane = struct { /// current scroll offset: file top line, or the scrollback offset pub fn scroll(pane: *Pane) i32 { if (pane.file) |f| return @intCast(f.scroll); - return pane.surfRow(@as(i32, @intCast(Term.grid(pane.terminal).offset)) + pane.pan); + return pane.surfRow(@as(i32, @intCast(pane.termTop())) + pane.pan); + } + + /// The row of its terminal's history (from the top) this view's top + /// shows: where it is scrolled to, else the output's first. + pub fn termTop(pane: *const Pane) usize { + const g = Term.grid(pane.terminal); + const output = g.total - g.len; + return @min(pane.scrolled_to orelse output, output); } /// The line and byte a body row shows from; a ghost row (jj.zig) is no @@ -637,10 +652,14 @@ pub const Pane = struct { f.syntax_dirty = true; } } else if (pane.addon == null) { - // the vt scrolls in SHELL rows; convert through the edit buffer + // the vt scrolls in SHELL rows; convert through the edit buffer. + // The alternate screen has no history (ghostty's scrolls none): + // the place on the primary's stays. const state = pane.terminal orelse return; - const off: i32 = @intCast(Term.grid(state).offset); - Term.scroll(state, pane.gridRow(pane.surfRow(off) + delta) - off); + if (Term.modes(state).alternate) return; + const g = Term.grid(state); + const to: i64 = pane.gridRow(pane.surfRow(@intCast(pane.termTop())) + delta); + pane.scrolled_to = if (to >= g.total - g.len) null else @intCast(@max(0, to)); } } @@ -722,15 +741,16 @@ pub const Pane = struct { pane.body.cur_col = 0; } else { const grid = Term.grid(pane.terminal); - pane.body.cur_row = pane.surfRow(@as(i32, grid.cursor_y) + @as(i32, @intCast(grid.offset))); + pane.body.cur_row = pane.surfRow(@as(i32, grid.cursor_y) + @as(i32, @intCast(pane.termTop()))); pane.body.cur_col = grid.cursor_x; } pane.body.cur_pinned = true; } - /// Record and parse one live pty read, invalidate its motion surface, - /// and follow it only when the body (possibly parked under a tag edit) - /// is raw. + /// Record and parse one live pty read and invalidate its motion surface. + /// Sticky bottom, as every other terminal does it: a view on the output + /// follows it, one scrolled back into the history stays on its row + /// (`Scrolled`, every view's). pub fn feedOutput(pane: *Pane, p: *Pardes, bytes: []const u8) void { // There are no pty reads at all without an emulator to parse them into. const state = pane.terminal orelse return; @@ -738,25 +758,51 @@ pub const Pane = struct { const has_positions = pane.ovl != null or pane.body.cur_pinned or pane.body.vsel.active or pane.body.nsel > 0 or pane.body.last_edit != null or pane.look_at != null or pane.body.ed_undo_len > 0 or pane.body.ed_redo_len > 0 or pane.pointerRowCount() > 0; - // Sticky bottom, as every other terminal does it: output drags the - // viewport down only when the viewport was already on the last row, - // so a reader scrolled back into the scrollback stays where they - // scrolled. - const grid = Term.grid(state); - const at_bottom = grid.offset + grid.len >= grid.total; Term.cellPixels(state, p.view().cell_pixels.w, p.view().cell_pixels.h); + var scrolled: Scrolled = .of(pane, p.live); + defer scrolled.put(pane, p.live); if (!has_positions) { - _ = Term.feed(state, bytes, &.{}, &.{}, &.{}); - } else if (pane.followRows(state, bytes)) if (paneIdOf(p, pane)) |id| p.setMessage(.at(id), positions_lost); + _ = Term.feed(state, bytes, &.{}, &.{}, &.{}, scrolled.places()); + } else if (pane.followRows(state, bytes, scrolled.places())) if (paneIdOf(p, pane)) |id| p.setMessage(.at(id), positions_lost); p.shell_rows.markStale(pane); - if (pane.body.mode == .tty and at_bottom) pane.followOutput(); } + /// Every open view's place in its terminal's history (`scrolled_to`: + /// the live view's in the pane, each other's in its share, `pv`), + /// carried through a write of the emulator that moves rows + /// (`Term.feed`, `Term.resize`) and put back after it. + const Scrolled = struct { + at: [views.max]?usize = @splat(null), + /// One stands off the output: else the write carries none, at no + /// cost (a terminal's output, read after read). + any: bool = false, + + fn of(pane: *const Pane, live: u8) Scrolled { + var pl: Scrolled = .{}; + for (&pl.at, 0..) |*at, k| { + if (k == live) at.* = pane.scrolled_to else if (pane.pv_known >> @intCast(k) & 1 != 0) at.* = pane.pv[k].scrolled_to; + pl.any = pl.any or at.* != null; + } + return pl; + } + + fn places(pl: *Scrolled) []?usize { + return pl.at[0..if (pl.any) pl.at.len else 0]; + } + + fn put(pl: *const Scrolled, pane: *Pane, live: u8) void { + if (!pl.any) return; + for (pl.at, 0..) |at, k| { + if (k == live) pane.scrolled_to = at else if (pane.pv_known >> @intCast(k) & 1 != 0) pane.pv[k].scrolled_to = at; + } + } + }; + /// Its terminal fed `bytes`, with every position kept over its rows /// (the edit copy, the cursors and selections, their undo, the /// pointer's rows) moved as the rows move (`Term.Told`). True when one /// could not be kept (no memory for its pin). - fn followRows(pane: *Pane, state: *Term.State, bytes: []const u8) bool { + fn followRows(pane: *Pane, state: *Term.State, bytes: []const u8, places: []?usize) bool { const Group = struct { overlay: ?*Text.EditBuffer, cursor: *i32, selection: *Text.CharSel }; var groups: [Text.history_max * 2 + 1]Group = undefined; groups[0] = .{ @@ -852,11 +898,11 @@ pub const Pane = struct { var rows_buf: [512]i32 = undefined; var erased_buf: [512]bool = undefined; // Out of memory for them, the read is parsed and nothing moves. - const track = if (n <= track_buf.len) track_buf[0..n] else pane.gpa.alloc(i32, n) catch return feedLost(state, bytes); + const track = if (n <= track_buf.len) track_buf[0..n] else pane.gpa.alloc(i32, n) catch return feedLost(state, bytes, places); defer if (n > track_buf.len) pane.gpa.free(track); - const rows_after = if (n <= rows_buf.len) rows_buf[0..n] else pane.gpa.alloc(i32, n) catch return feedLost(state, bytes); + const rows_after = if (n <= rows_buf.len) rows_buf[0..n] else pane.gpa.alloc(i32, n) catch return feedLost(state, bytes, places); defer if (n > rows_buf.len) pane.gpa.free(rows_after); - const erased_after = if (n <= erased_buf.len) erased_buf[0..n] else pane.gpa.alloc(bool, n) catch return feedLost(state, bytes); + const erased_after = if (n <= erased_buf.len) erased_buf[0..n] else pane.gpa.alloc(bool, n) catch return feedLost(state, bytes, places); defer if (n > erased_buf.len) pane.gpa.free(erased_after); var k: usize = 0; for (spans[0..nspans]) |span| for ([_]i32{ span.row, span.row +| (span.rows - 1) }) |row| { @@ -867,7 +913,7 @@ pub const Pane = struct { track[k] = at.grid; k += 1; }; - const fed = Term.feed(state, bytes, track, rows_after, erased_after); + const fed = Term.feed(state, bytes, track, rows_after, erased_after, places); const after = tape.value(.positions, keepTold, .{Term.Told{ .rows = fed.rows, .erased = fed.erased, .cleared = fed.cleared, .alive = fed.alive, .failed = failed or fed.failed }}); if (after.rows.len != n or after.erased.len != n) return false; // a replica its authority's states apart (diverged) failed = after.failed; @@ -948,8 +994,8 @@ pub const Pane = struct { return t; } - fn feedLost(state: *Term.State, bytes: []const u8) bool { - _ = Term.feed(state, bytes, &.{}, &.{}, &.{}); + fn feedLost(state: *Term.State, bytes: []const u8, places: []?usize) bool { + _ = Term.feed(state, bytes, &.{}, &.{}, &.{}, places); return true; } @@ -961,14 +1007,17 @@ pub const Pane = struct { return count; } - /// Its terminal's view snapped back onto live output. + /// This view of its terminal snapped back onto live output. On the + /// alternate screen (no history) the place on the primary's stays, as + /// ghostty's own did. pub fn followOutput(pane: *Pane) void { const state = pane.terminal orelse return; - Term.follow(state); + if (!Term.modes(state).alternate) pane.scrolled_to = null; } - /// Its terminal's grid reflowed to `cols` by `rows`. - pub fn resizeGrid(pane: *Pane, cols: u16, rows: u16) void { + /// Its terminal's grid reflowed to `cols` by `rows`, every open view's + /// place carried through it (`Scrolled`; `live` the live view's index). + pub fn resizeGrid(pane: *Pane, live: u8, cols: u16, rows: u16) void { // A client's speculation does what its replay does (its // selections) but the grid: it waits for no round trip. Under bytes // it owes the grid (`touchTerminal`): its input is replayed when @@ -979,7 +1028,9 @@ pub const Pane = struct { pane.sel[button].state = .none; }; if (tape.speculating()) return tape.touchTerminal(); - Term.resize(state, cols, rows); + var scrolled: Scrolled = .of(pane, live); + defer scrolled.put(pane, live); + Term.resize(state, cols, rows, scrolled.places()); } /// The rows its terminal's top is passed over by in a view whose rect @@ -993,10 +1044,11 @@ pub const Pane = struct { if (shown == 0 or shown >= pane.rows) return 0; const state = pane.terminal orelse return 0; const grid = Term.grid(state); - // The cursor's row in the viewport; past its end (scrolled back), - // its last row. + const top = pane.termTop(); + // The cursor's row in the view; past its end (scrolled back), its + // last row. const row = grid.total - grid.len + grid.cursor_y; - const at: usize = if (row < grid.offset) 0 else @min(row - grid.offset, pane.rows - 1); + const at: usize = if (row < top) 0 else @min(row - top, pane.rows - 1); return @intCast(@min(pane.rows - shown, (at + 1) -| shown)); } -- cgit v1.3