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/term/terminal.zig | |
| 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/term/terminal.zig')
| -rw-r--r-- | src/term/terminal.zig | 110 |
1 files changed, 31 insertions, 79 deletions
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); |
