From caffd9c1f8742a17431f7de0a2824d3fc02bfd72 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 28 Oct 2026 21:59:19 -0300 Subject: kitty z-index: a terminal image's z= goes with it, images go to the shell lowest first, and the tty hands z on to the outer terminal (under the text below 0); real clients recorded, replayed and run live z-index was lost: an ImagePlace had none, so every image went over the cells in hash-map order, a z<0 picture (a background under text) over the text and overlapping images in any order. ImagePlace.z now carries the placement's z (a virtual placement's from its placeholder target), TerminalImages sorts what it appends lowest z first (stable), and the tty's exactKittyPlacement sets vaxis's z_index, so a kitty terminal outside stacks them and puts z<0 under the text. The GUI draws them in that order but still over the cells: under the text there needs a glyph pass after the images (ui.frag's clear_bg_bit) in gui.zig's vertex buffer. Real clients: test/kitty_harness.zig's PARDES_KITTY_EXPLORE runs commands in the pane under script(1) and prints what pardes placed outside; kitten icat 0.49.2 (--align left/center/right, --place, --scale-up, --unicode-placeholder, --clear, file/memory/stream/detect), chafa 1.18.3, viu and yazi 26.9.1 all landed where they should. Their recorded traffic (test/kitty-fixtures/*.vt) replays through the core (test/kitty.zig) to the cells they drew live. The harness now also runs yazi live (shared memory, a virtual placement, placeholders), checks z=-1 outside, and runs a second pardes under a terminal without kitty graphics: no graphics command reaches it and the image is glyph art in all 32 cells. The harness empties NO_COLOR (zig build sets it, and vaxis drew no colour at all under the harness) by setenv, not unsetenv, which compacts the environment std captured. Also: tests that a pane's image follows pardes's own scrollback, an edited copy taller than its rows, a narrow pane's edge, Collapse and a cell-size change; and a knownGap: images do not survive a dump (Dump, Handover, a pane reopened from the Store), whose VT text has none. Co-Authored-By: Claude Opus 5.5 --- test/kitty.zig | 225 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 219 insertions(+), 6 deletions(-) (limited to 'test/kitty.zig') diff --git a/test/kitty.zig b/test/kitty.zig index a100e7a6..de4dd605 100644 --- a/test/kitty.zig +++ b/test/kitty.zig @@ -423,14 +423,26 @@ test "kitty: an image number gets an id, which the reply carries" { try std.testing.expect(r.storage(0).imageByNumber(5) != null); } -test "kitty: z-index orders images, and a negative one goes under the text" { +test "kitty: z-index travels with the image, and images are handed on lowest z first" { var r = try Rig.init(1); defer r.deinit(); - r.feed(0, "\x1b[1;1H" ++ apc("a=T,i=8,f=24,s=4,v=2,c=2,r=1,z=-1,q=2", rgb_b64) ++ "\x1b[1;1Htext"); - try std.testing.expectEqual(@as(usize, 1), r.placementCount(0)); - var it = r.storage(0).placements.iterator(); - try std.testing.expectEqual(@as(i32, -1), it.next().?.value_ptr.z); - try knownGap("pardes draws every terminal image over the cells, in storage order: an ImagePlace has no z-index, so z<0 (under the text) and the order between overlapping images are lost", @hasField(pardes.ImagePlace, "z")); + // Three overlapping at (0,0), given out of order; the virtual one's z + // is its placement's. + r.feed(0, "\x1b[1;1H" ++ apc("a=T,i=8,f=24,s=4,v=2,c=2,r=1,z=5,C=1,q=2", rgb_b64) ++ + apc("a=T,i=9,f=24,s=4,v=2,c=2,r=1,z=-7,C=1,q=2", rgb_b64) ++ + apc("a=T,i=10,f=24,s=4,v=2,c=2,r=1,C=1,q=2", rgb_b64) ++ + apc("a=T,U=1,i=11,f=24,s=4,v=2,c=1,r=1,z=3,q=2", rgb_b64) ++ + "\x1b[1;1H\x1b[38;2;0;0;11m\u{10EEEE}\u{0305}\u{0305}\x1b[39m"); + const drawn = try r.places(); + try std.testing.expectEqual(@as(usize, 4), drawn.len); + var zs: [4]i32 = undefined; + var ids: [4]u32 = undefined; + for (drawn, 0..) |place, i| { + zs[i] = place.?.z; + ids[i] = place.?.native.page; + } + try std.testing.expectEqualSlices(i32, &.{ -7, 0, 3, 5 }, &zs); + try std.testing.expectEqualSlices(u32, &.{ 9, 10, 11, 8 }, &ids); } /// A screen with two images in known places, for the deletes: @@ -1134,3 +1146,204 @@ test "kitty: fuzzed traffic neither crashes nor leaks, and every answer parses" } if (seeds != 24) std.debug.print("kitty fuzz: {d} seeds of {d} steps ok\n", .{ seeds, steps }); } + +// ---- An image's pane, as it lives ---- + +test "kitty: scrolled back by pardes, an image shows on its row; scrolled forward, it goes" { + var r = try Rig.init(1); + defer r.deinit(); + const pane = r.pane(0); + r.feed(0, "\x1b[1;1Hmarker" ++ apc("a=T,i=20,f=24,s=4,v=2,c=2,r=1,q=2", rgb_b64)); + var nl: [2 * 60]u8 = undefined; + for (0..60) |i| nl[2 * i ..][0..2].* = "\r\n".*; + r.feed(0, &nl); + try std.testing.expectEqual(@as(usize, 0), (try r.placesOf(20)).len); + // Back to the top of the history: the marker row is the body's first. + terminal.scrollGrid(pane, -1000); + const o = r.origin(0); + const drawn = try r.placesOf(20); + try std.testing.expectEqual(@as(usize, 1), drawn.len); + try std.testing.expectEqual(o.y, drawn[0].y); + try std.testing.expectEqual(o.x + 6, drawn[0].x); + terminal.followOutput(pane); + try std.testing.expectEqual(@as(usize, 0), (try r.placesOf(20)).len); +} + +test "kitty: an edited copy taller than the rows it covers pushes the images below it down" { + var r = try Rig.init(1); + defer r.deinit(); + const pane = r.pane(0); + r.feed(0, "\x1b[1;1Hone\r\ntwo\r\n\x1b[6;1H" ++ apc("a=T,i=21,f=24,s=4,v=2,c=2,r=1,q=2", rgb_b64)); + const o = r.origin(0); + try std.testing.expectEqual(o.y + 5, (try r.placesOf(21))[0].y); + // Row 1 ("two") edited into three lines. + pane.body.mode = .normal; + pane.ovl = .{ .row = 1, .rows = 1, .text = try gpa.dupe(u8, "two\nthree\nfour") }; + const drawn = try r.placesOf(21); + try std.testing.expectEqual(@as(usize, 1), drawn.len); + try std.testing.expectEqual(o.y + 7, drawn[0].y); +} + +test "kitty: an image wider than its pane is cut at the pane's edge, never drawn over the next one" { + var r = try Rig.init(3); + defer r.deinit(); + // The classic boot: two columns. Pane 0 is in the left one. + const rect = r.p.rects[0]; + const o = r.origin(0); + const right = rect.x + rect.w; + // 2x1 cells at its last-but-one column: one cell is the pane's. + const at = try std.fmt.allocPrint(r.frame.allocator(), "\x1b[1;{d}H", .{r.pane(0).cols}); + r.feed(0, at); + r.feed(0, apc("a=T,i=22,f=24,s=4,v=2,c=4,r=1,q=2", rgb_b64)); + const drawn = try r.placesOf(22); + try std.testing.expectEqual(@as(usize, 1), drawn.len); + try std.testing.expect(drawn[0].x + drawn[0].w <= o.x + r.pane(0).cols); + try std.testing.expect(drawn[0].x + drawn[0].w <= right); + // And the pixels it takes are only that cell's. + const g = drawn[0].native.geometry.?; + try std.testing.expect(g.dst.w <= @as(u32, drawn[0].w) * 10); +} + +test "kitty: a collapsed pane draws no image" { + var r = try Rig.init(3); + defer r.deinit(); + r.feed(1, "\x1b[1;1H" ++ apc("a=T,i=23,f=24,s=4,v=2,c=2,r=1,q=2", rgb_b64)); + try std.testing.expectEqual(@as(usize, 1), (try r.placesOf(23)).len); + try std.testing.expect(r.p.executeBuiltinLine(1, "Collapse")); + try std.testing.expectEqual(@as(usize, 0), (try r.placesOf(23)).len); + try std.testing.expect(r.p.executeBuiltinLine(1, "Collapse")); + try std.testing.expectEqual(@as(usize, 1), (try r.placesOf(23)).len); +} + +test "kitty: a new cell size (font zoom) redraws an image at the same cells, scaled to the new size" { + var r = try Rig.init(1); + defer r.deinit(); + r.feed(0, "\x1b[2;3H" ++ apc("a=T,i=24,f=24,s=4,v=2,c=2,r=1,q=2", rgb_b64)); + var drawn = try r.placesOf(24); + try std.testing.expectEqual(@as(u32, 20), drawn[0].native.geometry.?.dst.w); + r.p.update(.{ .resize = .{ .cols = 120, .rows = 40, .cell_pixels = .{ .w = 15, .h = 30 } } }); + r.drain(); + drawn = try r.placesOf(24); + const o = r.origin(0); + try std.testing.expectEqual(@as(usize, 1), drawn.len); + try std.testing.expectEqual(o.x + 2, drawn[0].x); + try std.testing.expectEqual(o.y + 1, drawn[0].y); + try std.testing.expectEqual(@as(u32, 30), drawn[0].native.geometry.?.dst.w); + try std.testing.expectEqual(@as(u32, 30), drawn[0].native.geometry.?.dst.h); +} + +test "kitty: a dumped and restored terminal keeps its images" { + var r = try Rig.init(1); + defer r.deinit(); + r.feed(0, "\x1b[1;1Hpicture" ++ apc("a=T,i=25,f=24,s=4,v=2,c=2,r=1,q=2", rgb_b64)); + try pardes.dump.dumpState(r.p); + const restored = try pardes.dump.restore(r.p, r.p.dump_out.?, "/tmp/pardes-kitty.dump.zon"); + defer restored.deinit(); + const pane = restored.panes[0].?; + const kept = pane.terminal != null and pane.terminal.?.vt.screens.active.kitty_images.placements.count() > 0; + try knownGap("a terminal's kitty images do not survive a dump (Dump/Load, Handover, a closed pane reopened from the Store): dumpPane writes the screen as VT text (ScreenFormatter), which has no images; ghostty's snapshot format carries them but is unstable (v1)", kept); +} + +// ---- Real clients' traffic ---- +// +// What kitten icat 0.49.2 and chafa 1.18.3 wrote to a pardes pane, recorded +// with script(1) between them and the pane (`PARDES_KITTY_EXPLORE` in +// test/kitty_harness.zig), a 64x64 PNG on a 118-column pane with 8x16 cells: +// their probes, then the image streamed. Replayed in 4 KiB reads, each must +// answer whole and draw its first row where it drew live. + +const Recorded = struct { + name: []const u8, + bytes: []const u8, + /// The pane cell its top-left landed on, live; null: draws nothing. + at: ?[2]u16, +}; + +const recorded = [_]Recorded{ + .{ .name = "icat --align=left", .bytes = @embedFile("kitty-fixtures/icat-left.vt"), .at = .{ 0, 0 } }, + .{ .name = "icat --align=center", .bytes = @embedFile("kitty-fixtures/icat-center.vt"), .at = .{ 55, 0 } }, + .{ .name = "icat --align=right", .bytes = @embedFile("kitty-fixtures/icat-right.vt"), .at = .{ 110, 0 } }, + .{ .name = "icat --place 10x5@20x3", .bytes = @embedFile("kitty-fixtures/icat-place.vt"), .at = .{ 21, 3 } }, + .{ .name = "icat --unicode-placeholder", .bytes = @embedFile("kitty-fixtures/icat-placeholder.vt"), .at = .{ 0, 0 } }, + .{ .name = "chafa -f kitty -s 8x4", .bytes = @embedFile("kitty-fixtures/chafa.vt"), .at = .{ 0, 0 } }, + .{ .name = "icat --clear", .bytes = @embedFile("kitty-fixtures/icat-clear.vt"), .at = null }, +}; + +test "kitty: recorded kitten icat and chafa traffic replays: answers whole, images where they were drawn" { + for (recorded) |rec| { + var r = try Rig.init(1); + defer r.deinit(); + r.p.update(.{ .resize = .{ .cols = 120, .rows = 40, .cell_pixels = .{ .w = 8, .h = 16 } } }); + r.drain(); + _ = try r.take(0); + r.feedSplit(0, rec.bytes, 4096); + const got = try r.take(0); + std.testing.expect(answersParse(got)) catch |err| { + std.debug.print("{s}: unparsable answer {f}\n", .{ rec.name, std.ascii.hexEscape(got, .lower) }); + return err; + }; + const drawn = try r.places(); + const want = rec.at orelse { + try std.testing.expectEqual(@as(usize, 0), drawn.len); + continue; + }; + const o = r.origin(0); + var top: ?pardes.ImagePlace = null; + for (drawn) |maybe| if (maybe) |place| { + if (top == null or place.y < top.?.y or (place.y == top.?.y and place.x < top.?.x)) top = place; + }; + const first = top orelse { + std.debug.print("{s}: nothing drawn\n", .{rec.name}); + return error.NothingDrawn; + }; + std.testing.expectEqual([2]u16{ o.x + want[0], o.y + want[1] }, [2]u16{ first.x, first.y }) catch |err| { + std.debug.print("{s}: drawn at {d},{d}, live at {d},{d}\n", .{ rec.name, first.x - o.x, first.y - o.y, want[0], want[1] }); + return err; + }; + } +} + +test "kitty: under a shell that draws no pixels, a 64x64 image over 8x4 cells is glyph art in every cell" { + var r = try Rig.init(1); + defer r.deinit(); + r.p.native_images = false; + r.p.update(.{ .resize = .{ .cols = 120, .rows = 40, .cell_pixels = .{ .w = 8, .h = 16 } } }); + r.drain(); + // Not the frame's arena: each frame resets that. + var data: std.heap.ArenaAllocator = .init(gpa); + defer data.deinit(); + const arena = data.allocator(); + const px = try arena.alloc(u8, 64 * 64 * 3); + for (0..64 * 64) |i| px[i * 3 ..][0..3].* = .{ 13, 77, 231 }; + const enc = try b64(arena, px); + for ([_]bool{ false, true }) |helix| { + if (helix) try std.testing.expect(r.p.executeBuiltinLine(0, "Theme helix")); + // As test/kitty_client.zig's `place` sends it: after text, in + // 1 KiB chunks. + var cmds: std.ArrayList(u8) = .empty; + try cmds.appendSlice(arena, "\x1b[H\x1b[2J\r\nANCHOR>"); + var at: usize = 0; + while (at < enc.len) : (at += 1024) { + const end = @min(enc.len, at + 1024); + const more: u8 = if (end < enc.len) '1' else '0'; + if (at == 0) + try cmds.print(arena, "\x1b_Ga=T,i=42,f=24,s=64,v=64,c=8,r=4,m={c};{s}\x1b\\", .{ more, enc[at..end] }) + else + try cmds.print(arena, "\x1b_Gm={c};{s}\x1b\\", .{ more, enc[at..end] }); + } + try cmds.appendSlice(arena, "\r\n"); + r.feed(0, cmds.items); + _ = try r.places(); + const s = &r.p.surface; + const o = r.origin(0); + var painted: usize = 0; + for (0..4) |dy| for (0..8) |dx| { + const cell = s.at(o.x + 7 + @as(u16, @intCast(dx)), o.y + 1 + @as(u16, @intCast(dy))); + if (cell.style.bg == .rgb and !cell.default) painted += 1; + }; + std.testing.expectEqual(@as(usize, 32), painted) catch |err| { + std.debug.print("helix theme {}: {d} cells painted\n", .{ helix, painted }); + return err; + }; + } +} -- cgit v1.3