diff options
| author | Can Celik <ogulcancelik@gmail.com> | 2026-04-04 19:22:04 +0300 |
|---|---|---|
| committer | Can Celik <ogulcancelik@gmail.com> | 2026-04-05 01:23:06 +0300 |
| commit | 41dd733ca7123c9ac4bfc6c781697a660e95b052 (patch) | |
| tree | 3fc91815ca139ef2e0b281bba0abda4ef15175f4 /vendor/libghostty-vt/src/synthetic/Generator.zig | |
| parent | 55c138263e190a1e79d4b997bfb9397306c0ec1f (diff) | |
feat: finish ghostty pane backend integration
Diffstat (limited to 'vendor/libghostty-vt/src/synthetic/Generator.zig')
| -rw-r--r-- | vendor/libghostty-vt/src/synthetic/Generator.zig | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/vendor/libghostty-vt/src/synthetic/Generator.zig b/vendor/libghostty-vt/src/synthetic/Generator.zig new file mode 100644 index 00000000..28929ecb --- /dev/null +++ b/vendor/libghostty-vt/src/synthetic/Generator.zig @@ -0,0 +1,42 @@ +/// A common interface for all generators. +const Generator = @This(); + +const std = @import("std"); +const assert = std.debug.assert; + +/// For generators, this is the only error that is allowed to be +/// returned by the next function. +pub const Error = error{WriteFailed}; + +/// The vtable for the generator. +ptr: *anyopaque, +nextFn: *const fn (ptr: *anyopaque, *std.Io.Writer, usize) Error!void, + +/// Create a new generator from a pointer and a function pointer. +/// This usually is only called by generator implementations, not +/// generator users. +pub fn init( + pointer: anytype, + comptime nextFn: fn (ptr: @TypeOf(pointer), *std.Io.Writer, usize) Error!void, +) Generator { + const Ptr = @TypeOf(pointer); + assert(@typeInfo(Ptr) == .pointer); // Must be a pointer + assert(@typeInfo(Ptr).pointer.size == .one); // Must be a single-item pointer + assert(@typeInfo(@typeInfo(Ptr).pointer.child) == .@"struct"); // Must point to a struct + const gen = struct { + fn next(ptr: *anyopaque, writer: *std.Io.Writer, max_len: usize) Error!void { + const self: Ptr = @ptrCast(@alignCast(ptr)); + try nextFn(self, writer, max_len); + } + }; + + return .{ + .ptr = pointer, + .nextFn = gen.next, + }; +} + +/// Get the next value from the generator. Returns the data written. +pub fn next(self: Generator, writer: *std.Io.Writer, max_size: usize) Error!void { + try self.nextFn(self.ptr, writer, max_size); +} |
