summaryrefslogtreecommitdiff
path: root/src/inspector/termio.zig
blob: 212f0ea4a62d3bcf4587a1dc6c0f20db2c98d6ba (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
const std = @import("std");
const Allocator = std.mem.Allocator;
const cimgui = @import("cimgui");
const terminal = @import("../terminal/main.zig");
const CircBuf = @import("../datastruct/main.zig").CircBuf;
const Surface = @import("../Surface.zig");

/// The stream handler for our inspector.
pub const Stream = terminal.Stream(VTHandler);

/// VT event circular buffer.
pub const VTEventRing = CircBuf(VTEvent, undefined);

/// VT event
pub const VTEvent = struct {
    /// Sequence number, just monotonically increasing.
    seq: usize = 1,

    /// Kind of event, for filtering
    kind: Kind,

    /// The formatted string of the event. This is allocated. We format the
    /// event for now because there is so much data to copy if we wanted to
    /// store the raw event.
    str: [:0]const u8,

    /// Various metadata at the time of the event (before processing).
    cursor: terminal.Screen.Cursor,
    scrolling_region: terminal.Terminal.ScrollingRegion,
    metadata: Metadata.Unmanaged = .{},

    /// imgui selection state
    imgui_selected: bool = false,

    const Kind = enum { print, execute, csi, esc, osc, dcs, apc };
    const Metadata = std.StringHashMap([:0]const u8);

    /// Initialize the event information for the given parser action.
    pub fn init(
        alloc: Allocator,
        surface: *Surface,
        action: terminal.Parser.Action,
    ) !VTEvent {
        var md = Metadata.init(alloc);
        errdefer md.deinit();
        var buf: std.Io.Writer.Allocating = .init(alloc);
        defer buf.deinit();
        try encodeAction(alloc, &buf.writer, &md, action);
        const str = try buf.toOwnedSliceSentinel(0);
        errdefer alloc.free(str);

        const kind: Kind = switch (action) {
            .print => .print,
            .execute => .execute,
            .csi_dispatch => .csi,
            .esc_dispatch => .esc,
            .osc_dispatch => .osc,
            .dcs_hook, .dcs_put, .dcs_unhook => .dcs,
            .apc_start, .apc_put, .apc_end => .apc,
        };

        const t = surface.renderer_state.terminal;

        return .{
            .kind = kind,
            .str = str,
            .cursor = t.screen.cursor,
            .scrolling_region = t.scrolling_region,
            .metadata = md.unmanaged,
        };
    }

    pub fn deinit(self: *VTEvent, alloc: Allocator) void {
        {
            var it = self.metadata.valueIterator();
            while (it.next()) |v| alloc.free(v.*);
            self.metadata.deinit(alloc);
        }

        alloc.free(self.str);
    }

    /// Returns true if the event passes the given filter.
    pub fn passFilter(
        self: *const VTEvent,
        filter: *cimgui.c.ImGuiTextFilter,
    ) bool {
        // Check our main string
        if (cimgui.c.ImGuiTextFilter_PassFilter(
            filter,
            self.str.ptr,
            null,
        )) return true;

        // We also check all metadata keys and values
        var it = self.metadata.iterator();
        while (it.next()) |entry| {
            var buf: [256]u8 = undefined;
            const key = std.fmt.bufPrintZ(&buf, "{s}", .{entry.key_ptr.*}) catch continue;
            if (cimgui.c.ImGuiTextFilter_PassFilter(
                filter,
                key.ptr,
                null,
            )) return true;
            if (cimgui.c.ImGuiTextFilter_PassFilter(
                filter,
                entry.value_ptr.ptr,
                null,
            )) return true;
        }

        return false;
    }

    /// Encode a parser action as a string that we show in the logs.
    fn encodeAction(
        alloc: Allocator,
        writer: *std.Io.Writer,
        md: *Metadata,
        action: terminal.Parser.Action,
    ) !void {
        switch (action) {
            .print => try encodePrint(writer, action),
            .execute => try encodeExecute(writer, action),
            .csi_dispatch => |v| try encodeCSI(writer, v),
            .esc_dispatch => |v| try encodeEsc(writer, v),
            .osc_dispatch => |v| try encodeOSC(alloc, writer, md, v),
            else => try writer.print("{f}", .{action}),
        }
    }

    fn encodePrint(writer: *std.Io.Writer, action: terminal.Parser.Action) !void {
        const ch = action.print;
        try writer.print("'{u}' (U+{X})", .{ ch, ch });
    }

    fn encodeExecute(writer: *std.Io.Writer, action: terminal.Parser.Action) !void {
        const ch = action.execute;
        switch (ch) {
            0x00 => try writer.writeAll("NUL"),
            0x01 => try writer.writeAll("SOH"),
            0x02 => try writer.writeAll("STX"),
            0x03 => try writer.writeAll("ETX"),
            0x04 => try writer.writeAll("EOT"),
            0x05 => try writer.writeAll("ENQ"),
            0x06 => try writer.writeAll("ACK"),
            0x07 => try writer.writeAll("BEL"),
            0x08 => try writer.writeAll("BS"),
            0x09 => try writer.writeAll("HT"),
            0x0A => try writer.writeAll("LF"),
            0x0B => try writer.writeAll("VT"),
            0x0C => try writer.writeAll("FF"),
            0x0D => try writer.writeAll("CR"),
            0x0E => try writer.writeAll("SO"),
            0x0F => try writer.writeAll("SI"),
            else => try writer.writeAll("?"),
        }
        try writer.print(" (0x{X})", .{ch});
    }

    fn encodeCSI(writer: *std.Io.Writer, csi: terminal.Parser.Action.CSI) !void {
        for (csi.intermediates) |v| try writer.print("{c} ", .{v});
        for (csi.params, 0..) |v, i| {
            if (i != 0) try writer.writeByte(';');
            try writer.print("{d}", .{v});
        }
        if (csi.intermediates.len > 0 or csi.params.len > 0) try writer.writeByte(' ');
        try writer.writeByte(csi.final);
    }

    fn encodeEsc(writer: *std.Io.Writer, esc: terminal.Parser.Action.ESC) !void {
        for (esc.intermediates) |v| try writer.print("{c} ", .{v});
        try writer.writeByte(esc.final);
    }

    fn encodeOSC(
        alloc: Allocator,
        writer: *std.Io.Writer,
        md: *Metadata,
        osc: terminal.osc.Command,
    ) !void {
        // The description is just the tag
        try writer.print("{s} ", .{@tagName(osc)});

        // Add additional fields to metadata
        switch (osc) {
            inline else => |v, tag| if (tag == osc) {
                try encodeMetadata(alloc, md, v);
            },
        }
    }

    fn encodeMetadata(
        alloc: Allocator,
        md: *Metadata,
        v: anytype,
    ) !void {
        switch (@TypeOf(v)) {
            void => {},
            []const u8,
            [:0]const u8,
            => try md.put("data", try alloc.dupeZ(u8, v)),
            else => |T| switch (@typeInfo(T)) {
                .@"struct" => |info| inline for (info.fields) |field| {
                    try encodeMetadataSingle(
                        alloc,
                        md,
                        field.name,
                        @field(v, field.name),
                    );
                },

                .@"union" => |info| {
                    const Tag = info.tag_type orelse @compileError("Unions must have a tag");
                    const tag_name = @tagName(@as(Tag, v));
                    inline for (info.fields) |field| {
                        if (std.mem.eql(u8, field.name, tag_name)) {
                            if (field.type == void) {
                                break try md.put("data", tag_name);
                            } else {
                                break try encodeMetadataSingle(alloc, md, tag_name, @field(v, field.name));
                            }
                        }
                    }
                },

                else => {
                    @compileLog(T);
                    @compileError("unsupported type, see log");
                },
            },
        }
    }

    fn encodeMetadataSingle(
        alloc: Allocator,
        md: *Metadata,
        key: []const u8,
        value: anytype,
    ) !void {
        const Value = @TypeOf(value);
        const info = @typeInfo(Value);
        switch (info) {
            .optional => if (value) |unwrapped| {
                try encodeMetadataSingle(alloc, md, key, unwrapped);
            } else {
                try md.put(key, try alloc.dupeZ(u8, "(unset)"));
            },

            .bool => try md.put(
                key,
                try alloc.dupeZ(u8, if (value) "true" else "false"),
            ),

            .@"enum" => try md.put(
                key,
                try alloc.dupeZ(u8, @tagName(value)),
            ),

            .@"union" => |u| {
                const Tag = u.tag_type orelse @compileError("Unions must have a tag");
                const tag_name = @tagName(@as(Tag, value));
                inline for (u.fields) |field| {
                    if (std.mem.eql(u8, field.name, tag_name)) {
                        const s = if (field.type == void)
                            try alloc.dupeZ(u8, tag_name)
                        else if (field.type == [:0]const u8 or field.type == []const u8)
                            try std.fmt.allocPrintSentinel(alloc, "{s}={s}", .{
                                tag_name,
                                @field(value, field.name),
                            }, 0)
                        else
                            try std.fmt.allocPrintSentinel(alloc, "{s}={}", .{
                                tag_name,
                                @field(value, field.name),
                            }, 0);

                        try md.put(key, s);
                    }
                }
            },

            .@"struct" => try md.put(
                key,
                try alloc.dupeZ(u8, @typeName(Value)),
            ),

            else => switch (Value) {
                u8, u16 => try md.put(
                    key,
                    try std.fmt.allocPrintSentinel(alloc, "{}", .{value}, 0),
                ),

                []const u8,
                [:0]const u8,
                => try md.put(key, try alloc.dupeZ(u8, value)),

                else => |T| {
                    @compileLog(T);
                    @compileError("unsupported type, see log");
                },
            },
        }
    }
};

/// Our VT stream handler.
pub const VTHandler = struct {
    /// The surface that the inspector is attached to. We use this instead
    /// of the inspector because this is pointer-stable.
    surface: *Surface,

    /// True if the handler is currently recording.
    active: bool = true,

    /// Current sequence number
    current_seq: usize = 1,

    /// Exclude certain actions by tag.
    filter_exclude: ActionTagSet = .initMany(&.{.print}),
    filter_text: *cimgui.c.ImGuiTextFilter,

    const ActionTagSet = std.EnumSet(terminal.Parser.Action.Tag);

    pub fn init(surface: *Surface) VTHandler {
        return .{
            .surface = surface,
            .filter_text = cimgui.c.ImGuiTextFilter_ImGuiTextFilter(""),
        };
    }

    pub fn deinit(self: *VTHandler) void {
        cimgui.c.ImGuiTextFilter_destroy(self.filter_text);
    }

    /// This is called with every single terminal action.
    pub fn handleManually(self: *VTHandler, action: terminal.Parser.Action) !bool {
        const insp = self.surface.inspector orelse return false;

        // We always increment the sequence number, even if we're paused or
        // filter out the event. This helps show the user that there is a gap
        // between events and roughly how large that gap was.
        defer self.current_seq +%= 1;

        // If we're pausing, then we ignore all events.
        if (!self.active) return true;

        // We ignore certain action types that are too noisy.
        switch (action) {
            .dcs_put, .apc_put => return true,
            else => {},
        }

        // If we requested a specific type to be ignored, ignore it.
        // We return true because we did "handle" it by ignoring it.
        if (self.filter_exclude.contains(std.meta.activeTag(action))) return true;

        // Build our event
        const alloc = self.surface.alloc;
        var ev = try VTEvent.init(alloc, self.surface, action);
        ev.seq = self.current_seq;
        errdefer ev.deinit(alloc);

        // Check if the event passes the filter
        if (!ev.passFilter(self.filter_text)) {
            ev.deinit(alloc);
            return true;
        }

        const max_capacity = 100;
        insp.vt_events.append(ev) catch |err| switch (err) {
            error.OutOfMemory => if (insp.vt_events.capacity() < max_capacity) {
                // We're out of memory, but we can allocate to our capacity.
                const new_capacity = @min(insp.vt_events.capacity() * 2, max_capacity);
                try insp.vt_events.resize(insp.surface.alloc, new_capacity);
                try insp.vt_events.append(ev);
            } else {
                var it = insp.vt_events.iterator(.forward);
                if (it.next()) |old_ev| old_ev.deinit(insp.surface.alloc);
                insp.vt_events.deleteOldest(1);
                try insp.vt_events.append(ev);
            },

            else => return err,
        };

        return true;
    }
};