summaryrefslogtreecommitdiff
path: root/src/terminal.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-28 21:03:48 -0300
committerGabriel Schneider <[email protected]>2026-10-28 21:23:53 -0300
commitb250eb6135475699a945324c005890e8e1b4dd0b (patch)
treec32f06f89cdbd501ba5d75dc8ba80ecebbe3c278 /src/terminal.zig
parenta48a825b06cc45947adaecf42073ed86af8b7a2d (diff)
downloadpardes-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 'src/terminal.zig')
-rw-r--r--src/terminal.zig45
1 files changed, 40 insertions, 5 deletions
diff --git a/src/terminal.zig b/src/terminal.zig
index 77ee9b44..ce41bd95 100644
--- a/src/terminal.zig
+++ b/src/terminal.zig
@@ -146,8 +146,14 @@ pub const MarkedHandler = if (enabled) struct {
pub const State = struct {
vt: VtSlot,
stream: StreamSlot,
- reply: [256]u8 = undefined,
+ /// The emulator's answers to its program (DSR, DA, kitty graphics, ...)
+ /// from one read of its output, written to its pty after it. Room for
+ /// a read's worth of kitty answers (yazi or `kitten icat` asking about
+ /// a batch of images in one write); an answer that does not fit whole
+ /// is dropped whole, never cut short (`reply_dropped` counts them).
+ reply: [4096]u8 = undefined,
reply_len: u16 = 0,
+ reply_dropped: u32 = 0,
/// The grid cell the program last heard the mouse at, so motion is
/// reported once per cell.
mouse_cell: if (enabled) ?ghostty_vt.Coordinate else void = if (enabled) null else {},
@@ -315,6 +321,16 @@ pub fn deinitPendingCommand(pane: *Pane) void {
pane.pending_command = .{};
}
+/// Where a kitty client puts its temporary files: `$TMPDIR`, else /tmp
+/// (which ghostty accepts whatever this says). The environment block lives
+/// as long as the process and pardes never edits it (ChildEnv.exportVar).
+fn tempDir() []const u8 {
+ if (comptime !pardes.hosted) return "/tmp";
+ const dir = std.c.getenv("TMPDIR") orelse return "/tmp";
+ const path = std.mem.span(dir);
+ return if (path.len > 0 and path[0] == '/') path else "/tmp";
+}
+
pub fn terminalIo() std.Io {
return if (comptime !pardes.hosted)
std.Io.failing
@@ -606,6 +622,13 @@ pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane {
.rows = rows,
.max_scrollback_bytes = 16 * 1024 * 1024,
.kitty_image_storage_limit = if (images_enabled) TerminalImages.storage_limit else 0,
+ // Every kitty medium, as ghostty and kitty take them: a file (t=f),
+ // a temporary file it then deletes (t=t, in the temp directory and
+ // named as one) and shared memory (t=s), beside direct data. libghostty
+ // defaults to direct alone, which `kitten icat` and timg detect and
+ // fall back from, base64 through the pty. What the program can read
+ // it could send itself: it runs as the user, under pardes.
+ .kitty_image_loading_limits = if (images_enabled) .allWithTempDir(tempDir()) else .direct,
});
state.stream = .init(.{ .handler = .{ .inner = state.vt.vtHandler() }, .allocator = gpa });
state.reply_len = 0;
@@ -677,7 +700,6 @@ fn ingest(pane: *Pane, bytes: []const u8) void {
pane.terminal.?.stream.nextSlice(bytes);
}
-
test "replay ownership is terminal-only and construction rolls back on allocation failure" {
const Case = struct {
fn run(gpa: std.mem.Allocator, terminal: bool) !void {
@@ -2808,9 +2830,14 @@ pub fn redo(p: *Pardes, pane: *Pane) void {
pub fn ptyReport(handler: *ghostty_vt.TerminalStream.Handler, data: []const u8) void {
const state: *State = @alignCast(@fieldParentPtr("vt", handler.terminal));
const room = state.reply.len - state.reply_len;
- const n = @min(room, data.len);
- @memcpy(state.reply[state.reply_len..][0..n], data[0..n]);
- state.reply_len += @intCast(n);
+ // A cut answer is worse than none: the program would read the start of
+ // a sequence and wait, or misparse what follows.
+ if (data.len > room) {
+ state.reply_dropped +|= 1;
+ return;
+ }
+ @memcpy(state.reply[state.reply_len..][0..data.len], data);
+ state.reply_len += @intCast(data.len);
}
const DeviceAttrs = @typeInfo(@typeInfo(@typeInfo(
@@ -2838,6 +2865,14 @@ fn syncPixels(p: *const Pardes, pane: *Pane) void {
vt.height_px = @as(u32, vt.rows) * p.cell_pixels.h;
}
+/// When the pane's next kitty animation frame is due (ms on the core's
+/// clock), as its last drawn frame left it; null when nothing plays.
+pub fn animationDueMs(pane: *const Pane) ?u64 {
+ if (comptime !images_enabled) return null;
+ const state = pane.terminal orelse return null;
+ return state.images.animation_due_ms;
+}
+
/// The Petscii builtin on a terminal: its kitty graphics as glyph art,
/// or back to pixels where the shell draws pixels.
pub fn togglePetscii(pane: *Pane) void {