summaryrefslogtreecommitdiff
path: root/src/panes.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/panes.zig')
-rw-r--r--src/panes.zig461
1 files changed, 437 insertions, 24 deletions
diff --git a/src/panes.zig b/src/panes.zig
index 99652b04..237693fa 100644
--- a/src/panes.zig
+++ b/src/panes.zig
@@ -109,7 +109,7 @@ pub const Pane = struct {
pub const Cwd = union(enum) { none, inherited: *Pane, owned: []u8 };
pub const Focus = enum { body, tag };
pub const SessionView = struct { wrap: bool = true, layered: bool = true, body_h: u32 = 0, tagline_h: u32 = 0, rect_h: u16 = 0 };
- terminal: ?*terminal.State = null,
+ terminal: ?*Term.State = null,
gpa: std.mem.Allocator,
serial: u32 = 0,
/// What the control filesystem keeps of this pane -- addr and limit, the
@@ -146,7 +146,12 @@ pub const Pane = struct {
/// A shell has run in it (Pardes.acknowledgeShell): a Term whose first
/// shell fails to start closes, one started again keeps its pane.
had_shell: bool = false,
- pending_command: terminal.PendingCommand = .{},
+ /// What pardes typed for a shell not ready for it yet: queued until it
+ /// is started (`spawn`) and then at its prompt (`input`).
+ pending_command: struct {
+ bytes: []u8 = &.{},
+ wait: enum { none, spawn, input } = .none,
+ } = .{},
/// The last command line pardes typed into this terminal (an exec, a
/// middle click, a pty/run): its first word, which Kill matches, and
/// the shell's command count its start (C mark) reaches.
@@ -171,7 +176,7 @@ pub const Pane = struct {
/// be printing to it, and a reuse would hang it up.
command_pty: bool = false,
/// Its child pages as a terminal's does (`pardes -`, a +Pager), not
- /// through cat: Look's `git show` of a commit (look.lookCommit).
+ /// through cat: Look's `git show` of a commit (hunks.lookedAtCommit).
command_pages: bool = false,
/// The shell a terminal runs when it is not the configured one (`Term
/// fish`), owned.
@@ -292,7 +297,7 @@ pub const Pane = struct {
next_pointer_selection: u64 = 0,
/// terminals only: the typed-text buffer standing in for shell rows
- ovl: ?terminal.EditBuffer = null,
+ ovl: ?Text.EditBuffer = null,
tty_filter: bool = false,
msg: [256]u8 = undefined,
msg_len: u16 = 0,
@@ -331,7 +336,7 @@ pub const Pane = struct {
sized_by: u8 = 0,
/// A terminal shown in a view shorter than it (one that does not size
/// it): the rows its top is passed over by, so its cursor stays in
- /// sight (terminal.panFor); 0 everywhere else.
+ /// sight (Pane.panFor); 0 everywhere else.
pan: u16 = 0,
/// The view whose step a `.name` prompt is (tagline.draftName): only
/// that view's focus leaving the pane drops it.
@@ -555,7 +560,7 @@ 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(terminal.gridOffset(pane) + pane.pan);
+ return pane.surfRow(@as(i32, @intCast(Term.grid(pane.terminal).offset)) + pane.pan);
}
pub fn wrapAt(pane: *Pane, vr: i32) struct { line: i32, at: i32, pad: i32 = 0 } {
@@ -592,8 +597,10 @@ pub const Pane = struct {
}
} else if (pane.addon == null) {
// the vt scrolls in SHELL rows; convert through the edit buffer
- const off = terminal.gridOffset(pane);
- terminal.scrollGrid(pane, pane.gridRow(pane.surfRow(off) + delta) - off);
+ if (tape.viewOnly("scroll a terminal")) return;
+ const state = pane.terminal orelse return;
+ const off: i32 = @intCast(Term.grid(state).offset);
+ Term.scroll(state, pane.gridRow(pane.surfRow(off) + delta) - off);
}
}
@@ -674,13 +681,364 @@ pub const Pane = struct {
pane.body.cur_row = pane.scroll();
pane.body.cur_col = 0;
} else {
- const cur = terminal.gridCursor(pane);
- pane.body.cur_row = pane.surfRow(@as(i32, cur.y) + terminal.gridOffset(pane));
- pane.body.cur_col = @intCast(cur.x);
+ 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_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.
+ 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;
+ if (tape.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 pane.pointerRowCount() > 0;
+ // A replica's emulator is what the authority's deltas make it; what
+ // pardes keeps over it moves as the authority's feed told it.
+ if (tape.screenReplica()) {
+ if (has_positions and pane.followRows(state, bytes)) if (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 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);
+ if (!has_positions) {
+ _ = Term.feed(state, bytes, &.{}, &.{}, &.{});
+ } else if (pane.followRows(state, bytes)) if (paneIdOf(p, pane)) |id| p.setMessage(id, positions_lost);
+ p.shell_rows.markStale(pane);
+ if (pane.body.mode == .tty and at_bottom) pane.followOutput();
+ }
+
+ /// 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 {
+ const on_replica = tape.screenReplica();
+ const Group = struct { overlay: ?*Text.EditBuffer, cursor: *i32, selection: *Text.CharSel };
+ var groups: [Text.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 ([_][]Text.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;
+ }
+ }
+ // A position on a row of an edit copy's text stays on it (its line
+ // of the copy, `edit_row`); one below the copy is tracked on the
+ // shell row it stands for.
+ const Position = struct { target: *i32, overlay: ?*Text.EditBuffer, edit_row: ?i32 = null, grid: i32 = -1 };
+ const position = struct {
+ fn of(target: *i32, overlay: ?*Text.EditBuffer) Position {
+ 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, .overlay = edit, .edit_row = grid - edit.row };
+ if (grid > edit.row) grid = grid -| lines +| edit.rows;
+ }
+ return .{ .target = target, .overlay = overlay, .grid = grid };
+ }
+ }.of;
+ var spans: [groups.len]*Text.EditBuffer = undefined;
+ var nspans: usize = 0;
+ var positions: [groups.len * 2 + Text.max_selections * 2 + 4]Position = undefined;
+ var npositions: usize = 0;
+ for (groups[0..ngroups]) |group| {
+ if (group.overlay) |overlay| {
+ spans[nspans] = overlay;
+ nspans += 1;
+ }
+ positions[npositions] = position(group.cursor, group.overlay);
+ npositions += 1;
+ if (group.selection.active) {
+ positions[npositions] = position(&group.selection.row, group.overlay);
+ 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: [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] = position(target, overlay);
+ npositions += 1;
+ }
+ var failed = false;
+ const pointer_positions = pane.gpa.alloc(Position, pane.pointerRowCount()) catch null;
+ defer if (pointer_positions) |held| pane.gpa.free(held);
+ if (pointer_positions) |held| {
+ var index: usize = 0;
+ for (pane.sel) |gesture| if (pane.pointerSelection(gesture)) |rows| {
+ for (rows) |*row| {
+ held[index] = position(&row.row, if (row.raw_terminal) null else overlay);
+ index += 1;
+ }
+ };
+ } else failed = true;
+ const pointers: []Position = pointer_positions orelse &.{};
+ // The rows to track, in the order told: each copy's first and last
+ // row, then the positions, then the pointer's rows. On the stack,
+ // as the local path never allocated here but for the pointer's rows.
+ const n = 2 * nspans + npositions + pointers.len;
+ var track_buf: [512]i32 = undefined;
+ 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);
+ 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);
+ 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);
+ 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| {
+ track[k] = row;
+ k += 1;
+ };
+ for ([_][]Position{ positions[0..npositions], pointers }) |batch| for (batch) |at| {
+ track[k] = at.grid;
+ k += 1;
+ };
+ // A replica's emulator is fed by its authority's deltas: it is told
+ // what the authority's feed told.
+ const fed: Term.Told = if (on_replica) told: {
+ @memset(rows_after, 0);
+ @memset(erased_after, false);
+ break :told .{ .rows = rows_after, .erased = erased_after, .cleared = false, .alive = false, .failed = false };
+ } else Term.feed(state, bytes, track, rows_after, erased_after);
+ 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;
+ 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, the emulator 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 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.row or rows != span.rows;
+ span.row = start;
+ span.rows = rows;
+ }
+ // The rule above for the copy holds for the selections: one whose
+ // row is gone (ED 3 erased its history row) 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: [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 ([_][]Position{ positions[0..npositions], pointers }, [_]usize{ at_position, at_pointer }) |batch, first| for (batch, first..) |at, i| {
+ var row = after.rows[i];
+ if (at.overlay) |edit| {
+ if (at.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 != at.target.*;
+ at.target.* = row;
+ };
+ if (pointer_rows_lost) for (0..pane.sel.len) |button| {
+ pane.clearPointerSelection(button);
+ pane.sel[button].state = .none;
+ };
+ 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;
+ }
+
+ fn keepTold(t: Term.Told) Term.Told {
+ return t;
+ }
+
+ fn feedLost(state: *Term.State, bytes: []const u8) bool {
+ if (!tape.screenReplica()) _ = Term.feed(state, bytes, &.{}, &.{}, &.{});
+ return true;
+ }
+
+ fn pointerRowCount(pane: *const Pane) usize {
+ var count: usize = 0;
+ for (pane.sel) |gesture| if (pane.pointerSelection(gesture)) |rows| {
+ count += rows.len;
+ };
+ return count;
+ }
+
+ /// Its terminal's view snapped back onto live output.
+ pub fn followOutput(pane: *Pane) void {
+ if (tape.viewOnly("follow a terminal")) return;
+ const state = pane.terminal orelse return;
+ Term.follow(state);
+ }
+
+ /// Its terminal's grid reflowed to `cols` by `rows`: on a screen-state
+ /// replica by its authority (the next delta says), on such an authority
+ /// once the step's unit is out (`resizeDeferred`).
+ pub fn resizeGrid(pane: *Pane, cols: u16, rows: u16) void {
+ if (tape.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;
+ };
+ if (tape.screenReplica()) return;
+ // 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): the authority's waits for the unit.
+ Term.resize(state, cols, rows, tape.screenAuthority());
+ }
+
+ /// The rows its terminal'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(pane: *const Pane, p: *const Pardes, 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 grid = Term.grid(state);
+ // The cursor's row in the viewport; 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);
+ return @intCast(@min(pane.rows - shown, (at + 1) -| shown));
+ }
+
+ /// Whether a new terminal's greeting may be typed now: only at the
+ /// shell's first prompt (pardes's own marks: no command started; a
+ /// prompt drawn again for a resize is the same first prompt), with
+ /// nothing typed at it and no program on the alternate screen. Any
+ /// later moment is `.never`, for good: something has run or been typed
+ /// first, and an `ls` then would go into it. A shell pardes cannot mark
+ /// has no first prompt to find, and is never greeted.
+ pub fn greeting(pane: *const Pane) enum { wait, now, never } {
+ if (pane.fs.unmarked) return .never;
+ // Text typed into the edit buffer that stands in for the prompt row.
+ if (pane.ovl) |o| if (o.text.len > 0) return .never;
+ const state = pane.terminal orelse return .wait;
+ const at = Term.prompt(state);
+ // (Not `declined`: bash's first prompt comes after a D of its own.)
+ if (at.started > 0) return .never;
+ // At a prompt's input (the emulator's semantic marks, pardes's or
+ // the shell's own), before any command pardes's marks saw run.
+ if (!at.input_ready) return .wait;
+ return if (at.input_empty) .now else .never;
+ }
+
+ /// Remembers a line pardes types into this terminal as the command it
+ /// started, for Kill: it runs from the shell's next C mark to its D.
+ /// ponytail: of several lines queued for a shell not up yet, only the
+ /// last is remembered, as the first of them.
+ pub fn noteCommand(pane: *Pane, line: []const u8) void {
+ const state = pane.terminal orelse return;
+ const said = std.mem.trim(u8, line, " \t");
+ const word = said[0 .. std.mem.indexOfAny(u8, said, " \t") orelse said.len];
+ var sent: @typeInfo(@FieldType(Pane, "sent_command")).optional.child = .{ .seq = Term.prompt(state).started +% 1 };
+ sent.len = @intCast(@min(word.len, sent.word.len));
+ @memcpy(sent.word[0..sent.len], word[0..sent.len]);
+ pane.sent_command = sent;
+ }
+
+ /// A line for a shell not ready for it (`pending_command`), queued
+ /// after the ones before it; false when the shell is ready now.
+ pub fn queuePendingCommand(pane: *Pane, command: []const u8) !bool {
+ if (pane.pending_command.wait == .none) return false;
+ const old_len = pane.pending_command.bytes.len;
+ const new_len = try std.math.add(usize, old_len, try std.math.add(usize, command.len, 1));
+ const bytes = if (old_len == 0)
+ try pane.gpa.alloc(u8, new_len)
+ else
+ try pane.gpa.realloc(pane.pending_command.bytes, new_len);
+ @memcpy(bytes[old_len..][0..command.len], command);
+ bytes[new_len - 1] = '\r';
+ pane.pending_command.bytes = bytes;
+ pane.greet = false;
+ return true;
+ }
+
+ /// The lines queued for its shell, written now that it is `ready`.
+ pub fn releasePendingCommand(pane: *Pane, p: *Pardes, id: usize, ready: bool) void {
+ if (!ready) return;
+ const bytes = pane.pending_command.bytes;
+ pane.pending_command = .{};
+ if (bytes.len > 0) {
+ p.emitWrite(id, bytes);
+ pane.gpa.free(bytes);
+ }
+ }
+
/// `run <serial> <word>` or `exit <serial> <N|?>` in the log.
/// The whole line, one record as `msg` carries its text: its control
/// characters, a newline too, become spaces (events.pushLog). A new pane is
@@ -708,8 +1066,8 @@ pub const Pane = struct {
var buf: [16]u8 = undefined;
const code = if (status) |n| std.fmt.bufPrint(&buf, "{d}", .{n}) catch unreachable else "?"; // an exit status fits 16
var said: pardes.Kind.Buf(32) = .{};
- const lead = if (panes.terminal.gridCursor(pane).x != 0) "\r\n" else "";
- panes.terminal.feedOutput(p, pane, said.print("{s}exit {s}\r\n", .{ lead, code }));
+ const lead = if (Term.grid(pane.terminal).cursor_x != 0) "\r\n" else "";
+ pane.feedOutput(p, said.print("{s}exit {s}\r\n", .{ lead, code }));
pane.noteRun(p, "exit", code);
p.needs_frame = true;
}
@@ -730,7 +1088,53 @@ pub const Dir = @import("Dir.zig");
pub const mini = @import("mini.zig");
-pub const terminal = @import("terminal.zig");
+pub const Term = @import("Term.zig");
+
+/// 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.
+pub const positions_lost = "terminal edit position reset: out of memory";
+
+/// The sizes steps gave terminals (`Pane.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 (Term.resizeDeferred(state))
+ p.shell_rows.markStale(pane);
+}
+
+/// A pane with nothing in it yet, `cols` by `rows`: what a file, a
+/// directory, a kind's pane or a terminal is made from.
+pub fn createDoc(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane {
+ const pane = try gpa.create(Pane);
+ pane.* = .{
+ .gpa = gpa,
+ .cols = cols,
+ .rows = rows,
+ };
+ return pane;
+}
+
+/// A terminal pane: a pane and its emulator. Slot ownership, serial
+/// assignment and spawn effects remain core lifecycle invariants.
+pub fn createTerminal(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane {
+ if (tape.authorityOnly("open a terminal")) return error.OutOfMemory;
+ const pane = try createDoc(gpa, cols, rows);
+ errdefer gpa.destroy(pane);
+ pane.terminal = try Term.open(emulatorGpa(gpa), cols, rows);
+ pane.tty_filter = true;
+ return pane;
+}
+
+/// A screen-state replica's emulators' allocator (`Term.State.gpa`): the C
+/// heap, outside its rollback epoch: written by deltas while a copy is
+/// held, no part of it is ever a copy's to save or put back.
+pub const emulator_gpa = std.heap.c_allocator;
+
+/// What a terminal's emulator is made with, by `createTerminal` or a pane's
+/// load (dump.loadPane, a closed terminal brought back): `gpa`, but on a
+/// screen-state replica `emulator_gpa`.
+pub fn emulatorGpa(gpa: std.mem.Allocator) std.mem.Allocator {
+ return if (tape.screenReplica()) emulator_gpa else gpa;
+}
// ---- the session's pane slots: making, finding, closing and freeing panes, and their prompts ----
@@ -792,7 +1196,8 @@ pub fn teardownPane(p: *Pardes, pane: *Pane) void {
}
p.shell_rows.dropPane(pane);
for (0..pane.pointer_selections.len) |slot| pane.clearPointerSelection(slot);
- panes.terminal.deinitPendingCommand(pane);
+ if (pane.pending_command.bytes.len > 0) pane.gpa.free(pane.pending_command.bytes);
+ pane.pending_command = .{};
if (pane.command) |line| p.gpa.free(line);
if (pane.shell) |bin| p.gpa.free(bin);
@import("Kind.zig").close(p, pane);
@@ -813,7 +1218,12 @@ pub fn teardownPane(p: *Pardes, pane: *Pane) void {
p.gpa.destroy(q);
pane.query = null;
}
- panes.terminal.deinitEmulator(pane, p.gpa);
+ // A pane with none (a file's, a picker's, a preview's) closes in a
+ // speculation as anywhere.
+ if (pane.terminal) |state| if (!tape.authorityOnly("close a terminal")) {
+ Term.close(state, p.gpa);
+ pane.terminal = null;
+ };
pane.clearCwd();
pane.fs.deinit(p.gpa);
p.gpa.destroy(pane);
@@ -846,12 +1256,13 @@ pub fn installPane(p: *Pardes, id: usize, pane: *Pane) void {
pub fn newShell(p: *Pardes, id: usize, cwd: []const u8) !*Pane {
std.debug.assert(p.panes[id] == null);
if (cwd.len > limits.host_path_cap) return error.PathTooLong;
- const pane = try panes.terminal.create(p.gpa, p.view().screen_w, p.view().screen_h);
+ const pane = try panes.createTerminal(p.gpa, p.view().screen_w, p.view().screen_h);
// Named by the directory it is started in from the first, so the
// shell saying it is there is no rename (`new N /` then `rename`);
// out of memory for it, the shell names it when it says.
if (cwd.len > 0) pane.setOwnedCwd(cwd) catch {};
- panes.terminal.armShellSpawn(pane);
+ std.debug.assert(pane.pending_command.bytes.len == 0);
+ pane.pending_command.wait = .spawn;
p.installPane(id, pane);
p.emitSpawn(id, pane.serial, cwd);
return pane;
@@ -877,7 +1288,7 @@ pub fn newCommand(p: *Pardes, id: usize, cwd: []const u8, line: []const u8) !*Pa
if (cwd.len > limits.host_path_cap) return error.PathTooLong;
const owned = try p.gpa.dupe(u8, line);
errdefer p.gpa.free(owned);
- const pane = try panes.terminal.create(p.gpa, p.view().screen_w, p.view().screen_h);
+ const pane = try panes.createTerminal(p.gpa, p.view().screen_w, p.view().screen_h);
pane.command = owned;
pane.command_pty = true;
pane.body.mode = .tty;
@@ -891,7 +1302,7 @@ pub fn newCommand(p: *Pardes, id: usize, cwd: []const u8, line: []const u8) !*Pa
pub fn newDocPane(p: *Pardes, id: usize) !*Pane {
std.debug.assert(p.panes[id] == null);
- const pane = try panes.terminal.createDoc(p.gpa, p.view().screen_w, p.view().screen_h);
+ const pane = try panes.createDoc(p.gpa, p.view().screen_w, p.view().screen_h);
p.installPane(id, pane);
return pane;
}
@@ -942,7 +1353,7 @@ pub fn paneBySerial(p: *Pardes, serial: u32) ?usize {
/// The pane a session word runs with in an empty window.
pub fn standIn(p: *Pardes) ?*Pane {
- if (p.stand_in == null) p.stand_in = panes.terminal.createDoc(p.gpa, p.view().screen_w, p.view().screen_h) catch null;
+ if (p.stand_in == null) p.stand_in = panes.createDoc(p.gpa, p.view().screen_w, p.view().screen_h) catch null;
return p.stand_in;
}
@@ -973,7 +1384,7 @@ pub fn setTestFile(p: *Pardes, content: []const u8) !*Pane {
errdefer p.gpa.free(path);
const history = try panes.File.History.create(p.gpa);
errdefer p.gpa.destroy(history);
- const pane = try panes.terminal.createDoc(p.gpa, v.screen_w, v.screen_h);
+ const pane = try panes.createDoc(p.gpa, v.screen_w, v.screen_h);
errdefer p.gpa.destroy(pane);
try deinitPane(p, p.panes[0].?);
p.panes[0] = null;
@@ -1145,10 +1556,10 @@ test {
_ = Dir;
_ = mini;
_ = @import("Kind.zig");
- _ = terminal;
}
test "owned cwd preserves long paths aliases and failed updates" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
var allocator = std.testing.FailingAllocator.init(std.testing.allocator, .{});
const p = try Pardes.init(allocator.allocator(), .{ .tty_only = true });
defer p.deinit();
@@ -1246,6 +1657,7 @@ test "owned cwd close copies are transactional across inherited chains" {
}
test "owned cwd column close allocates every copy before removing any pane" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // the classic boot's three shells
var allocator = std.testing.FailingAllocator.init(std.testing.allocator, .{});
const p = try Pardes.init(allocator.allocator(), .{ .shells = 3, .cols = 100, .rows = 30 });
defer p.deinit();
@@ -1299,7 +1711,7 @@ test "owned cwd EOF failure retains the pane and Del retries its close" {
}
test "owned cwd restore either copies the directory or preserves the old core" {
- // No terminal: ghostty 1.4's snapshot decode leaks when an allocation
+ // No terminal: the emulator's (1.4) snapshot decode leaks when an allocation
// inside it fails, and this fails every allocation of a Restore in turn
// (what pardes's own load made is freed, src/walk.zig rollback).
var allocator = std.testing.FailingAllocator.init(std.testing.allocator, .{});
@@ -1365,6 +1777,7 @@ test "the Tutor pane is named +Tutor in the session's directory, never a bare pa
}
test "New opens an empty scratch below the caller, inheriting its directory" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // the classic boot's three shells
const p = try Pardes.init(std.testing.allocator, .{ .shells = 3, .cols = 100, .rows = 30 });
defer p.deinit();
while (p.nextEffect()) |_| {}