diff options
| author | Can Celik <ogulcancelik@gmail.com> | 2026-09-10 21:51:57 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-09-10 21:51:57 +0300 |
| commit | 425c86179791cd32e2d4ce0ae7f269940b4b0663 (patch) | |
| tree | e52c4383a6d69216cbe2a32638bf90841d7e2189 /vendor/libghostty-vt/src/synthetic/cli/Ascii.zig | |
| parent | 90e947a6e26e02f10ec7f1694c4eb5b78a86406b (diff) | |
chore: upgrade libghostty and preserve terminal behavior (#3906)
Diffstat (limited to 'vendor/libghostty-vt/src/synthetic/cli/Ascii.zig')
| -rw-r--r-- | vendor/libghostty-vt/src/synthetic/cli/Ascii.zig | 61 |
1 files changed, 54 insertions, 7 deletions
diff --git a/vendor/libghostty-vt/src/synthetic/cli/Ascii.zig b/vendor/libghostty-vt/src/synthetic/cli/Ascii.zig index d416189c..b9ce2cb1 100644 --- a/vendor/libghostty-vt/src/synthetic/cli/Ascii.zig +++ b/vendor/libghostty-vt/src/synthetic/cli/Ascii.zig @@ -7,7 +7,14 @@ const Bytes = @import("../Bytes.zig"); const log = std.log.scoped(.@"terminal-stream-bench"); -pub const Options = struct {}; +pub const Options = struct { + /// When nonzero, emit lines whose printable length is uniformly + /// distributed in `[line-min, line-max]`, each terminated by CR LF. + /// When zero (the default), emit an unbroken stream of printable + /// bytes that relies on terminal wrapping. + @"line-min": usize = 0, + @"line-max": usize = 0, +}; fn checkAsciiAlphabet(c: u8) bool { return switch (c) { @@ -18,13 +25,16 @@ fn checkAsciiAlphabet(c: u8) bool { pub const ascii = Bytes.generateAlphabet(checkAsciiAlphabet); +opts: Options, + /// Create a new terminal stream handler for the given arguments. pub fn create( alloc: Allocator, - _: Options, + opts: Options, ) !*Ascii { const ptr = try alloc.create(Ascii); errdefer alloc.destroy(ptr); + ptr.* = .{ .opts = opts }; return ptr; } @@ -32,26 +42,36 @@ pub fn destroy(self: *Ascii, alloc: Allocator) void { alloc.destroy(self); } -pub fn run(_: *Ascii, writer: *std.Io.Writer, rand: std.Random) !void { +pub fn run(self: *Ascii, writer: *std.Io.Writer, rand: std.Random) !void { + const line_max = @max(self.opts.@"line-min", self.opts.@"line-max"); + const lines = line_max > 0; var gen: Bytes = .{ .rand = rand, .alphabet = ascii, - .min_len = 1024, - .max_len = 1024, + .min_len = if (lines) @max(self.opts.@"line-min", 1) else 1024, + .max_len = if (lines) line_max else 1024, }; while (true) { - _ = gen.write(writer) catch |err| { + writeChunk(&gen, writer, lines) catch |err| { const Error = error{ WriteFailed, BrokenPipe } || @TypeOf(err); switch (@as(Error, err)) { error.BrokenPipe => return, // stdout closed error.WriteFailed => return, // fixed buffer full - else => return err, } }; } } +fn writeChunk( + gen: *const Bytes, + writer: *std.Io.Writer, + lines: bool, +) std.Io.Writer.Error!void { + _ = try gen.write(writer); + if (lines) try writer.writeAll("\r\n"); +} + test Ascii { const testing = std.testing; const alloc = testing.allocator; @@ -66,3 +86,30 @@ test Ascii { var writer: std.Io.Writer = .fixed(&buf); try impl.run(&writer, rand); } + +test "Ascii lines" { + const testing = std.testing; + const alloc = testing.allocator; + + const impl: *Ascii = try .create(alloc, .{ + .@"line-min" = 5, + .@"line-max" = 20, + }); + 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); + + // Every emitted line respects the configured bounds. + var it = std.mem.splitSequence(u8, writer.buffered(), "\r\n"); + while (it.next()) |line| { + // The fixed buffer may end mid-line. + if (it.rest().len == 0) break; + try testing.expect(line.len >= 5); + try testing.expect(line.len <= 20); + } +} |
