diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 00:06:42 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | c742d19d4bacbd9a91627c4859ca8cb0d11bdba7 (patch) | |
| tree | 810036d05946b51dfadef508ee75f677303ba7c8 /src | |
| parent | e4fbb97a506ca2a4b469b6e2e13ecba6e516272c (diff) | |
| download | pardes-c742d19d4bacbd9a91627c4859ca8cb0d11bdba7.tar.gz pardes-c742d19d4bacbd9a91627c4859ca8cb0d11bdba7.zip | |
Term: the terminal as the core calls it, src/Term.zig, its lifecycle first (docs/next-terminal.md, Phase 0): Term.open makes an emulator and Term.close frees it, Term.due says when its next kitty frame is due, Term.revive resets a restored terminal's modes under its banner, Term.start and Term.stop run the PNG resize workers; the pane half of each is the core's: panes.createDoc and panes.createTerminal make the pane (the speculation guard there), emulatorGpa and emulator_gpa are the core's choice of allocator, teardownPane closes the emulator, dump.zig's revivePane resets what the old shell was and ensureKind opens an emulator straight into the pane; tape.authorityOnly is the one guard Kind.zig and the terminal both had a copy of; nothing else changes
Diffstat (limited to 'src')
| -rw-r--r-- | src/Core.zig | 2 | ||||
| -rw-r--r-- | src/File.zig | 2 | ||||
| -rw-r--r-- | src/Kind.zig | 18 | ||||
| -rw-r--r-- | src/Term.zig | 31 | ||||
| -rw-r--r-- | src/dump.zig | 21 | ||||
| -rw-r--r-- | src/gui/gui.zig | 4 | ||||
| -rw-r--r-- | src/host_io.zig | 4 | ||||
| -rw-r--r-- | src/macos.zig | 6 | ||||
| -rw-r--r-- | src/panes.zig | 56 | ||||
| -rw-r--r-- | src/pardes.zig | 4 | ||||
| -rw-r--r-- | src/replica.zig | 6 | ||||
| -rw-r--r-- | src/tape.zig | 10 | ||||
| -rw-r--r-- | src/term/terminal.zig | 110 | ||||
| -rw-r--r-- | src/tui/tui.zig | 4 |
14 files changed, 157 insertions, 121 deletions
diff --git a/src/Core.zig b/src/Core.zig index 40dabda3..1f3e9283 100644 --- a/src/Core.zig +++ b/src/Core.zig @@ -1988,7 +1988,7 @@ pub fn nextWake(p: *const Pardes) ?u64 { // A terminal's kitty animation, at its next frame. Only a time still // ahead counts: a pane not drawn keeps the last one it was given, // and a past one would wake every frame for nothing. - for (p.panes) |slot| if (slot) |pane| if (pardes.panes.terminal.animationDueMs(pane)) |due_ms| { + for (p.panes) |slot| if (slot) |pane| if (pane.terminal) |state| if (pardes.Term.due(state)) |due_ms| { const due = due_ms *| std.time.ns_per_ms; if (due > p.clock.stepped_ns) { const wait = @max(1, (due - p.clock.stepped_ns + frame_ns - 1) / frame_ns); diff --git a/src/File.zig b/src/File.zig index 5150f1cd..ffac06fd 100644 --- a/src/File.zig +++ b/src/File.zig @@ -598,7 +598,7 @@ pub fn open(p: *Pardes, id: usize, path: []const u8, line: usize) !*Pane { std.debug.assert(p.panes[id] == null and !p.reserved_slots[id]); const path_copy = try p.gpa.dupe(u8, path); errdefer p.gpa.free(path_copy); - const pane = try 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); errdefer p.gpa.destroy(pane); const history = try History.create(p.gpa); errdefer p.gpa.destroy(history); diff --git a/src/Kind.zig b/src/Kind.zig index 54c7e0e7..055b17f9 100644 --- a/src/Kind.zig +++ b/src/Kind.zig @@ -275,7 +275,7 @@ pub fn openFile(p: *Pardes, id: usize, k: u8, path: []const u8) !*Pane { /// if the kind refused, in `slot.refused`. One call for both cores. pub fn reopen(p: ?*Pardes, id: usize, s: *Slot, bytes: ?[]const u8) bool { // A replica has no kind's code: what the open says is the authority's. - if (authorityOnly("a kind's open")) return false; + if (tape.authorityOnly("a kind's open")) return false; var cl: Call = .{ .p = p, .id = id, .slot = s }; const prev = cl.begin("open"); if (p) |core| if (core.kinds.byName(s.nameOf())) |k| { @@ -530,7 +530,7 @@ fn prefixOf(p: *const Pardes, slot: *const Slot, buf: *[400]u8) []const u8 { pub fn key(p: *Pardes, id: usize, k: pardes.Key) bool { const slot = (p.panes[id] orelse return false).addon orelse return false; if (!caps(p, slot).key) return false; - if (authorityOnly("a key into a kind's pane")) return true; + if (tape.authorityOnly("a key into a kind's pane")) return true; const pk: c.pa_key = .{ .cp = k.cp, .ctrl = @intFromBool(k.ctrl), .alt = @intFromBool(k.alt), .shift = @intFromBool(k.shift), .view = p.live + 1 }; var cl: Call = .{ .p = p, .id = id, .slot = slot }; const prev = cl.begin("key"); @@ -554,7 +554,7 @@ pub fn takesKeys(p: *const Pardes, s: *const Slot) bool { pub fn search(p: *Pardes, id: usize, pat: []const u8) ?[]const u8 { const slot = (p.panes[id] orelse return null).addon orelse return null; if (!caps(p, slot).search) return null; - if (authorityOnly("a kind's search")) return null; + if (tape.authorityOnly("a kind's search")) return null; var cl: Call = .{ .p = p, .id = id, .slot = slot }; const prev = cl.begin("search"); if (slot.ops) |o| o.search.?(slot.st, pat.ptr, @intCast(pat.len)); @@ -570,7 +570,7 @@ pub fn address(p: *Pardes, id: usize, text: []const u8, said: *Buf(160)) bool { const slot = (p.panes[id] orelse return false).addon orelse return false; // By its caps, not its code: a Shadow calls as the authority does. if (!caps(p, slot).address) return false; - if (authorityOnly("a kind's address")) return true; + if (tape.authorityOnly("a kind's address")) return true; var cl: Call = .{ .p = p, .id = id, .slot = slot, .missing = said }; const prev = cl.begin("address"); if (slot.ops) |o| cl.ret = o.address.?(slot.st, text.ptr, @intCast(text.len)); @@ -587,7 +587,7 @@ pub fn takesMouse(p: *const Pardes, s: *const Slot) bool { pub fn mouse(p: *Pardes, id: usize, m: pardes.Mouse, col: u16, row: u16) bool { const slot = (p.panes[id] orelse return false).addon orelse return false; if (!caps(p, slot).mouse) return false; - if (authorityOnly("the mouse in a kind's pane")) return true; + if (tape.authorityOnly("the mouse in a kind's pane")) return true; const pm: c.pa_mouse = .{ .button = switch (m.button) { .left => c.PA_LEFT, @@ -815,7 +815,7 @@ pub fn ctl(p: *Pardes, id: usize, line: []const u8, reply: *[160]u8) ?[]const u8 const slot = (p.panes[id] orelse return "no such pane").addon orelse return "not a kind's pane"; if (!slot.live) return "its addon is not loaded"; if (!caps(p, slot).ctl) return "its kind takes no words"; - if (authorityOnly("a kind's word")) return null; + if (tape.authorityOnly("a kind's word")) return null; var cl: Call = .{ .p = p, .id = id, .slot = slot }; const prev = cl.begin("ctl"); @memset(reply, 0); @@ -841,12 +841,6 @@ pub fn ctlRead(pane: *const Pane, buf: []u8) []const u8 { return buf[0..@min(buf.len, f(slot.st, buf.ptr, @intCast(buf.len)))]; } -fn authorityOnly(comptime why: []const u8) bool { - if (!tape.speculating()) return false; - tape.poison(why); - return true; -} - // ---- a call into a kind ---- /// What a call into a kind did that the core takes on: its answer, a diff --git a/src/Term.zig b/src/Term.zig new file mode 100644 index 00000000..36315df4 --- /dev/null +++ b/src/Term.zig @@ -0,0 +1,31 @@ +//! The terminal as the core sees it (docs/next-terminal.md, Phase 0): the +//! entries the core calls and the plain records they take and give. A +//! terminal pane holds a `*State` the core never looks into: what it knows +//! of one it is told here, as numbers, bytes and text. The emulator +//! (ghostty-vt) and what it brings stay behind this file, in src/term/; +//! this is the one file outside it that names a file inside it (and so +//! the one place Phase 1 turns into the module's registration). +const std = @import("std"); +const terminal = @import("term/terminal.zig"); + +/// A terminal pane's emulator, its parse and what pardes keeps beside it. +pub const State = terminal.State; + +// ---- lifecycle ---- + +/// A new emulator, `cols` by `rows`, allocated with `gpa` (its own from +/// then on). +pub const open = terminal.open; +/// Its emulator and what it holds, freed; the kitty images' caches were +/// `gpa`'s. +pub const close = terminal.close; +/// When its next kitty animation frame is due (ms on the core's clock), as +/// its last drawn frame left it; null when nothing plays. +pub const due = terminal.due; +/// Loaded from a dump without its shell: its program's modes reset, its +/// history marked off from what the next shell prints. +pub const revive = terminal.revive; +/// The PNG resize workers kitty images are decoded with, started and +/// stopped with the host. +pub const start = terminal.TerminalImages.start; +pub const stop = terminal.TerminalImages.stop; diff --git a/src/dump.zig b/src/dump.zig index a880c7e1..19ba25ce 100644 --- a/src/dump.zig +++ b/src/dump.zig @@ -490,10 +490,8 @@ fn loadImage(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, notes: ?* /// build's dump could not give): a terminal, a new emulator for its shell. fn ensureKind(p: *Pardes, pane: *pardes.panes.Pane) !void { if (pane.terminal != null or pane.file != null or pane.addon != null or pane.pdf != null) return; - const fresh = try pardes.panes.terminal.create(p.gpa, @max(1, pane.cols), @max(1, pane.rows)); - pane.terminal = fresh.terminal; - fresh.terminal = null; - p.gpa.destroy(fresh); + if (pardes.tape.authorityOnly("open a terminal")) return error.OutOfMemory; + pane.terminal = try pardes.Term.open(pardes.panes.emulatorGpa(p.gpa), @max(1, pane.cols), @max(1, pane.rows)); } /// The panes a column lists that are not there (another build's dump left @@ -660,7 +658,18 @@ fn revivePane(p: *Pardes, id: usize, pane: *pardes.panes.Pane, was_on_disk: bool } if (comptime isolated) return; if (pane.cwdSlice().len > effect_path_cap) return; - pardes.panes.terminal.reviveShell(pane); + // Its shell not there, made ready for a new one: what the old + // shell was, ran and said is gone with it (the REPL it ran, the + // Term+ word naming it), and the pane waits for the new one like + // any new terminal (a line queued for the old one goes to it). + pardes.Term.revive(pane.terminal.?); + pane.shell_spoke = false; + pane.had_shell = false; + pane.shell_failed = false; + pane.shell_tail_len = 0; + pane.repl = null; + pane.sent_command = null; + pane.pending_command.wait = .spawn; p.emitSpawn(id, pane.serial, pane.cwdSlice()); return; } @@ -719,7 +728,7 @@ pub fn savePane(gpa: std.mem.Allocator, pane: *pardes.panes.Pane, slim: bool) ![ pub fn loadPane(p: *Pardes, id: usize, bytes: []const u8) !*pardes.panes.Pane { var l = try walk.Load(&.{pardes.panes.Pane}).init(p.gpa, bytes); defer l.deinit(); - l.allocs = .{ .image = p.image_gpa, .terminal = pardes.panes.terminal.emulatorGpa(p.gpa) }; + l.allocs = .{ .image = p.image_gpa, .terminal = pardes.panes.emulatorGpa(p.gpa) }; const pane = try p.gpa.create(pardes.panes.Pane); errdefer { l.rollback(); diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 202f4aa8..dbae12b5 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -1658,11 +1658,11 @@ fn remoteSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_ const have: ?*replica.Have = if (far) &have_store else null; const allocs = pardes.memory.init(gpa); defer pardes.memory.deinit(); - pardes.TerminalImages.start(allocs.image); + pardes.Term.start(allocs.image); pardes.Kind.heap = allocs.addon; pardes.syntax.start(allocs.tree_sitter); defer { - pardes.TerminalImages.stop(); + pardes.Term.stop(); pardes.syntax.stop(); } var o = opts_in; diff --git a/src/host_io.zig b/src/host_io.zig index 3141ff64..0e1d29fd 100644 --- a/src/host_io.zig +++ b/src/host_io.zig @@ -173,13 +173,13 @@ pub fn testClock() ?u64 { /// heap, `pa_api.heap`: the pdf addon's documents and pages). `stopWorkers` /// is its defer. pub fn startWorkers(allocs: pardes.memory.Allocators) void { - pardes.TerminalImages.start(allocs.image); + pardes.Term.start(allocs.image); pardes.Kind.heap = allocs.addon; pardes.syntax.start(allocs.tree_sitter); } pub fn stopWorkers() void { - pardes.TerminalImages.stop(); + pardes.Term.stop(); pardes.syntax.stop(); } diff --git a/src/macos.zig b/src/macos.zig index 254cf2a7..3dc87e1a 100644 --- a/src/macos.zig +++ b/src/macos.zig @@ -340,11 +340,11 @@ fn initCore(runtime: ?*const Runtime, cols_arg: u16, rows_arg: u16) !void { if (found.dir) |d| crash.setDir(d); } - pardes.TerminalImages.start(allocs.image); + pardes.Term.start(allocs.image); pardes.Kind.heap = allocs.addon; // Its addons are compiled in (no loader runs here): image and PDF panes. opts.addons = pardes.Addon.scan(); - errdefer pardes.TerminalImages.stop(); + errdefer pardes.Term.stop(); pardes.syntax.start(allocs.tree_sitter); errdefer pardes.syntax.stop(); @@ -432,7 +432,7 @@ export fn pardes_deinit() void { for (st.tag_cells) |cells| st.gpa.free(cells); if (st.images.len > 0) st.gpa.free(st.images); st.core.deinit(); - pardes.TerminalImages.stop(); + pardes.Term.stop(); pardes.syntax.stop(); st.config_arena.deinit(); st.prompt_rcs.deinit(); diff --git a/src/panes.zig b/src/panes.zig index b03cd7cd..e1eb7e0e 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 @@ -732,6 +732,43 @@ pub const mini = @import("mini.zig"); pub const terminal = @import("term/terminal.zig"); +pub const Term = @import("Term.zig"); + +/// 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 ---- const tutor_text = @embedFile("tutor.txt"); @@ -813,7 +850,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,7 +888,7 @@ 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. @@ -877,7 +919,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 +933,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 +984,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 +1015,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; diff --git a/src/pardes.zig b/src/pardes.zig index 95adf54d..4628c07e 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -78,8 +78,8 @@ pub const rollback = @import("rollback.zig"); pub const replica = @import("replica.zig"); pub const echo = @import("term/echo.zig"); pub const Kind = @import("Kind.zig"); -/// A terminal's kitty pictures; its PNG resize is started with the shell. -pub const TerminalImages = panes.terminal.TerminalImages; +/// The terminal, as the core calls it (src/term/ behind it). +pub const Term = @import("Term.zig"); pub const Addon = @import("Addon.zig"); pub const dump = @import("dump.zig"); pub const lsp = @import("lsp/lsp.zig"); diff --git a/src/replica.zig b/src/replica.zig index c5ec4aec..635ada75 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -739,7 +739,7 @@ pub const Replica = struct { o.state = .off; // Its emulators off the epoch, as `terminal.create` makes a screen // replica's: deltas write them while a copy is held. - pardes.dump.terminal_gpa = terminal.emulator_gpa; + pardes.dump.terminal_gpa = pardes.panes.emulator_gpa; defer pardes.dump.terminal_gpa = null; const c = try pardes.dump.load(epoch, o, bytes, null); // The chrome's colours are local, never dumped: the restored theme's. @@ -760,7 +760,7 @@ pub const Replica = struct { } pub fn deinit(r: *Replica) void { - for (&r.screens) |*slot| if (slot.*) |*sc| sc.rep.deinit(terminal.emulator_gpa); + for (&r.screens) |*slot| if (slot.*) |*sc| sc.rep.deinit(pardes.panes.emulator_gpa); if (r.t.core) |c| { const was = Stepping.begin(&r.t, &r.epoch); if (r.copy.held) _ = r.copy.restore(); @@ -1158,7 +1158,7 @@ pub const Replica = struct { const state = pane.terminal orelse return null; const slot = &r.screens[m.pane]; if (slot.* == null or slot.*.?.serial != m.serial) { - if (slot.*) |*old| old.rep.deinit(terminal.emulator_gpa); + if (slot.*) |*old| old.rep.deinit(pardes.panes.emulator_gpa); slot.* = .{ .serial = m.serial }; } const sc = &slot.*.?; diff --git a/src/tape.zig b/src/tape.zig index 68413315..608ad6ff 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -1062,6 +1062,14 @@ pub fn poison(why: []const u8) void { }; } +/// A replica's speculation leaves what is the authority's be (a terminal's +/// emulator, a kind's state): true, and the input waits for its order. +pub fn authorityOnly(comptime why: []const u8) bool { + if (!speculating()) return false; + poison(why); + return true; +} + /// A speculation read a terminal's rows or moved its view (`Tape.touched`). pub fn touchTerminal() void { if (on) |t| if (t.speculating) { @@ -1247,7 +1255,7 @@ pub fn startFromEnv() void { pub fn replayMain(path: []const u8) u8 { const gpa = std.heap.c_allocator; // What a shell starts before its core (tui.zig's localSession). - pardes.TerminalImages.start(gpa); + pardes.Term.start(gpa); pardes.syntax.start(gpa); const bytes = std.Io.Dir.cwd().readFileAlloc(std.Io.Threaded.global_single_threaded.io(), path, gpa, .unlimited) catch |err| { std.debug.print("replay: cannot read {s}: {t}\n", .{ path, err }); diff --git a/src/term/terminal.zig b/src/term/terminal.zig index bc5e7fd6..9472cfe1 100644 --- a/src/term/terminal.zig +++ b/src/term/terminal.zig @@ -594,23 +594,7 @@ pub fn takeMarks(state: *State, bytes: []const u8) void { m.finished = if (seq == std.math.maxInt(u32)) null else .{ .seq = seq, .status = if (status == std.math.minInt(i32)) null else status }; } -/// A screen-state replica's emulators' allocator (`State.gpa`). -pub const emulator_gpa = std.heap.c_allocator; - -/// What a terminal's emulator is made with, by `create` 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 (pardes.tape.screenReplica()) emulator_gpa else gpa; -} - -/// A replica's speculation leaves an emulator be: it is the authority's -/// (docs/next-daemon.md, phase 5), so the input waits for its order. -fn authorityOnly(comptime why: []const u8) bool { - if (!pardes.tape.speculating()) return false; - pardes.tape.poison(why); - return true; -} +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 @@ -863,21 +847,17 @@ pub fn screenTextLen(pane: *Pane) usize { return @intCast(counting.fullCount()); } -/// Release the emulator's heap. The Pane allocation itself is the core's. -pub fn deinitEmulator(pane: *Pane, gpa: std.mem.Allocator) void { - // A pane with none (a file's, a picker's, a preview's) closes in a - // speculation as anywhere. - const state = pane.terminal orelse return; - if (authorityOnly("close a terminal")) return; +/// Its emulator and what it holds, freed; its kitty images' caches were +/// `gpa`'s. +pub fn close(state: *State, gpa: std.mem.Allocator) void { if (comptime images_enabled) state.images.deinit(gpa); state.stream.deinit(); const own = state.gpa; state.vt.deinit(own); own.destroy(state); - pane.terminal = null; } -/// A terminal pane's emulator, `cols` by `rows`, as `create` makes one. +/// A terminal pane's emulator, `cols` by `rows`, as `open` makes one. fn emulator(gpa: std.mem.Allocator, cols: u16, rows: u16) !ghostty_vt.Terminal { return ghostty_vt.Terminal.init(terminalIo(), gpa, .{ .cols = cols, @@ -894,65 +874,38 @@ fn emulator(gpa: std.mem.Allocator, cols: u16, rows: u16) !ghostty_vt.Terminal { }); } -/// Allocate the live emulator half of a terminal pane. Slot ownership, serial -/// assignment, and spawn effects remain core lifecycle invariants. -pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { - if (authorityOnly("open a terminal")) return error.OutOfMemory; - const pane = try createDoc(gpa, cols, rows); - errdefer gpa.destroy(pane); - const own = emulatorGpa(gpa); - const state = try own.create(State); - errdefer own.destroy(state); +/// A new emulator, `cols` by `rows`, allocated with `gpa`, its own from +/// then on (`State.gpa`). +pub fn open(gpa: std.mem.Allocator, cols: u16, rows: u16) !*State { + const state = try gpa.create(State); + errdefer gpa.destroy(state); // Every other field its default, never what the heap had: a digest // reads them, and two replicas must agree (src/replica.zig). - state.* = .{ .gpa = own, .vt = try emulator(own, cols, rows), .stream = undefined }; + state.* = .{ .gpa = gpa, .vt = try emulator(gpa, cols, rows), .stream = undefined }; state.wire(); - pane.terminal = state; - pane.tty_filter = true; - return pane; -} - -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; + return state; } /// Where a restored terminal's history ends and its new shell begins. pub const restored_banner = "\x1b[0m\r\n\x1b[2m\u{2500}\u{2500} restored history \u{2500}\u{2500}\x1b[0m\r\n"; -/// A terminal loaded from a dump, its shell not there, made ready for a new -/// one: its program's screen modes reset (the old program is gone), its -/// history marked off from what the new shell prints, and the pane waiting -/// for it like any new terminal (a line queued for the old one goes to it). -pub fn reviveShell(pane: *Pane) void { - if (pane.terminal == null) return; +/// A terminal loaded from a dump, its shell not there, made ready for a +/// new one: its program's screen modes reset (the old program is gone) and +/// its history marked off from what the new shell prints. +pub fn revive(state: *State) void { // The primary screen, keys, cursor and mouse as a shell starts with // them, the cursor where it was (a soft reset, or leaving a primary // screen for itself, would take it home). - if (pane.terminal.?.vt.screens.active_key == .alternate) ingest(pane, "\x1b[?1049l"); - ingest(pane, "\x1b[?1l\x1b>\x1b[?25h\x1b[?7h\x1b[?1000l\x1b[?1002l\x1b[?1003l\x1b[?1006l\x1b[?2004l\x1b[<u"); - ingest(pane, restored_banner); - // What the old shell was, ran and said is gone with it: the REPL it - // ran, the Term+ word naming it. - pane.shell_spoke = false; - pane.had_shell = false; - pane.shell_failed = false; - pane.shell_tail_len = 0; - pane.repl = null; - pane.sent_command = null; - pane.pending_command.wait = .spawn; + if (state.vt.screens.active_key == .alternate) ingest(state, "\x1b[?1049l"); + ingest(state, "\x1b[?1l\x1b>\x1b[?25h\x1b[?7h\x1b[?1000l\x1b[?1002l\x1b[?1003l\x1b[?1006l\x1b[?2004l\x1b[<u"); + ingest(state, restored_banner); } /// Feed the emulator. Live output and restoration share this byte path, /// then apply different views. -fn ingest(pane: *Pane, bytes: []const u8) void { - pane.terminal.?.stream.nextSlice(bytes); - pane.terminal.?.changes +%= 1; +fn ingest(state: *State, bytes: []const u8) void { + state.stream.nextSlice(bytes); + state.changes +%= 1; } /// What a pane says when a position kept over its terminal's rows could @@ -980,7 +933,7 @@ pub fn feedOutput(p: *Pardes, pane: *Pane, bytes: []const u8) void { const at_bottom = atBottom(pane); syncPixels(p, pane); if (!has_positions) { - ingest(pane, bytes); + 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); @@ -1147,7 +1100,7 @@ fn ingestWithPositions(pane: *Pane, bytes: []const u8) bool { // 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, bytes); + 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; @@ -2444,9 +2397,8 @@ fn syncPixels(p: *const Pardes, pane: *Pane) void { /// 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 animationDueMs(pane: *const Pane) ?u64 { +pub fn due(state: *const State) ?u64 { if (comptime !images_enabled) return null; - const state = pane.terminal orelse return null; return state.images.animation_due_ms; } @@ -2548,9 +2500,9 @@ fn lineBreaks(comptime n: usize) [2 * n]u8 { test "replay ownership is terminal-only and construction rolls back on allocation failure" { const Case = struct { fn run(gpa: std.mem.Allocator, terminal: bool) !void { - const pane = if (terminal) try create(gpa, 40, 12) else try createDoc(gpa, 40, 12); + const pane = if (terminal) try panes.createTerminal(gpa, 40, 12) else try panes.createDoc(gpa, 40, 12); defer gpa.destroy(pane); - defer deinitEmulator(pane, gpa); + defer if (pane.terminal) |state| close(state, gpa); try std.testing.expectEqual(terminal, pane.terminal != null); } }; @@ -2586,18 +2538,18 @@ test "a terminal made fresh has no size waiting: one never resized keeps its gri }; var filling: Filling = .{ .child = std.testing.allocator }; const gpa: std.mem.Allocator = .{ .ptr = &filling, .vtable = &Filling.vtable }; - const pane = try create(gpa, 40, 12); + const pane = try panes.createTerminal(gpa, 40, 12); defer gpa.destroy(pane); - defer deinitEmulator(pane, gpa); + defer if (pane.terminal) |state| close(state, gpa); try std.testing.expect(pane.terminal.?.resize_to == null); } test "document construction allocates only the pane and has inert terminal defaults" { var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{ .fail_index = 1 }); const allocator = failing.allocator(); - const pane = try createDoc(allocator, 40, 12); + const pane = try panes.createDoc(allocator, 40, 12); defer allocator.destroy(pane); - defer deinitEmulator(pane, allocator); + defer if (pane.terminal) |state| close(state, allocator); try std.testing.expect(pane.terminal == null); try std.testing.expectEqual(@as(usize, 1), failing.alloc_index); try std.testing.expectEqual(@as(usize, 0), failing.resize_index); diff --git a/src/tui/tui.zig b/src/tui/tui.zig index 77c8ce2b..8afa8ca8 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -685,11 +685,11 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se const have: ?*replica.Have = if (far) &have_store else null; const allocs = pardes.memory.init(gpa); defer pardes.memory.deinit(); - pardes.TerminalImages.start(allocs.image); + pardes.Term.start(allocs.image); pardes.Kind.heap = allocs.addon; pardes.syntax.start(allocs.tree_sitter); defer { - pardes.TerminalImages.stop(); + pardes.Term.stop(); pardes.syntax.stop(); } var o = opts; |
