From a22b579ad2df8d1811f0e194fa1bbc06e2282cf8 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 03:44:57 -0300 Subject: term: the terminal a module of its own (docs/next-terminal.md, Phase 1): src/term/term.zig roots it, importing std, ghostty-vt, wuffs, zstbi and plain, never the core, and gives the core one table, `ops` (plain.term.Ops: about 60 entries, the records they take and give and the opaque handles State, Encoder, Track and Replica, in src/plain/term.zig), its own functions put in it through a cast refused at compile time unless each parameter and result matches the table's but for the handles and every error is one the table names; src/Term.zig, the core's side, takes `ops` from the term module when the build's term_options say the core has one (every core module but core-test's), null otherwise, and Term.State is a handle with the walk's hooks, so a dump keeps a terminal as it did (its Parts by name through walk.save, then ghostty's snapshot) and the digest is the emulator's; echo.zig comes back to the core (src/echo.zig: predicted echo is the replica's presentation, its five reads of the emulator through Term.grid, modes, prompt and Term.echoCell); screen_state's Peer is a plain record both sides name (its noteSent a function of screen_state's), its Encoder, Track and Replica handles made and freed through the table (replica.Screens makes its encoder at the first encode); a prompt's cut is told in columns and measured in its row's text by the core (Term.promptPrefix, File.rawAtDisplay); TerminalImages' decode bound moves from memory.limits to the decoder it bounds; build.zig: core_lean, the core module without the terminal, is core-test's, and wireCore no longer gives any core module ghostty-vt, wuffs or zstbi (the term module has them), so core-test's compile names none of ghostty, wuffs, zstbi, simdutf, highway or -lc++ (from --verbose); term-test runs the module's own tests (the pager's, screen_state's, the luminance table's); the tests of a terminal through a core move as they were to test/terminal.zig, in pane-test, which imports both (`of` reads the emulator behind a handle; the dump round trip frees through State.dumpFree), and tests elsewhere that read the emulator do it through Term.emulatorOf, a test in core-test skipping first; core-test skips a test whose terminal Term.open refuses; Authority.init releases its tape when the core it boots fails (it leaked); tests: core-test (no terminal) 409 of 941, 532 skipped, shell-test (the same tests with the terminal) 984 of 984, pane-test 180 of 181 (1 skipped, as before), term-test 15 of 15, kitty-test 45 of 45, replicas checked/wrong 0, hxdiff 658 cases 0 mismatches, behaviour-test 268 of 272 (the base's four addon ABI tests), lib-test --- src/term/screen_state.zig | 131 +++++++++------------------------------------- 1 file changed, 25 insertions(+), 106 deletions(-) (limited to 'src/term/screen_state.zig') diff --git a/src/term/screen_state.zig b/src/term/screen_state.zig index 43eae542..59085578 100644 --- a/src/term/screen_state.zig +++ b/src/term/screen_state.zig @@ -32,6 +32,7 @@ //! `applyRows`). const std = @import("std"); const vt = @import("ghostty-vt"); +const plain = @import("plain"); const snapshot = vt.snapshot; pub const Terminal = vt.Terminal; const Page = vt.page.Page; @@ -198,7 +199,7 @@ fn clampHoles(holes: *std.ArrayList([2]u64), oldest: u64) void { /// Deltas a peer may have in flight. At 207 ms and 60 frames a second about /// 13 are; past the bound a peer gets nothing until an acknowledgement, and /// then the newest state, the ones between skipped. -pub const bound = 32; +pub const bound = plain.term.flight_bound; /// What a peer holds once it has a version: the addresses of its active /// rows and its meta. A delta against it sends no blob for these. @@ -290,60 +291,30 @@ pub const Track = struct { } }; -/// What the authority knows of one peer of one terminal. -pub const Peer = struct { - /// The version last sent, which the next delta is against; 0: none, so - /// the next starts the replica afresh. - sent: u32 = 0, - /// The versions in flight, oldest first. - flight: [bound]u32 = undefined, - n: u8 = 0, - /// The history rows its replica holds as holes, absolute, as the - /// replica keeps them (Replica.apply's rule, from what it was sent): - /// what `fill` sends before a step reads them. - top: u64 = 0, - holes: std.ArrayListUnmanaged([2]u64) = .empty, - - pub fn deinit(peer: *Peer, gpa: std.mem.Allocator) void { - peer.holes.deinit(gpa); - } - - /// Every version up to `seq` has landed. - pub fn ack(peer: *Peer, seq: u32) void { - var i: u8 = 0; - while (i < peer.n and peer.flight[i] <= seq) i += 1; - std.mem.copyForwards(u32, peer.flight[0 .. peer.n - i], peer.flight[i..peer.n]); - peer.n -= i; - } +/// What the authority knows of one peer of one terminal: a plain record +/// both sides name (src/plain/term.zig), the core keeping one a peer. +pub const Peer = plain.term.Peer; - /// Start the peer afresh: it lost its state, or never had one. - pub fn reset(peer: *Peer) void { - var holes = peer.holes; - holes.clearRetainingCapacity(); - peer.* = .{ .holes = holes }; - } - - /// Its replica's holes once it applies a delta to `tr`'s newest version - /// against `base`, as Replica.apply makes them. - fn noteSent(peer: *Peer, gpa: std.mem.Allocator, tr: *const Track, base: ?*const Known, rows: u16) !void { - const top = tr.rows.top; - const history = tr.history; - if (base) |b| { - if (top > peer.top) { - const tail = @min(top -| b.top, rows, history); - if (top - peer.top >= @as(u64, rows) + history) { - peer.holes.clearRetainingCapacity(); - if (history > tail) try peer.holes.append(gpa, .{ top - history, top - tail }); - } else if (top - tail > peer.top) try peer.holes.append(gpa, .{ peer.top, top - tail }); - } - } else { - peer.holes.clearRetainingCapacity(); - if (history > 0) try peer.holes.append(gpa, .{ top - history, top }); +/// Its replica's holes once it applies a delta to `tr`'s newest version +/// against `base`, as Replica.apply makes them. +fn noteSent(peer: *Peer, gpa: std.mem.Allocator, tr: *const Track, base: ?*const Known, rows: u16) !void { + const top = tr.rows.top; + const history = tr.history; + if (base) |b| { + if (top > peer.top) { + const tail = @min(top -| b.top, rows, history); + if (top - peer.top >= @as(u64, rows) + history) { + peer.holes.clearRetainingCapacity(); + if (history > tail) try peer.holes.append(gpa, .{ top - history, top - tail }); + } else if (top - tail > peer.top) try peer.holes.append(gpa, .{ peer.top, top - tail }); } - peer.top = top; - clampHoles(&peer.holes, top -| history); + } else { + peer.holes.clearRetainingCapacity(); + if (history > 0) try peer.holes.append(gpa, .{ top - history, top }); } -}; + peer.top = top; + clampHoles(&peer.holes, top -| history); +} /// The delta's fixed header: u32 seq, u32 base (0: none, the replica /// starts afresh), u8 flags, u32 epoch, u64 meta, u16 cursor x and y, u8 @@ -376,7 +347,7 @@ pub fn encode(e: *Encoder, tr: *Track, peer: *Peer, t: *Terminal, changes: u32, tr.cache_base = base_version; } try out.writeAll(tr.cache.written()); - try peer.noteSent(e.gpa, tr, base, t.rows); + try noteSent(peer, e.gpa, tr, base, t.rows); peer.sent = tr.version; peer.flight[peer.n] = tr.version; peer.n += 1; @@ -513,7 +484,7 @@ pub const Replica = struct { if (r.scratch) |*s| s.deinit(); } - pub const Applied = struct { seq: u32, extra: []const u8 }; + pub const Applied = plain.term.Applied; /// Write one delta into `t`: the seq to acknowledge, and the caller's /// extra bytes (borrowed from `bytes`). error.BaseMismatch: the delta @@ -1240,55 +1211,3 @@ test "several deltas in flight land in order, and a peer at the bound waits for try testing.expectEqual(@as(u64, 0), s.stats.refused); } -test "a replica's terminal pane draws as its authority's does" { - const pardes = @import("../pardes.zig"); - const gpa = testing.allocator; - const a = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 16 }); - defer a.deinit(); - const b = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 16 }); - defer b.deinit(); - while (a.nextEffect()) |_| {} - while (b.nextEffect()) |_| {} - var fa = std.heap.ArenaAllocator.init(gpa); - defer fa.deinit(); - var fb = std.heap.ArenaAllocator.init(gpa); - defer fb.deinit(); - // Laid out once, so both panes have their size before the first delta. - _ = try a.render(fa.allocator()); - _ = try b.render(fb.allocator()); - const ta = &a.panes[0].?.terminal.?.vt; - const tb = &b.panes[0].?.terminal.?.vt; - var enc: Encoder = .init(gpa); - defer enc.deinit(); - var peer: Peer = .{}; - defer peer.deinit(gpa); - var tr: Track = .init(gpa); - defer tr.deinit(gpa, ta); - var rep: Replica = .{}; - defer rep.deinit(gpa); - var delta: std.Io.Writer.Allocating = .init(gpa); - defer delta.deinit(); - var line: [256]u8 = undefined; - for (0..60) |i| { - const bytes = try std.fmt.bufPrint(&line, "\x1b[1;3{d}m{d}\x1b[0m 漢字 e\u{301} \x1b[48;2;{d};40;60mbg\x1b[0m and then some text that wraps past the width of the pane\r\n", .{ i % 8, i, i * 4 % 256 }); - a.update(.{ .output = .{ .pane = 0, .bytes = bytes } }); - delta.clearRetainingCapacity(); - try testing.expect(try encode(&enc, &tr, &peer, ta, @intCast(i + 1), "", &delta.writer)); - peer.ack((try rep.apply(gpa, std.Io.Threaded.global_single_threaded.io(), tb, delta.written())).seq); - b.needs_frame = true; - _ = fa.reset(.retain_capacity); - _ = fb.reset(.retain_capacity); - const sa = try a.render(fa.allocator()); - const sb = try b.render(fb.allocator()); - const r = a.view().rects[0]; - // The body: below the pane's tag row. - for (r.y + 1..r.y + r.h) |y| for (r.x..r.x + r.w) |x| { - const ca = sa.at(@intCast(x), @intCast(y)); - const cb = sb.at(@intCast(x), @intCast(y)); - if (!ca.visuallyEqual(cb)) { - std.debug.print("step {d}: cell {d},{d}: '{s}' vs '{s}'\n", .{ i, x, y, ca.grapheme(), cb.grapheme() }); - return error.ReplicaDrawsDifferently; - } - }; - } -} -- cgit v1.3