aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/libghostty-vt/src/synthetic/cli/Osc.zig
blob: 686563fc3f3137a0c8a798b793ba3863231d15ae (plain) (blame)
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
const Osc = @This();

const std = @import("std");
const assert = std.debug.assert;
const Allocator = std.mem.Allocator;
const synthetic = @import("../main.zig");

const log = std.log.scoped(.@"terminal-stream-bench");

pub const Options = struct {
    /// Probability of generating a valid value.
    @"p-valid": f64 = 0.5,

    style: enum {
        /// Write all OSC data, including ESC ] and ST for end-to-end tests
        streaming,
        /// Only write data, prefixed with a length, used for testing just the
        /// OSC parser.
        parser,
    } = .streaming,
};

opts: Options,

/// Create a new terminal stream handler for the given arguments.
pub fn create(
    alloc: Allocator,
    opts: Options,
) !*Osc {
    const ptr = try alloc.create(Osc);
    errdefer alloc.destroy(ptr);
    ptr.* = .{ .opts = opts };
    return ptr;
}

pub fn destroy(self: *Osc, alloc: Allocator) void {
    alloc.destroy(self);
}

pub fn run(self: *Osc, writer: *std.Io.Writer, rand: std.Random) !void {
    var gen: synthetic.Osc = .{
        .rand = rand,
        .p_valid = self.opts.@"p-valid",
    };

    var buf: [1024]u8 = undefined;
    while (true) {
        var fixed: std.Io.Writer = .fixed(&buf);
        try gen.next(&fixed, buf.len);
        const data = fixed.buffered();
        switch (self.opts.style) {
            .streaming => {
                writer.writeAll(data) catch |err| switch (err) {
                    error.WriteFailed => return,
                };
            },
            .parser => {
                writer.writeInt(usize, data.len - 3, .little) catch |err| switch (err) {
                    error.WriteFailed => return,
                };
                writer.writeAll(data[2 .. data.len - 1]) catch |err| switch (err) {
                    error.WriteFailed => return,
                };
            },
        }
    }
}

test Osc {
    const testing = std.testing;
    const alloc = testing.allocator;

    const impl: *Osc = try .create(alloc, .{});
    defer impl.destroy(alloc);

    var prng = std.Random.DefaultPrng.init(1);
    const rand = prng.random();

    var buf: [1024]u8 = undefined;
    var writer: std.Io.Writer = .fixed(&buf);
    try impl.run(&writer, rand);
}