From 5354090b6122faf0d18e246a55e8a4ead0955394 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 00:17:45 -0300 Subject: Term: output and view through the interface: Term.feed takes one read and the rows the core keeps positions on and gives back a Term.Told (where each went, erased or not, cleared, alive, a pin missed), Term.grid is the plain record of the view and the cursor (total, offset, len, the cursor, whether it shows, the view's row or null while it follows), Term.scroll, Term.follow, Term.viewTo, Term.resize (now or for later) and Term.resizeDeferred move it, Term.cellPixels sizes its cells in pixels and Term.replies hands over what it answered its program; the core's half is the core's: Pane.feedOutput and its row tracking (the edit copy's rows, the cursors and selections, their undo and the pointer's rows, gathered as rows and moved as the feed told, ingestWithPositions's core) and Pane.followOutput, Pane.resizeGrid and Pane.panFor in panes.zig with panes.resizeDeferred, the cursor a program shows read from the grid where the body draws it, tape.viewOnly beside tape.authorityOnly; scrollbar, gridOffset, gridCursor, visibleCursor, viewOf, atBottom and their types go; nothing else changes --- src/term/terminal.zig | 526 +++++++++++--------------------------------------- 1 file changed, 111 insertions(+), 415 deletions(-) (limited to 'src/term/terminal.zig') diff --git a/src/term/terminal.zig b/src/term/terminal.zig index 9472cfe1..50fca2d0 100644 --- a/src/term/terminal.zig +++ b/src/term/terminal.zig @@ -11,6 +11,7 @@ const tracy = @import("../tracy.zig"); const tagline = @import("../tagline.zig"); const Key = pardes.Key; const Pane = panes.Pane; +const Term = @import("../Term.zig"); const EditText = panes.EditText; const File = panes.File; @@ -510,55 +511,21 @@ pub fn terminalIo() std.Io { return std.Io.Threaded.global_single_threaded.io(); } -/// The three numbers ghostty's scrollbar reports; all zero without an emulator. -pub const Scrollbar = struct { total: usize = 0, offset: usize = 0, len: usize = 0 }; - -pub fn scrollbar(pane: *const Pane) Scrollbar { - const state = pane.terminal orelse return .{}; - const sb = state.vt.screens.active.pages.scrollbar(); - return .{ .total = sb.total, .offset = sb.offset, .len = sb.len }; -} - -/// The emulator's viewport offset, in SHELL rows: the top of what it shows. -pub fn gridOffset(pane: *const Pane) i32 { - return @intCast(scrollbar(pane).offset); -} - -/// Where the emulator itself puts the cursor, in active-area cells — the origin -/// without one, which is where an empty pane's cursor belongs anyway. -pub const GridCursor = struct { x: u16 = 0, y: u16 = 0 }; - -pub fn gridCursor(pane: *const Pane) GridCursor { - const state = pane.terminal orelse return .{}; - const cur = state.vt.screens.active.cursor; - return .{ .x = @intCast(cur.x), .y = @intCast(cur.y) }; -} - -pub fn visibleCursor(pane: *const Pane) ?GridCursor { - const state = pane.terminal orelse return null; - if (!state.vt.modes.get(.cursor_visible)) return null; - const cur = state.vt.screens.active.cursor; - const sb = scrollbar(pane); - const row = sb.total - sb.len + cur.y; - if (row < sb.offset + pane.pan or row - sb.offset >= sb.len) return null; - return .{ .x = @intCast(cur.x), .y = @intCast(row - sb.offset - pane.pan) }; -} - -/// The rows terminal `pane`'s top is passed over by in a view whose rect -/// `r` is shorter than it, one that does not size it (Pardes.sizes): as -/// many as keep its cursor's row in sight, as tmux pans a bigger window in -/// a smaller client. 0 in the view that sizes it, and in one tall enough. -pub fn panFor(p: *const Pardes, pane: *const Pane, r: pardes.Rect) u16 { - if (pane.sized_by == p.live or pane.collapsed or !pane.isTerminal()) return 0; - const shown = r.h -| pane.tag_rows; - if (shown == 0 or shown >= pane.rows) return 0; - const state = pane.terminal orelse return 0; - const sb = scrollbar(pane); - // The cursor's row in the viewport; past its end (scrolled back), its - // last row. - const row = sb.total - sb.len + state.vt.screens.active.cursor.y; - const at: usize = if (row < sb.offset) 0 else @min(row - sb.offset, pane.rows - 1); - return @intCast(@min(pane.rows - shown, (at + 1) -| shown)); +/// Where the view and the cursor are (`Term.Grid`); all zero without an +/// emulator. +pub fn grid(state: ?*const State) Term.Grid { + const s = state orelse return .{}; + const screen = s.vt.screens.active; + const sb = screen.pages.scrollbar(); + return .{ + .total = sb.total, + .offset = sb.offset, + .len = sb.len, + .cursor_x = @intCast(screen.cursor.x), + .cursor_y = @intCast(screen.cursor.y), + .cursor_visible = s.vt.modes.get(.cursor_visible), + .view = if (screen.pages.viewport == .active) null else sb.offset, + }; } /// What pardes made of a terminal's stream that its emulator does not hold @@ -596,72 +563,30 @@ pub fn takeMarks(state: *State, bytes: []const u8) void { const authorityOnly = pardes.tape.authorityOnly; -/// A speculation that moves a terminal's view: a screen-state replica's -/// may (its rollback puts the view back, replica.zig `views`, and a delta -/// sets it), any other stops (`authorityOnly`). -fn viewOnly(comptime why: []const u8) bool { - if (!pardes.tape.speculating()) return false; - if (pardes.tape.screenState()) { - pardes.tape.touchTerminal(); - return false; - } - pardes.tape.poison(why); - return true; -} - -/// Where a terminal's view is: its top row counted from the top of its -/// history, or null following its output (a replica's rollback copy, -/// which holds no emulator, keeps it so). -pub fn viewOf(pane: *const Pane) ?usize { - const state = pane.terminal orelse return null; - const pages = &state.vt.screens.active.pages; - if (pages.viewport == .active) return null; - return pages.scrollbar().offset; -} - -pub fn viewTo(pane: *Pane, at: ?usize) void { - const state = pane.terminal orelse return; +/// Its view to row `at` of its history (from the top), or onto its output. +pub fn viewTo(state: *State, at: ?usize) void { state.vt.screens.active.scroll(if (at) |row| .{ .row = row } else .active); } -/// Move the emulator's viewport by `delta` shell rows (negative scrolls back). -pub fn scrollGrid(pane: *Pane, delta: i32) void { - if (viewOnly("scroll a terminal")) return; - const state = pane.terminal orelse return; +/// Its view moved by `delta` rows (negative scrolls back). +pub fn scroll(state: *State, delta: i32) void { state.vt.screens.active.scroll(.{ .delta_row = delta }); state.changes +%= 1; } -/// Is the viewport already showing the last row of output? -pub fn atBottom(pane: *const Pane) bool { - const sb = scrollbar(pane); - return sb.offset + sb.len >= sb.total; -} - -/// Snap the viewport back onto live output. -pub fn followOutput(pane: *Pane) void { - if (viewOnly("follow a terminal")) return; - const state = pane.terminal orelse return; +/// Its view back onto the output. +pub fn follow(state: *State) void { const moved = state.vt.screens.active.pages.viewport != .active; state.vt.screens.active.scroll(.active); state.changes +%= @intFromBool(moved); } -/// Reflow the grid. A failed reflow keeps the grid it had rather than dropping -/// a scrollback; the next resize retries with the same numbers. -pub fn resizeGrid(pane: *Pane, cols: u16, rows: u16) void { - if (authorityOnly("resize a terminal")) return; - const state = pane.terminal orelse return; - if (cols != pane.cols) for (0..pane.sel.len) |button| { - pane.clearPointerSelection(button); - pane.sel[button].state = .none; - }; - // A replica's emulator is sized by its authority: the next delta says. - if (pardes.tape.screenReplica()) return; - // ...and the authority's once the step's unit is out (a client typing - // at another size than the last typist's read history rows its replay - // held as holes, and the rows came renumbered, 2026-10-30). - if (pardes.tape.screenAuthority()) { +/// Its grid reflowed to `cols` by `rows`, now or, `later`, when +/// `resizeDeferred` is called. A failed reflow keeps the grid it had +/// rather than dropping a scrollback; the next resize retries with the +/// same numbers. +pub fn resize(state: *State, cols: u16, rows: u16, later: bool) void { + if (later) { state.resize_to = .{ .cols = cols, .rows = rows }; return; } @@ -669,15 +594,30 @@ pub fn resizeGrid(pane: *Pane, cols: u16, rows: u16) void { state.vt.resize(state.gpa, .{ .cols = cols, .rows = rows }) catch {}; } -/// The sizes steps gave terminals (`resizeGrid`), made: between units, on an -/// authority with screen-state replicas (replica.Peers.unit). -pub fn resizeDeferred(p: *Pardes) void { - for (p.panes) |slot| if (slot) |pane| if (pane.terminal) |state| if (state.resize_to) |size| { - state.resize_to = null; - state.changes +%= 1; - p.shell_rows.markStale(pane); - state.vt.resize(state.gpa, .{ .cols = size.cols, .rows = size.rows }) catch {}; - }; +/// The size `resize` left for later, made; whether there was one. +pub fn resizeDeferred(state: *State) bool { + const size = state.resize_to orelse return false; + state.resize_to = null; + state.changes +%= 1; + state.vt.resize(state.gpa, .{ .cols = size.cols, .rows = size.rows }) catch {}; + return true; +} + +/// The emulator's size in pixels follows the core's cells: kitty graphics +/// placements are sized by it, and CSI 14/16 t report it. +pub fn cellPixels(state: *State, w: u32, h: u32) void { + if (comptime !images_enabled) return; + const vt = &state.vt; + vt.width_px = @as(u32, vt.cols) * w; + vt.height_px = @as(u32, vt.rows) * h; +} + +/// What it answered its program (a cursor report, its attributes) since +/// the last call, to be written to its pty behind whatever is queued +/// there; valid until it is fed again. +pub fn replies(state: *State) []const u8 { + defer state.reply_len = 0; + return state.reply[0..state.reply_len]; } /// DECSET 2004: the program wants its pastes bracketed. @@ -908,48 +848,9 @@ fn ingest(state: *State, bytes: []const u8) void { state.changes +%= 1; } -/// What a pane says when a position kept over its terminal's rows could -/// not follow the output (no memory for its pin), so it was reset. -const positions_lost = "terminal edit position reset: out of memory"; - -/// 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. -pub fn feedOutput(p: *Pardes, pane: *Pane, bytes: []const u8) void { - // There are no pty reads at all without an emulator to parse them into. - if (pane.terminal == null) return; - if (authorityOnly("a terminal's output")) return; - 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 pointerRowCount(pane) > 0; - // A replica's emulator is what the authority's deltas make it; what - // pardes keeps over it moves as the authority's ingest told it. - if (pardes.tape.screenReplica()) { - if (has_positions and ingestWithPositions(pane, bytes)) if (panes.paneIdOf(p, pane)) |id| p.setMessage(id, positions_lost); - return; - } - // 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 at_bottom = atBottom(pane); - syncPixels(p, pane); - if (!has_positions) { - ingest(pane.terminal.?, bytes); - } else if (ingestWithPositions(pane, bytes)) if (panes.paneIdOf(p, pane)) |id| p.setMessage(id, positions_lost); - p.shell_rows.markStale(pane); - if (pane.body.mode == .tty and at_bottom) followOutput(pane); -} - const RowPin = struct { pin: ?*ghostty_vt.Pin = null, offset: i32 = 0 }; -const PositionPin = struct { - target: *i32, - row: RowPin, - overlay: ?*EditBuffer, - edit_row: ?i32 = null, -}; -fn trackRow(maybe_pages: ?*ghostty_vt.PageList, row: i32, failed: *bool) RowPin { - // A screen-state replica's: told by the tape, not its emulator. - const pages = maybe_pages orelse return .{ .offset = row }; +fn trackRow(pages: *ghostty_vt.PageList, row: i32, failed: *bool) RowPin { if (row < 0) return .{ .offset = row }; const bounded: usize = @min(@as(usize, @intCast(row)), pages.total_rows - 1); var left = pages.total_rows - 1 - bounded; @@ -979,236 +880,43 @@ fn pinnedRow(pages: ?*ghostty_vt.PageList, row: RowPin) i32 { return 0; } -fn trackPosition(pages: ?*ghostty_vt.PageList, target: *i32, overlay: ?*EditBuffer, failed: *bool) PositionPin { - var grid = target.*; - if (overlay) |edit| { - const lines: i32 = @intCast(modal.lineCount(edit.text)); - if (grid >= edit.row and grid < edit.row +| lines) - return .{ .target = target, .row = .{}, .overlay = edit, .edit_row = grid - edit.row }; - if (grid > edit.row) grid = grid -| lines +| edit.rows; +/// One read of its program's output. `track` are rows (absolute, from the +/// top of the history) the core keeps positions on; each is told in +/// `rows` where it is after the read and in `erased` whether the program +/// erased it (a negative row passes through untouched). With nothing +/// tracked the read is parsed and nothing more. +pub fn feed(state: *State, bytes: []const u8, track: []const i32, rows: []i32, erased: []bool) Term.Told { + if (track.len == 0) { + ingest(state, bytes); + return .{ .rows = rows[0..0], .erased = erased[0..0], .cleared = false, .alive = true, .failed = false }; } - return .{ .target = target, .row = trackRow(pages, grid, failed), .overlay = overlay }; -} - -fn pointerRowCount(pane: *const Pane) usize { - var count: usize = 0; - for (pane.sel) |gesture| if (pane.pointerSelection(gesture)) |rows| { - count += rows.len; - }; - return count; -} - -/// What an ingest told the positions over it (`ingestWithPositions`): each -/// tracked row where it is after it (the spans' starts and ends, the -/// positions, the pointer rows, in that order) and whether the program -/// erased it, whether the screen was cleared, whether its pages lived and -/// whether a pin could not be made. Taped: a screen-state replica, which -/// ingests nothing, is told the authority's. -pub const Told = struct { rows: []const i32, erased: []const bool, cleared: bool, alive: bool, failed: bool }; - -fn told(t: Told) Told { - return t; -} - -/// `ingest`, with every position kept over the rows (the edit copy, the -/// cursors and selections, their undo, the pointer's rows) moved as the -/// rows move. True when one could not be kept (no memory for its pin). -fn ingestWithPositions(pane: *Pane, bytes: []const u8) bool { - const screens = &pane.terminal.?.vt.screens; + const screens = &state.vt.screens; const key = screens.active_key; const generation = screens.generation(key); - const replica = pardes.tape.screenReplica(); - const pages: ?*ghostty_vt.PageList = if (replica) null else &screens.active.pages; - const Group = struct { overlay: ?*EditBuffer, cursor: *i32, selection: *panes.Text.CharSel }; - var groups: [history_max * 2 + 1]Group = undefined; - groups[0] = .{ - .overlay = if (pane.ovl) |*overlay| overlay else null, - .cursor = &pane.body.cur_row, - .selection = &pane.body.vsel, - }; - var ngroups: usize = 1; - for ([_][]Snapshot{ pane.body.ed_undo[0..pane.body.ed_undo_len], pane.body.ed_redo[0..pane.body.ed_redo_len] }) |history| { - for (history) |*snapshot| { - groups[ngroups] = .{ - .overlay = if (snapshot.ovl) |*overlay| overlay else null, - .cursor = &snapshot.cur_row, - .selection = &snapshot.vsel, - }; - ngroups += 1; - } - } - const Span = struct { overlay: *EditBuffer, start: RowPin, end: RowPin }; - var spans: [groups.len]Span = undefined; - var nspans: usize = 0; - var positions: [groups.len * 2 + panes.Text.max_selections * 2 + 4]PositionPin = undefined; - var npositions: usize = 0; + const pages = &screens.active.pages; var failed = false; - for (groups[0..ngroups]) |group| { - if (group.overlay) |overlay| { - spans[nspans] = .{ - .overlay = overlay, - .start = trackRow(pages, overlay.row, &failed), - .end = trackRow(pages, overlay.row +| (overlay.rows - 1), &failed), - }; - nspans += 1; - } - positions[npositions] = trackPosition(pages, group.cursor, group.overlay, &failed); - npositions += 1; - if (group.selection.active) { - positions[npositions] = trackPosition(pages, &group.selection.row, group.overlay, &failed); - npositions += 1; - } - } - const overlay = groups[0].overlay; - // Where the live pane's own selections sit in `positions`: one whose - // row the program erased goes (below), as the edited copy does. - const vsel_at: ?usize = if (pane.body.vsel.active) 1 else null; - const sels_at = npositions; - var extra: [panes.Text.max_selections * 2 + 2]*i32 = undefined; - var nextra: usize = 0; - for (pane.body.sels[0..pane.body.nsel]) |*selection| { - extra[nextra] = &selection.row; - extra[nextra + 1] = &selection.arow; - nextra += 2; - } - if (pane.body.last_edit) |*at| { - extra[nextra] = &at.row; - nextra += 1; - } - if (pane.look_at) |*at| { - extra[nextra] = &at.row; - nextra += 1; - } - for (extra[0..nextra]) |target| { - positions[npositions] = trackPosition(pages, target, overlay, &failed); - npositions += 1; - } - const pointer_positions = pane.gpa.alloc(PositionPin, pointerRowCount(pane)) catch null; - defer if (pointer_positions) |pins| pane.gpa.free(pins); - if (pointer_positions) |pins| { - var index: usize = 0; - for (pane.sel) |gesture| if (pane.pointerSelection(gesture)) |rows| { - for (rows) |*row| { - pins[index] = trackPosition(pages, &row.row, if (row.raw_terminal) null else overlay, &failed); - index += 1; - } - }; - } else failed = true; - var cleared = false; - var alive: ?*ghostty_vt.PageList = null; - if (!replica) { - // Whether the program cleared the screen (ED 2, a switch of - // screens); pardes reads the flag nowhere else. - pane.terminal.?.vt.flags.dirty.clear = false; - ingest(pane.terminal.?, bytes); - cleared = pane.terminal.?.vt.flags.dirty.clear; - // RIS can destroy the alternate screen and every pin it owned. - alive = if (screens.generation(key) == generation) pages else null; - } - const pointers: []PositionPin = pointer_positions orelse &.{}; - const n = 2 * nspans + npositions + pointers.len; - // On the stack, as the local path never allocated here but for the - // pointer's rows. - var rows_buf: [512]i32 = undefined; - var erased_buf: [512]bool = undefined; - const rows_after = if (n <= rows_buf.len) rows_buf[0..n] else pane.gpa.alloc(i32, n) catch return true; - 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 true; - defer if (n > erased_buf.len) pane.gpa.free(erased_after); - @memset(rows_after, 0); - @memset(erased_after, false); - if (!replica) { - var k: usize = 0; - for (spans[0..nspans]) |span| for ([_]RowPin{ span.start, span.end }) |rp| { - rows_after[k] = pinnedRow(alive, rp); - erased_after[k] = alive != null and rp.pin != null and rp.pin.?.garbage; - k += 1; - }; - for ([_][]PositionPin{ positions[0..npositions], pointers }) |batch| for (batch) |position| { - rows_after[k] = pinnedRow(alive, position.row); - erased_after[k] = alive != null and position.row.pin != null and position.row.pin.?.garbage; - k += 1; - }; - } - const after = pardes.tape.value(.positions, told, .{Told{ .rows = rows_after, .erased = erased_after, .cleared = cleared, .alive = alive != null, .failed = failed }}); - if (after.rows.len != n or after.erased.len != n) return false; // a replica its authority's states apart (diverged) - failed = after.failed; - const at_position = 2 * nspans; - const at_pointer = at_position + npositions; - var moved = false; - var pointer_rows_lost = !after.alive or pointer_positions == null; - for (0..pointers.len) |i| if (after.erased[at_pointer + i]) { - pointer_rows_lost = true; - }; - // The program cleared the screen, or the rows the live copy stood on are - // gone (pruned, a reset): the copy goes with them, as what the program - // drew next. Kept, ghostty would move its rows to the top of what is - // left, over the program's new text. - var copy_lost = after.cleared or !after.alive; - for (spans[0..nspans], 0..) |span, i| { - if (span.overlay == overlay and after.erased[2 * i]) copy_lost = true; - const start = after.rows[2 * i]; - const rows = @max(1, after.rows[2 * i + 1] -| start +| 1); - moved = moved or start != span.overlay.row or rows != span.overlay.rows; - span.overlay.row = start; - span.overlay.rows = rows; - if (alive) |owner| { - if (span.start.pin) |pin| owner.untrackPin(pin); - if (span.end.pin) |pin| owner.untrackPin(pin); - } - } - // The rule above for the copy holds for the selections: one whose row - // is gone (ED 3 erased its history row, which ghostty flags `garbage`) - // goes too, rather than being clamped to the top. - const erased = after.erased[at_position..]; - const vsel_lost = if (vsel_at) |i| - erased[i] or (copy_lost and positions[i].edit_row != null) - else - false; - var sels_keep: [panes.Text.max_selections]bool = undefined; - for (0..pane.body.nsel) |i| sels_keep[i] = !(erased[sels_at + 2 * i] or erased[sels_at + 2 * i + 1]); - for ([_][]PositionPin{ positions[0..npositions], pointers }, [_]usize{ at_position, at_pointer }) |batch, first| for (batch, first..) |position, k| { - var row = after.rows[k]; - if (position.overlay) |edit| { - if (position.edit_row) |relative| { - row = edit.row +| relative; - } else if (row > edit.row) { - row = if (row < edit.row +| edit.rows) - edit.row - else - row -| edit.rows +| @as(i32, @intCast(modal.lineCount(edit.text))); - } - } - moved = moved or row != position.target.*; - position.target.* = row; - if (alive) |owner| if (position.row.pin) |pin| owner.untrackPin(pin); - }; - if (pointer_rows_lost) for (0..pane.sel.len) |button| { - pane.clearPointerSelection(button); - pane.sel[button].state = .none; + var room: [512]RowPin = undefined; + const pins = if (track.len <= room.len) room[0..track.len] else state.gpa.alloc(RowPin, track.len) catch { + ingest(state, bytes); + @memcpy(rows[0..track.len], track); + @memset(erased[0..track.len], false); + return .{ .rows = rows[0..track.len], .erased = erased[0..track.len], .cleared = false, .alive = true, .failed = true }; }; - if (vsel_lost) { - pane.body.vsel.active = false; - moved = true; - } - if (pane.body.nsel > 0) { - var kept: u16 = 0; - for (0..pane.body.nsel) |i| if (sels_keep[i]) { - pane.body.sels[kept] = pane.body.sels[i]; - kept += 1; - }; - if (kept != pane.body.nsel) { - moved = true; - if (kept == 0) pane.body.dropSels() else pane.body.nsel = kept; - } - } - if (copy_lost) if (pane.ovl) |copy| { - pane.gpa.free(copy.text); - pane.ovl = null; - moved = true; - }; - if (moved) pane.nsel_snap = 0; // Saved regex-preview offsets name the old flat text. - return failed; + defer if (track.len > room.len) state.gpa.free(pins); + for (pins, track) |*pin, row| pin.* = trackRow(pages, row, &failed); + // Whether the program cleared the screen (ED 2, a switch of screens); + // pardes reads the flag nowhere else. + state.vt.flags.dirty.clear = false; + ingest(state, bytes); + const cleared = state.vt.flags.dirty.clear; + // RIS can destroy the alternate screen and every pin it owned. + const alive: ?*ghostty_vt.PageList = if (screens.generation(key) == generation) pages else null; + for (pins, rows[0..track.len], erased[0..track.len]) |pin, *row, *gone| { + row.* = pinnedRow(alive, pin); + gone.* = alive != null and pin.pin != null and pin.pin.?.garbage; + if (alive) |owner| if (pin.pin) |held| owner.untrackPin(held); + } + return .{ .rows = rows[0..track.len], .erased = erased[0..track.len], .cleared = cleared, .alive = alive != null, .failed = failed }; } /// A full-screen program has the emulator on its alternate screen. @@ -1416,7 +1124,7 @@ pub fn enterTty(p: *Pardes, id: usize) void { } pane.body.mode = .tty; - followOutput(pane); // entering raw mode is a request for the live shell + pane.followOutput(); // entering raw mode is a request for the live shell pane.body.vsel.active = false; pane.body.nsel = 0; // The raw renderer follows the live cursor. A captured mouse selection @@ -1800,7 +1508,7 @@ const BodyWalk = struct { fn init(pane: *Pane) BodyWalk { const off = pane.scroll(); - const goff = gridOffset(pane); + const goff: i32 = @intCast(grid(pane.terminal).offset); return .{ .pane = pane, .goff = goff, @@ -2003,7 +1711,7 @@ pub fn recolorAnsi(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, tx: u16 // path needs it per cell. const scnm = pane.terminal.?.vt.modes.get(.reverse_colors); const pages = &pane.terminal.?.vt.screens.active.pages; - const vp_rows: i32 = @intCast(scrollbar(pane).len); + const vp_rows: i32 = @intCast(grid(pane.terminal).len); // Which buffer lines the user has not actually changed, so a row swallowed // by a growing buffer keeps the colour it still stands over. var anchors: ?EditAnchors = null; @@ -2385,16 +2093,6 @@ pub fn undoStep(p: *Pardes, pane: *Pane, back: bool) error{OutOfMemory}!void { pane.ensureCursorVisible(); } -/// The emulator's size in pixels follows the shell's cells: kitty graphics -/// placements are sized by it, and CSI 14/16 t report it. -fn syncPixels(p: *const Pardes, pane: *Pane) void { - if (comptime !images_enabled) return; - const state = pane.terminal orelse return; - const vt = &state.vt; - vt.width_px = @as(u32, vt.cols) * p.view().cell_pixels.w; - vt.height_px = @as(u32, vt.rows) * p.view().cell_pixels.h; -} - /// When the pane's next kitty animation frame is due (ms on the core's /// clock), as its last drawn frame left it; null when nothing plays. pub fn due(state: *const State) ?u64 { @@ -2431,10 +2129,10 @@ pub fn drawImages(p: *Pardes, s: *Surface, pane: *Pane, id: usize, tx: u16, tw: if (comptime !images_enabled) return; const state = pane.terminal orelse return; const screen = state.vt.screens.active; - syncPixels(p, pane); + cellPixels(state, p.view().cell_pixels.w, p.view().cell_pixels.h); // A body taller than this draws its images on the rows that fit. var rows_of: [1024]?u16 = undefined; - const vp_rows: usize = @min(scrollbar(pane).len, rows_of.len); + const vp_rows: usize = @min(grid(state).len, rows_of.len); @memset(rows_of[0..vp_rows], null); var any_placeholder = false; var walk: BodyWalk = .init(pane); @@ -2522,7 +2220,7 @@ test "a terminal made fresh has no size waiting: one never resized keeps its gri @memset(ptr[0..len], 0xAA); return ptr; } - fn resize(ctx: *anyopaque, buf: []u8, a: std.mem.Alignment, n: usize, ra: usize) bool { + fn grow(ctx: *anyopaque, buf: []u8, a: std.mem.Alignment, n: usize, ra: usize) bool { const self: *@This() = @ptrCast(@alignCast(ctx)); return self.child.rawResize(buf, a, n, ra); } @@ -2534,7 +2232,7 @@ test "a terminal made fresh has no size waiting: one never resized keeps its gri const self: *@This() = @ptrCast(@alignCast(ctx)); self.child.rawFree(buf, a, ra); } - const vtable: std.mem.Allocator.VTable = .{ .alloc = alloc, .resize = resize, .remap = remap, .free = free }; + const vtable: std.mem.Allocator.VTable = .{ .alloc = alloc, .resize = grow, .remap = remap, .free = free }; }; var filling: Filling = .{ .child = std.testing.allocator }; const gpa: std.mem.Allocator = .{ .ptr = &filling, .vtable = &Filling.vtable }; @@ -2555,16 +2253,14 @@ test "document construction allocates only the pane and has inert terminal defau try std.testing.expectEqual(@as(usize, 0), failing.resize_index); try std.testing.expect(!failing.has_induced_failure); try std.testing.expectEqual(@as(usize, @sizeOf(Pane)), failing.allocated_bytes); - try std.testing.expectEqual(GridCursor{}, gridCursor(pane)); - try std.testing.expectEqual(Scrollbar{}, scrollbar(pane)); + try std.testing.expectEqual(Term.Grid{}, grid(pane.terminal)); try std.testing.expect(!bracketedPaste(pane)); try std.testing.expect(!reportsMouse(pane)); try std.testing.expect(!promptInputReady(pane)); - scrollGrid(pane, 100); - followOutput(pane); - resizeGrid(pane, 80, 24); - try std.testing.expectEqual(GridCursor{}, gridCursor(pane)); - try std.testing.expectEqual(Scrollbar{}, scrollbar(pane)); + pane.scrollBy(100); + pane.followOutput(); + pane.resizeGrid(80, 24); + try std.testing.expectEqual(Term.Grid{}, grid(pane.terminal)); try std.testing.expectEqualStrings("", try screenTextAlloc(pane, allocator)); try std.testing.expect(!failing.has_induced_failure); } @@ -2581,14 +2277,14 @@ test "terminal state keeps parser fragments and sends emulator replies through i if (effect == .write) return error.PrematureTerminalReply; } p.update(.{ .output = .{ .pane = 0, .bytes = "6n" } }); - var replies: usize = 0; + var answers: usize = 0; while (p.nextEffect()) |effect| { if (effect != .write) continue; try std.testing.expectEqual(@as(u8, 0), effect.write.pane); try std.testing.expectEqualStrings("\x1b[1;1R", effect.write.bytes.slice()); - replies += 1; + answers += 1; } - try std.testing.expectEqual(@as(usize, 1), replies); + try std.testing.expectEqual(@as(usize, 1), answers); try std.testing.expectEqual(@as(u16, 0), state.reply_len); } @@ -2598,12 +2294,12 @@ test "a terminal's dump restores the cells it drew, its history and where its vi defer p.deinit(); while (p.nextEffect()) |_| {} const pane = p.panes[0].?; - feedOutput(p, pane, "\x1b[31mred\x1b[0m\r\ncafé\r\n"); + pane.feedOutput(p, "\x1b[31mred\x1b[0m\r\ncafé\r\n"); for (0..30) |i| { var line: [16]u8 = undefined; - feedOutput(p, pane, std.fmt.bufPrint(&line, "line {d}\r\n", .{i}) catch unreachable); + pane.feedOutput(p, std.fmt.bufPrint(&line, "line {d}\r\n", .{i}) catch unreachable); } - scrollGrid(pane, -5); + scroll(pane.terminal.?, -5); const bytes = try pardes.walk.save(State, gpa, pane.terminal.?); defer gpa.free(bytes); var arena: std.heap.ArenaAllocator = .init(gpa); @@ -2627,7 +2323,7 @@ test "a restored command pane comes back finished, its command not run again" { while (p.nextEffect()) |_| {} const cmd = p.freeSlot().?; _ = try panes.newCommand(p, cmd, "/tmp", "make test"); - feedOutput(p, p.panes[cmd].?, "ok\r\n"); + p.panes[cmd].?.feedOutput(p, "ok\r\n"); p.update(.{ .exited = .{ .pane = @intCast(cmd), .status = 2 } }); try pardes.dump.dumpState(p); const restored = try pardes.dump.initFromDump(gpa, .{ .tty_only = true, .cols = 60, .rows = 16 }, p.dump_out.?); @@ -2656,7 +2352,7 @@ test "a restored terminal keeps its history above a marker and gets a shell wher defer p.deinit(); while (p.nextEffect()) |_| {} const pane = p.panes[0].?; - feedOutput(p, pane, "old output\r\n$ ls\r\n"); + pane.feedOutput(p, "old output\r\n$ ls\r\n"); try pane.setOwnedCwd("/tmp"); pane.shell = try gpa.dupe(u8, "bash"); // `Term bash`: it comes back as bash try pardes.dump.dumpState(p); @@ -2684,19 +2380,19 @@ test "a position that cannot follow the output for want of memory is said on the const p = try Pardes.init(failing.allocator(), .{ .tty_only = true, .cols = 80, .rows = 24 }); defer p.deinit(); const pane = p.panes[0].?; - feedOutput(p, pane, "one\r\ntwo\r\n"); + pane.feedOutput(p, "one\r\ntwo\r\n"); // More positions than the emulator's pins have room for: following them // through the output needs memory, and there is none. try std.testing.expect(pane.body.reserveSels()); for (pane.body.sels[0..8], 0..) |*range, i| range.* = .{ .row = @intCast(i), .col = 0, .arow = @intCast(i), .acol = 1 }; pane.body.nsel = 8; failing.fail_index = failing.alloc_index; - feedOutput(p, pane, "three\r\n"); + pane.feedOutput(p, "three\r\n"); failing.fail_index = std.math.maxInt(usize); try std.testing.expect(failing.has_induced_failure); - try std.testing.expectEqualStrings(positions_lost, pane.msg[0..pane.msg_len]); + try std.testing.expectEqualStrings(panes.positions_lost, pane.msg[0..pane.msg_len]); const logged = p.messages.at(p.messages.len -| 1) orelse return error.NothingLogged; - try std.testing.expectEqualStrings(positions_lost, logged.slice()); + try std.testing.expectEqualStrings(panes.positions_lost, logged.slice()); } test "raw terminal output needs no position tracking allocations" { @@ -2705,11 +2401,11 @@ test "raw terminal output needs no position tracking allocations" { defer p.deinit(); const pane = p.panes[0].?; pane.body.mode = .tty; - feedOutput(p, pane, "warm"); + pane.feedOutput(p, "warm"); const allocations = failing.alloc_index; failing.fail_index = allocations; const pins = pane.terminal.?.vt.screens.active.pages.countTrackedPins(); - feedOutput(p, pane, " output"); + pane.feedOutput(p, " output"); try std.testing.expectEqual(allocations, failing.alloc_index); try std.testing.expect(!failing.has_induced_failure); try std.testing.expectEqual(pins, pane.terminal.?.vt.screens.active.pages.countTrackedPins()); -- cgit v1.3