diff options
Diffstat (limited to 'src/term/term.zig')
| -rw-r--r-- | src/term/term.zig | 164 |
1 files changed, 164 insertions, 0 deletions
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_; +} |
