diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-28 21:03:48 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-28 21:23:53 -0300 |
| commit | b250eb6135475699a945324c005890e8e1b4dd0b (patch) | |
| tree | c32f06f89cdbd501ba5d75dc8ba80ecebbe3c278 /test/kitty_client.zig | |
| parent | a48a825b06cc45947adaecf42073ed86af8b7a2d (diff) | |
| download | pardes-b250eb6135475699a945324c005890e8e1b4dd0b.tar.gz pardes-b250eb6135475699a945324c005890e8e1b4dd0b.zip | |
kitty graphics conformance for terminal panes: a core suite (test/kitty.zig, `zig build kitty-test`, in unit-test) and an end-to-end harness (`zig build kitty-harness`, tty and gui); four gaps it found fixed, one left
The core suite feeds a pane's output as pty reads do and checks what the program reads back, ghostty's image storage and the ImagePlaces a frame draws: queries and their errors, q=0/1/2, f=24/32/100 and o=z, chunked m=1 split at every read size, t=f/t=t/t=s, a=T and C=1 cursor movement, a=p by placement id with source rects and re-puts, image numbers, every d= delete (a/A i/I n/N c/C p/P q/Q x/X y/Y z/Z r/R) and a delete abandoning a chunked load, Unicode placeholders over two rows with implicit columns and a placement named in the underline colour, scroll/resize/reflow, the alternate screen, clear (ED 2 + ED 3), replies per pane and never in the cells, many replies from one read, kitty before DA, CAN/SUB, an oversized image, 300 images against the storage limit, animations and relative placements.
The harness runs test/kitty_client.zig in a pane's bash (query, a 16-chunk 8x4-cell place, the three media, quiet levels, CAN, show then delete) and kitten icat (stream and detect) and chafa when installed. Outside, the tty arm is a kitty terminal (a ghostty-vt emulator): the image pardes re-emits lands at the cell after ANCHOR>, 8x4 cells a row at a time, the client's own commands and replies never pass through, and d=I frees what was sent. The gui arm reads the GPU capture.
Fixed: kitty file, temporary-file and shared-memory media (libghostty defaults to direct only; now allWithTempDir like ghostty and kitty); replies past 256 bytes of one read were cut mid-sequence (4 KiB now, an answer that does not fit dropped whole and counted); animations never played (TerminalImages draws renderData(), ticks ImageStorage.animationTick on the core clock, nextWake wakes for the next frame); relative placements (P=/Q=/H=/V=) were not drawn (resolveChain, as ghostty's renderer). image.decode no longer jumps through stb_image's null allocator before image.start.
Known gap (a knownGap test): no z-index; every image is drawn over the cells in storage order.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'test/kitty_client.zig')
| -rw-r--r-- | test/kitty_client.zig | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/test/kitty_client.zig b/test/kitty_client.zig new file mode 100644 index 00000000..23c7587f --- /dev/null +++ b/test/kitty_client.zig @@ -0,0 +1,150 @@ +//! A kitty graphics client for test/kitty_harness.zig: run in a pardes +//! terminal pane, it plays one case of the protocol the way yazi or +//! `kitten icat` do (raw mode, a batch of commands, then reads what comes +//! back) and records every byte its pty gave it. +//! +//! pardes-kitty-client <case> <out> +//! +//! After the case's commands it sends a sync query (`i=999`); replies come +//! in order, so all the case's are in before the sync's. `<out>` gets the +//! bytes read, and `<out>.done` is made last, which the harness waits for. +const std = @import("std"); +const c = std.c; + +// 64x64 RGB, one colour: the harness looks for it in the outer terminal's +// image store and in the GUI's capture. +pub const side = 64; +pub const colour = [3]u8{ 13, 77, 231 }; +const sync = "\x1b_Gi=999,s=1,v=1,a=q,t=d,f=24;AAAA\x1b\\"; + +pub fn main(init: std.process.Init) !void { + var arena_state: std.heap.ArenaAllocator = .init(std.heap.page_allocator); + defer arena_state.deinit(); + const arena = arena_state.allocator(); + const args = try init.minimal.args.toSlice(arena); + if (args.len != 3) return error.BadArgs; + const case = args[1]; + const out = try arena.dupeSentinel(u8, args[2], 0); + + var saved: c.termios = undefined; + if (c.tcgetattr(0, &saved) != 0) return error.NotATerminal; + var raw = saved; + raw.lflag.ICANON = false; + raw.lflag.ECHO = false; + raw.cc[@backingInt(c.V.MIN)] = 0; + raw.cc[@backingInt(c.V.TIME)] = 1; + _ = c.tcsetattr(0, .NOW, &raw); + defer _ = c.tcsetattr(0, .NOW, &saved); + + var cmds: std.ArrayList(u8) = .empty; + try play(arena, case, &cmds); + try cmds.appendSlice(arena, sync); + try writeAll(cmds.items); + + // Read until the sync's answer, or 5 s. + var got: std.ArrayList(u8) = .empty; + var buf: [4096]u8 = undefined; + var waited: usize = 0; + while (waited < 50 and std.mem.indexOf(u8, got.items, "\x1b_Gi=999;") == null) { + const n = c.read(0, &buf, buf.len); + if (n > 0) try got.appendSlice(arena, buf[0..@intCast(n)]) else waited += 1; + } + // A little longer, for anything that should not have come. + for (0..3) |_| { + const n = c.read(0, &buf, buf.len); + if (n > 0) try got.appendSlice(arena, buf[0..@intCast(n)]); + } + // t=f's file stays the client's to remove (a terminal deletes only t=t's). + _ = c.unlink(try std.fmt.allocPrintSentinel(arena, "/tmp/kc-{d}.rgb", .{c.getpid()}, 0)); + try writeFile(out, got.items); + try writeFile(try std.fmt.allocPrintSentinel(arena, "{s}.done", .{out}, 0), ""); +} + +fn play(arena: std.mem.Allocator, case: []const u8, cmds: *std.ArrayList(u8)) !void { + const pixels = try solid(arena); + const enc = std.base64.standard.Encoder; + const b64 = try arena.alloc(u8, enc.calcSize(pixels.len)); + _ = enc.encode(b64, pixels); + const pid = c.getpid(); + + if (std.mem.eql(u8, case, "query")) { + // yazi's and chafa's probe: a graphics query, then DA to end it. + try cmds.appendSlice(arena, "\x1b_Gi=31,s=1,v=1,a=q,t=d,f=24;AAAA\x1b\\\x1b[c"); + } else if (std.mem.eql(u8, case, "place")) { + // A line to find, then the image right after it: chunked in 1 KiB + // pieces, as kitten icat sends a big one, 8x4 cells. + try cmds.appendSlice(arena, "\r\nANCHOR>"); + var at: usize = 0; + var first = true; + while (at < b64.len) : (at += 1024) { + const end = @min(b64.len, at + 1024); + const more: u8 = if (end < b64.len) '1' else '0'; + if (first) + try cmds.print(arena, "\x1b_Ga=T,i=42,f=24,s={d},v={d},c=8,r=4,m={c};{s}\x1b\\", .{ side, side, more, b64[at..end] }) + else + try cmds.print(arena, "\x1b_Gm={c};{s}\x1b\\", .{ more, b64[at..end] }); + first = false; + } + try cmds.appendSlice(arena, "\r\n"); + } else if (std.mem.eql(u8, case, "media")) { + const file = try std.fmt.allocPrintSentinel(arena, "/tmp/kc-{d}.rgb", .{pid}, 0); + const temp = try std.fmt.allocPrintSentinel(arena, "/tmp/tty-graphics-protocol-kc-{d}.rgb", .{pid}, 0); + const shm_name = try std.fmt.allocPrintSentinel(arena, "/kc-{d}", .{pid}, 0); + try writeFile(file, pixels); + try writeFile(temp, pixels); + const fd = shm_open(shm_name, @bitCast(c.O{ .ACCMODE = .RDWR, .CREAT = true, .TRUNC = true }), 0o600); + if (fd < 0) return error.ShmOpenFailed; + if (c.write(fd, pixels.ptr, pixels.len) != @as(isize, @intCast(pixels.len))) return error.WriteFailed; + _ = c.close(fd); + for ([_]struct { u8, u32, []const u8 }{ .{ 'f', 51, file }, .{ 't', 52, temp }, .{ 's', 53, shm_name } }) |m| { + const path = try arena.alloc(u8, enc.calcSize(m[2].len)); + _ = enc.encode(path, m[2]); + try cmds.print(arena, "\x1b_Ga=t,t={c},i={d},f=24,s={d},v={d};{s}\x1b\\", .{ m[0], m[1], side, side, path }); + } + } else if (std.mem.eql(u8, case, "quiet")) { + // q=1 hides the OK, q=2 the error; q=0's error is heard. + try cmds.print(arena, "\x1b_Ga=t,i=61,f=24,s={d},v={d},q=1;{s}\x1b\\", .{ side, side, b64 }); + try cmds.appendSlice(arena, "\x1b_Ga=t,i=62,f=24,s=2,v=2,q=2;AAAA\x1b\\"); + try cmds.appendSlice(arena, "\x1b_Ga=t,i=63,f=24,s=2,v=2,q=0;AAAA\x1b\\"); + } else if (std.mem.eql(u8, case, "cancel")) { + // CAN in the middle of the payload: nothing runs, the text goes on. + try cmds.print(arena, "\x1b_Ga=T,i=64,f=24,s={d},v={d};{s}\x18CANCELLED-OK\r\n", .{ side, side, b64[0..512] }); + } else if (std.mem.eql(u8, case, "show")) { + try cmds.print(arena, "\x1b_Ga=T,i=65,f=24,s={d},v={d},c=8,r=4,q=2;{s}\x1b\\\r\n", .{ side, side, b64 }); + } else if (std.mem.eql(u8, case, "delete")) { + // Run after `show`, in the same pane: image 65 is still stored. + try cmds.appendSlice(arena, "\x1b_Ga=d,d=I,i=65\x1b\\"); + } else return error.UnknownCase; +} + +extern "c" fn shm_open(name: [*:0]const u8, flag: c_int, mode: c.mode_t) c_int; + +fn solid(arena: std.mem.Allocator) ![]u8 { + const px = try arena.alloc(u8, side * side * 3); + for (0..side * side) |i| px[i * 3 ..][0..3].* = colour; + return px; +} + +fn writeAll(bytes: []const u8) !void { + var at: usize = 0; + while (at < bytes.len) { + const n = c.write(1, bytes[at..].ptr, bytes.len - at); + if (n < 0) { + if (c.errno(n) == .INTR or c.errno(n) == .AGAIN) continue; + return error.WriteFailed; + } + at += @intCast(n); + } +} + +fn writeFile(path: [:0]const u8, bytes: []const u8) !void { + const fd = c.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(c.mode_t, 0o644)); + if (fd < 0) return error.OpenFailed; + defer _ = c.close(fd); + var at: usize = 0; + while (at < bytes.len) { + const n = c.write(fd, bytes[at..].ptr, bytes.len - at); + if (n <= 0) return error.WriteFailed; + at += @intCast(n); + } +} |
