1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
//! 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 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` (core/Term.zig), the records of
//! abi/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 TerminalImages = terminal_.TerminalImages;
/// 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),
.reload = cast(iface.Ops, "reload", terminal_.reload),
.media = terminal_.media,
.feed = cast(iface.Ops, "feed", terminal_.feed),
.grid = cast(iface.Ops, "grid", terminal_.grid),
.resize = cast(iface.Ops, "resize", terminal_.resize),
.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),
.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),
.hasImages = cast(iface.Ops, "hasImages", terminal_.hasImages),
};
/// 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 },
};
/// 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;
}
|