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/term.zig | 164 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 src/term/term.zig (limited to 'src/term/term.zig') diff --git a/src/term/term.zig b/src/term/term.zig new file mode 100644 index 00000000..87c06611 --- /dev/null +++ b/src/term/term.zig @@ -0,0 +1,164 @@ +//! The terminal, a module of its own (docs/next-terminal.md, Phase 1): +//! ghostty-vt's emulator and what pardes keeps beside it (terminal.zig), +//! the screen state its replicas are sent (screen_state.zig), the kitty +//! images it draws (TerminalImages.zig) and the pager's parse (pager.zig). +//! It imports std, ghostty-vt, wuffs, zstbi and `plain`, never the core: +//! the core reaches it through `ops` (src/Term.zig), the records of +//! src/plain/term.zig in and out. +const std = @import("std"); +const plain = @import("plain"); +const iface = plain.term; +const terminal_ = @import("terminal.zig"); +const pager = @import("pager.zig"); +const screen_state_ = @import("screen_state.zig"); +const TerminalImages = terminal_.TerminalImages; + +/// The screen state replicas are sent, its streams and soak (test/replicas.zig). +pub const screen_state = screen_state_; +/// The emulator itself, for the tests that read it through a core +/// (test/terminal.zig). +pub const terminal = terminal_; + +pub const ops: iface.Ops = .{ + .open = cast(iface.Ops, "open", terminal_.open), + .close = cast(iface.Ops, "close", terminal_.close), + .due = cast(iface.Ops, "due", terminal_.due), + .revive = cast(iface.Ops, "revive", terminal_.revive), + .start = TerminalImages.start, + .stop = TerminalImages.stop, + .gpa = cast(iface.Ops, "gpa", terminal_.gpaOf), + .digest = cast(iface.Ops, "digest", terminal_.digest), + .parts = cast(iface.Ops, "parts", terminal_.parts), + .snapshot = cast(iface.Ops, "snapshot", terminal_.snapshot), + .load = cast(iface.Ops, "load", terminal_.load), + .feed = cast(iface.Ops, "feed", terminal_.feed), + .grid = cast(iface.Ops, "grid", terminal_.grid), + .viewTo = cast(iface.Ops, "viewTo", terminal_.viewTo), + .scroll = cast(iface.Ops, "scroll", terminal_.scroll), + .follow = cast(iface.Ops, "follow", terminal_.follow), + .resize = cast(iface.Ops, "resize", terminal_.resize), + .resizeDeferred = cast(iface.Ops, "resizeDeferred", terminal_.resizeDeferred), + .cellPixels = cast(iface.Ops, "cellPixels", terminal_.cellPixels), + .replies = cast(iface.Ops, "replies", terminal_.replies), + .key = cast(iface.Ops, "key", terminal_.key), + .mouse = cast(iface.Ops, "mouse", terminal_.mouse), + .wheel = cast(iface.Ops, "wheel", terminal_.wheel), + .paste = cast(iface.Ops, "paste", terminal_.paste), + .modes = cast(iface.Ops, "modes", terminal_.modes), + .promptMoves = cast(iface.Ops, "promptMoves", terminal_.promptMoves), + .prompt = cast(iface.Ops, "prompt", terminal_.prompt), + .commandOutput = cast(iface.Ops, "commandOutput", terminal_.commandOutput), + .endOutputHere = cast(iface.Ops, "endOutputHere", terminal_.endOutputHere), + .scrollback = cast(iface.Ops, "scrollback", terminal_.scrollback), + .scrollbackLen = cast(iface.Ops, "scrollbackLen", terminal_.scrollbackLen), + .textRows = cast(iface.Ops, "textRows", terminal_.textRows), + .viewport = cast(iface.Ops, "viewport", terminal_.viewport), + .editRows = cast(iface.Ops, "editRows", terminal_.editRows), + .promptCut = cast(iface.Ops, "promptCut", terminal_.promptCutAt), + .markRead = cast(iface.Ops, "markRead", terminal_.markRead), + .echoCell = cast(iface.Ops, "echoCell", terminal_.echoCell), + .page = pager.parse, + .filter = terminal_.pagerFilter, + .spanColor = terminal_.spanColor, + .paint = cast(iface.Ops, "paint", terminal_.paint), + .images = cast(iface.Ops, "images", terminal_.images), + .petscii = cast(iface.Ops, "petscii", terminal_.petscii), + .togglePetscii = cast(iface.Ops, "togglePetscii", terminal_.togglePetscii), + .hasImages = cast(iface.Ops, "hasImages", terminal_.hasImages), + .encoder = cast(iface.Ops, "encoder", terminal_.encoder), + .encoderFree = cast(iface.Ops, "encoderFree", terminal_.encoderFree), + .track = cast(iface.Ops, "track", terminal_.track), + .untrack = cast(iface.Ops, "untrack", terminal_.untrack), + .encode = cast(iface.Ops, "encode", terminal_.encode), + .encodeRows = cast(iface.Ops, "encodeRows", terminal_.encodeRows), + .wasRead = cast(iface.Ops, "wasRead", terminal_.wasRead), + .clearRead = cast(iface.Ops, "clearRead", terminal_.clearRead), + .replica = cast(iface.Ops, "replica", terminal_.replica), + .replicaFree = cast(iface.Ops, "replicaFree", terminal_.replicaFree), + .replicaTop = cast(iface.Ops, "replicaTop", terminal_.replicaTop), + .missing = cast(iface.Ops, "missing", terminal_.missing), + .apply = cast(iface.Ops, "apply", terminal_.apply), + .applyRows = cast(iface.Ops, "applyRows", terminal_.applyRows), + .shown = cast(iface.Ops, "shown", terminal_.shown), + .blank = cast(iface.Ops, "blank", terminal_.blank), + .dumpScreen = cast(iface.Ops, "dumpScreen", terminal_.dumpScreen), +}; + +/// The term's handle types as the interface names them, and the module's +/// own behind each: a pointer to one is a pointer to the other. +const handles = .{ + .{ iface.State, terminal_.State }, + .{ iface.Encoder, screen_state_.Encoder }, + .{ iface.Track, screen_state_.Track }, + .{ iface.Replica, screen_state_.Replica }, +}; + +/// The interface's handle for one of this module's own types, or the type +/// itself. +fn handleOf(comptime T: type) type { + inline for (handles) |pair| if (T == pair[1]) return pair[0]; + return switch (@typeInfo(T)) { + .pointer => |p| if (p.size != .one) T else if (p.attrs.@"const") *const handleOf(p.child) else *handleOf(p.child), + .optional => |o| ?handleOf(o.child), + else => T, + }; +} + +/// Every error of `Have` is one of `Want`'s (anyerror holds them all). +fn within(comptime Have: type, comptime Want: type) bool { + const want = @typeInfo(Want).error_set.error_names orelse return true; + const have = @typeInfo(Have).error_set.error_names orelse return false; + outer: for (have) |h| { + for (want) |w| if (std.mem.eql(u8, h, w)) continue :outer; + return false; + } + return true; +} + +/// The errors of result `H` that result `W` has not, named. +fn missingFrom(comptime H: type, comptime W: type) []const u8 { + if (@typeInfo(H) != .error_union or @typeInfo(W) != .error_union) return ""; + const want = @typeInfo(@typeInfo(W).error_union.error_set).error_set.error_names orelse return ""; + const have = @typeInfo(@typeInfo(H).error_union.error_set).error_set.error_names orelse return " (anyerror)"; + var out: []const u8 = ": not"; + outer: for (have) |h| { + for (want) |w| if (std.mem.eql(u8, h, w)) continue :outer; + out = out ++ " " ++ h; + } + return out; +} + +/// `f`, one of this module's own entries, as the `Ops` field `name`: the +/// same function, its handles the interface's opaque ones. Refused at +/// compile time unless every parameter and the result match but for them. +fn cast(comptime O: type, comptime name: []const u8, comptime f: anytype) @FieldType(O, name) { + @setEvalBranchQuota(100_000); + const Want = @typeInfo(@FieldType(O, name)).pointer.child; + const want = @typeInfo(Want).@"fn"; + const have = @typeInfo(@TypeOf(f)).@"fn"; + if (want.param_types.len != have.param_types.len) @compileError(name ++ ": not as many parameters as Ops says"); + inline for (want.param_types, have.param_types, 0..) |w, h, i| { + if (w.? != handleOf(h.?)) @compileError(std.fmt.comptimePrint("{s}: parameter {d} is {s}, Ops says {s}", .{ name, i, @typeName(h.?), @typeName(w.?) })); + } + // A narrower error set is the same function pointer: an error is its + // number whatever set names it. + const W = want.return_type.?; + const H = have.return_type.?; + const same_result = switch (@typeInfo(W)) { + // Ops says anyerror: any error set of the same payload. + .error_union => |we| switch (@typeInfo(H)) { + .error_union => |he| we.payload == handleOf(he.payload) and within(he.error_set, we.error_set), + else => false, + }, + else => W == handleOf(H), + }; + if (!same_result) @compileError(name ++ ": returns " ++ @typeName(H) ++ ", Ops says " ++ @typeName(W) ++ missingFrom(H, W)); + return @ptrCast(&f); +} + +test { + _ = &ops; + _ = terminal_; + _ = pager; + _ = screen_state_; +} -- cgit v1.3