From e4fbb97a506ca2a4b469b6e2e13ecba6e516272c Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 16 Nov 2026 23:37:14 -0300 Subject: term: the terminal's own files move into src/term/ (terminal.zig, screen_state.zig, echo.zig, TerminalImages.zig), as they are: names and signatures kept, only the import paths change (theirs to ../, pardes.zig's, panes.zig's, tape.zig's, replica.zig's and Kind.zig's to term/), the comments that name them by path follow, and the embedded sources serve terminal.zig at src/term/terminal.zig; the first step of docs/next-terminal.md's Phase 0, a move, nothing else --- src/term/TerminalImages.zig | 380 +++++ src/term/echo.zig | 834 +++++++++++ src/term/screen_state.zig | 1664 +++++++++++++++++++++ src/term/terminal.zig | 3411 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 6289 insertions(+) create mode 100644 src/term/TerminalImages.zig create mode 100644 src/term/echo.zig create mode 100644 src/term/screen_state.zig create mode 100644 src/term/terminal.zig (limited to 'src/term') diff --git a/src/term/TerminalImages.zig b/src/term/TerminalImages.zig new file mode 100644 index 00000000..6894945c --- /dev/null +++ b/src/term/TerminalImages.zig @@ -0,0 +1,380 @@ +//! A terminal pane's kitty graphics: the images its program sent (yazi's +//! previews, `kitten icat`) drawn where it placed them. ghostty-vt keeps +//! the images and placements; this reads them each frame, cuts each one +//! into the cell rows it covers, clipped to the rows the pane shows, and +//! hands them on as placements, each at its exact pixels: a frontend draws +//! them, or draws them as glyph art (Art.zig), as an image pane's. +//! Both kinds of placement are drawn: one on a cell (`a=T`/`a=p`), which +//! moves with the text it was put on, and a virtual one (`U=1`) shown +//! through the U+10EEEE placeholder cells the program prints. +const std = @import("std"); +const ghostty_vt = @import("ghostty-vt"); +const pardes = @import("../pardes.zig"); +const zstbi = @import("zstbi"); +const wuffs = @import("wuffs"); + +const TerminalImages = @This(); +const gfx = ghostty_vt.kitty.graphics; +const PixelRect = @import("../surface.zig").RectPx; + +/// The bytes ghostty-vt may hold for one screen's images (it evicts the +/// oldest past this): a 4K picture in RGBA, with room for a second. +pub const storage_limit = 64 * 1024 * 1024; + +/// This pane asks for its images as glyph art even where pixels could be +/// drawn (the Petscii builtin). +petscii: bool = false, +/// Pixels in RGBA for images ghostty-vt holds in another format (RGB, +/// grey): only for images on screen, dropped the frame they are not. +converted: std.AutoHashMapUnmanaged(u32, Converted) = .empty, +/// When the next frame of a playing animation (a=a) is due, in ms on the +/// core's clock (`Pardes.stepped_ns`): the core wakes for it (nextWake). +animation_due_ms: ?u64 = null, + +const Converted = struct { generation: u64, rgba: []u8, used: bool = true }; + +/// One image's rectangle in the pane, in pixels from the viewport's top +/// left corner (above or left of it is negative). +const Rect = struct { x: i64, y: i64, w: i64, h: i64 }; + +/// The runs of Kitty placeholder cells between two pins, one row's run at a +/// time, as ghostty's own `kitty.graphics.unicode.placementIterator` gave +/// them before it moved into its RenderState, which pardes does not keep. +const PlaceholderRuns = struct { + rows: ghostty_vt.PageList.RowIterator, + row: ?ghostty_vt.Pin, + /// Where the scan of `row` resumes: a run ended by an incompatible + /// placeholder starts the next one there. + x: usize = 0, + + fn init(top: ghostty_vt.Pin, bottom: ghostty_vt.Pin) PlaceholderRuns { + var rows = top.rowIterator(.right_down, bottom); + const row = rows.next(); + return .{ .rows = rows, .row = row }; + } + + fn next(self: *PlaceholderRuns) ?gfx.unicode.Placement { + while (self.row) |row| { + if (!row.rowAndCell().row.kitty_virtual_placeholder) { + self.advance(); + continue; + } + var run: ?gfx.unicode.IncompletePlacement = null; + var first: usize = 0; + const cells = row.cells(.all); + while (self.x < cells.len) : (self.x += 1) { + const cell = &cells[self.x]; + if (cell.codepoint() != gfx.unicode.placeholder) { + if (run) |done| return complete(row, first, done); + continue; + } + const style = row.style(cell); + const curr: gfx.unicode.IncompletePlacement = .init(&style, row.grapheme(cell) orelse &.{}); + if (run) |*prev| { + if (!prev.append(&curr)) return complete(row, first, prev.*); + } else { + run = curr.start(); + first = self.x; + } + } + self.advance(); + if (run) |done| return complete(row, first, done); + } + return null; + } + + fn advance(self: *PlaceholderRuns) void { + self.row = self.rows.next(); + self.x = 0; + } + + fn complete(row: ghostty_vt.Pin, x: usize, run: gfx.unicode.IncompletePlacement) gfx.unicode.Placement { + var pin = row; + pin.x = @intCast(x); + return .{ + .pin = pin, + .image_id = run.imageId(), + .placement_id = run.placement_id orelse 0, + .col = run.col.?, + .row = run.row.?, + .width = run.width, + .height = 1, + }; + } +}; + +pub fn deinit(ti: *TerminalImages, gpa: std.mem.Allocator) void { + var conv = ti.converted.valueIterator(); + while (conv.next()) |c| gpa.free(c.rgba); + ti.converted.deinit(gpa); +} + +/// Accept PNG (`f=100`) as well as raw pixels: ghostty-vt built as a +/// library decodes none itself. wuffs does it (ghostty's, linked for it). +pub fn installPngDecoder() void { + ghostty_vt.sys.decode_png = &decodePng; +} + +/// The longest side a PNG is kept at: a pane is at most a screenful of +/// cells, and a multi-thousand-pixel picture only costs its memory. +const max_dim: u32 = 1280; + +/// stb_image_resize has its allocator (`start`): before, a PNG is refused. +var started = false; + +/// Once at startup and exit, with what the resize allocates from. +pub fn start(gpa: std.mem.Allocator) void { + // Refused only when started twice with a resize still alive: PNGs then stay refused. + zstbi.init(gpa) catch return; + started = true; +} +pub fn stop() void { + started = false; + zstbi.deinit(); +} + +fn decodePng(gpa: std.mem.Allocator, bytes: []const u8) ghostty_vt.sys.DecodeError!ghostty_vt.sys.Image { + if (!started) return error.InvalidData; + // Bounded before decoding: a few KB of PNG can name gigapixels. The + // signature, then IHDR first: its length, type, width and height. + if (bytes.len < 24 or !std.mem.startsWith(u8, bytes, "\x89PNG\r\n\x1a\n") or !std.mem.eql(u8, bytes[12..16], "IHDR")) return error.InvalidData; + const w = std.mem.readInt(u32, bytes[16..20], .big); + const h = std.mem.readInt(u32, bytes[20..24], .big); + if (@as(u64, w) * h > pardes.memory.limits.image_decode_pixels) return error.InvalidData; + const got = wuffs.png.decode(gpa, bytes) catch return error.InvalidData; + if (got.data.len != @as(usize, got.width) * got.height * 4) { + gpa.free(got.data); + return error.InvalidData; + } + const longest = @max(got.width, got.height); + if (longest <= max_dim) return .{ .width = got.width, .height = got.height, .data = got.data }; + defer gpa.free(got.data); + const img: zstbi.Image = .{ .data = got.data, .width = got.width, .height = got.height, .num_components = 4, .bytes_per_component = 1, .bytes_per_row = got.width * 4, .is_hdr = false }; + var scaled = img.resize(@intCast(@max(1, @as(u64, got.width) * max_dim / longest)), @intCast(@max(1, @as(u64, got.height) * max_dim / longest))) catch return error.InvalidData; + defer scaled.deinit(); + return .{ .width = scaled.width, .height = scaled.height, .data = gpa.dupe(u8, scaled.data) catch return error.InvalidData }; +} + +/// Draw the active screen's images. `rows_of[v]` is the body row the +/// viewport's row `v` is shown on, if it is shown; `cols` how many of its +/// columns the body shows, from `tx`. +pub fn draw( + ti: *TerminalImages, + p: *pardes.Pardes, + s: *pardes.Surface, + vt: *ghostty_vt.Terminal, + pane_id: u8, + serial: u32, + tx: u16, + body_y: u16, + cols: u16, + rows_of: []const ?u16, +) void { + var conv = ti.converted.valueIterator(); + while (conv.next()) |c| c.used = false; + defer ti.prune(p.gpa); + + const screen = vt.screens.active; + const storage = &screen.kitty_images; + // Animations step on the core's clock, as this frame shows them; the + // shown frame is `renderData()` (a new `generation` each step, which + // the caches key on). + const now_ms = p.clock.stepped_ns / std.time.ns_per_ms; + ti.animation_due_ms = if (storage.animationTick(vt.io(), now_ms)) |delay| now_ms +| delay else null; + const pages = &screen.pages; + const top = pages.getTopLeft(.viewport); + const bottom = pages.getBottomRight(.viewport) orelse return; + const cw: u32 = @max(1, p.view().cell_pixels.w); + const ch: u32 = @max(1, p.view().cell_pixels.h); + const ctx: Ctx = .{ .ti = ti, .p = p, .pane = pane_id, .serial = serial, .tx = tx, .body_y = body_y, .cols = cols, .rows_of = rows_of, .cw = cw, .ch = ch }; + // Handed on lowest z first (the storage's order is a hash map's), the + // order a shell draws them in; equal z keeps the order they came in. + const first = s.placements.items.len; + defer std.sort.insertion(pardes.Placement, s.placements.items[first..], {}, struct { + fn lessThan(_: void, a: pardes.Placement, b: pardes.Placement) bool { + return a.z < b.z; + } + }.lessThan); + + // Virtual placements: shown through the placeholder cells, one run of + // them on a row at a time. The placeholders themselves are not text. + var runs: PlaceholderRuns = .init(top, bottom); + while (runs.next()) |run| { + const at = pages.pointFromPin(.viewport, run.pin) orelse continue; + const v = at.viewport.y; + if (v >= rows_of.len) continue; + const row = rows_of[v] orelse continue; + for (@as(usize, at.viewport.x)..@as(usize, at.viewport.x) + run.width) |x| { + if (x >= cols) break; + const cell = s.at(tx + @as(u16, @intCast(x)), body_y + row); + cell.text[0] = ' '; + cell.len = 1; + } + const img = storage.imageById(run.image_id) orelse continue; + const target = storage.placeholderTarget(run.image_id, run.placement_id) orelse continue; + const rp = run.renderPlacement(&target.placement, &img, cw, ch) catch continue; + if (rp.dest_width == 0 or rp.dest_height == 0 or rp.source_width == 0 or rp.source_height == 0) continue; + ctx.place(img, target.placement.z, .{ .x = rp.source_x, .y = rp.source_y, .w = rp.source_width, .h = rp.source_height }, .{ + .x = @as(i64, at.viewport.x) * cw + rp.offset_x, + .y = @as(i64, v) * ch + rp.offset_y, + .w = rp.dest_width, + .h = rp.dest_height, + }); + } + + // Placements on a cell: they move with the text they were put on, and + // go where it goes, above the viewport or off the screen. + const origin = pages.pointFromPin(.screen, top) orelse return; + var it = storage.placements.iterator(); + while (it.next()) |entry| { + const placed = entry.value_ptr.*; + // A placement relative to another (P=/Q=) sits at its chain's root + // plus the chain's cell offsets (H=/V=), as ghostty's renderer puts it. + var dx: i64 = 0; + var dy: i64 = 0; + const pin = switch (placed.location) { + .pin => |pin| pin, + .virtual => continue, + .relative => |rel| root: { + const chain = storage.resolveChain(rel) orelse continue; + dx = chain.horizontal_offset; + dy = chain.vertical_offset; + break :root switch (chain.root.location) { + .pin => |pin| pin, + // ponytail: a chain rooted on a virtual placement (shown + // through placeholders) is not drawn; ghostty anchors it at + // the placeholder run. + .virtual, .relative => continue, + }; + }, + }; + const img = storage.images.get(entry.key_ptr.image_id) orelse continue; + const at = pages.pointFromPin(.screen, pin.*) orelse continue; + const size = placed.pixelSize(img, vt); + const sx = @min(placed.source_x, img.width); + const sy = @min(placed.source_y, img.height); + const sw = if (placed.source_width > 0) @min(placed.source_width, img.width - sx) else img.width - sx; + const sh = if (placed.source_height > 0) @min(placed.source_height, img.height - sy) else img.height - sy; + if (size.width == 0 or size.height == 0 or sw == 0 or sh == 0) continue; + ctx.place(img, placed.z, .{ .x = sx, .y = sy, .w = sw, .h = sh }, .{ + .x = (@as(i64, at.screen.x) + dx) * cw + placed.x_offset, + .y = (@as(i64, at.screen.y) - @as(i64, origin.screen.y) + dy) * ch + placed.y_offset, + .w = size.width, + .h = size.height, + }); + } +} + +const Ctx = struct { + ti: *TerminalImages, + p: *pardes.Pardes, + pane: u8, + serial: u32, + tx: u16, + body_y: u16, + cols: u16, + rows_of: []const ?u16, + cw: u32, + ch: u32, + + /// Draw `src` of `img` over `dst`, one cell row of it at a time: a row + /// is shown, or not, or shown elsewhere, as the body lays it out. + fn place(ctx: Ctx, img: gfx.Image, z: i32, src: PixelRect, dst: Rect) void { + const cw: i64 = ctx.cw; + const ch: i64 = ctx.ch; + const x0 = @max(dst.x, 0); + const x1 = @min(dst.x + dst.w, @as(i64, ctx.cols) * cw); + if (x1 <= x0) return; + const rgba = ctx.ti.pixelsOf(ctx.p.gpa, img) orelse return; + const first_row = @divFloor(@max(dst.y, 0), ch); + const last_row = @min(@divFloor(dst.y + dst.h - 1, ch), @as(i64, @intCast(ctx.rows_of.len)) - 1); + var v = first_row; + while (v <= last_row) : (v += 1) { + const row = ctx.rows_of[@intCast(v)] orelse continue; + const y0 = @max(dst.y, v * ch); + const y1 = @min(dst.y + dst.h, (v + 1) * ch); + if (y1 <= y0) continue; + // The source pixels under this piece, by the same scale. + const piece: PixelRect = .{ + .x = src.x + @as(u32, @intCast(@divFloor((x0 - dst.x) * src.w, dst.w))), + .y = src.y + @as(u32, @intCast(@divFloor((y0 - dst.y) * src.h, dst.h))), + .w = @max(1, @as(u32, @intCast(@divFloor((x1 - x0) * src.w, dst.w)))), + .h = @max(1, @as(u32, @intCast(@divFloor((y1 - y0) * src.h, dst.h)))), + }; + const c0: u16 = @intCast(@divFloor(x0, cw)); + const c1: u16 = @intCast(@divFloor(x1 + cw - 1, cw)); + _ = ctx.p.appendPlacement(.{ + .pane = ctx.pane, + .raster = .of(rgba, img.width, img.height), + .src = piece, + .dst = .{ .x = ctx.tx + c0, .y = ctx.body_y + row, .w = c1 - c0, .h = 1 }, + .at = .{ + .x = @intCast(x0 - @as(i64, c0) * cw), + .y = @intCast(y0 - v * ch), + .w = @intCast(x1 - x0), + .h = @intCast(y1 - y0), + }, + .key = pardes.Placement.keyOf(ctx.serial, img.id, @truncate(img.generation)), + .z = z, + .art = .{ .glyph = ctx.ti.petscii }, + }); + } + } +}; + +/// The image's pixels as RGBA: its own bytes when it is held so. +fn pixelsOf(ti: *TerminalImages, gpa: std.mem.Allocator, img: gfx.Image) ?[]const u8 { + const n = @as(usize, img.width) * img.height; + if (n == 0) return null; + const data = img.renderData().bytes() orelse return null; + if (img.format == .rgba) return if (data.len == n * 4) data else null; + const gop = ti.converted.getOrPut(gpa, img.id) catch return null; + if (gop.found_existing) { + if (gop.value_ptr.generation == img.generation) { + gop.value_ptr.used = true; + return gop.value_ptr.rgba; + } + gpa.free(gop.value_ptr.rgba); + _ = ti.converted.remove(img.id); + return ti.pixelsOf(gpa, img); + } + const out = convert(gpa, img, n) orelse { + _ = ti.converted.remove(img.id); + return null; + }; + gop.value_ptr.* = .{ .generation = img.generation, .rgba = out }; + return out; +} + +fn convert(gpa: std.mem.Allocator, img: gfx.Image, n: usize) ?[]u8 { + const depth: usize = switch (img.format) { + .rgb => 3, + .gray_alpha => 2, + .gray => 1, + else => return null, + }; + const data = img.renderData().bytes() orelse return null; + if (data.len != n * depth) return null; + const out = gpa.alloc(u8, n * 4) catch return null; + for (0..n) |i| { + const px = data[i * depth ..][0..depth]; + out[i * 4 ..][0..4].* = switch (depth) { + 3 => .{ px[0], px[1], px[2], 255 }, + 2 => .{ px[0], px[0], px[0], px[1] }, + else => .{ px[0], px[0], px[0], 255 }, + }; + } + return out; +} + +/// Drop what this frame did not draw: the caches hold only what is shown. +fn prune(ti: *TerminalImages, gpa: std.mem.Allocator) void { + while (true) { + var conv = ti.converted.iterator(); + const stale = while (conv.next()) |entry| { + if (!entry.value_ptr.used) break entry.key_ptr.*; + } else break; + gpa.free(ti.converted.get(stale).?.rgba); + _ = ti.converted.remove(stale); + } +} diff --git a/src/term/echo.zig b/src/term/echo.zig new file mode 100644 index 00000000..4f27a08f --- /dev/null +++ b/src/term/echo.zig @@ -0,0 +1,834 @@ +//! Predicted echo for a replica's terminal panes, mosh's way +//! (docs/next-daemon.md, "Phase 6: predicted echo notes"). A key typed into +//! a terminal goes to the authority, and its echo comes back as screen state +//! a round trip later: this shows it at once. +//! +//! Presentation only. The overlay is the replica's own (`Pardes.echo`, a +//! `.local` field the rollback copy skips), painted over a terminal's body +//! after it is drawn (`paint`, from body_layer.renderBody, which the tty and +//! the GUI share). The core's emulator stays the authority's, and no digest +//! sees any of this. +//! +//! What is predicted: a printable character at the cursor, backspace, and +//! left and right, in tty mode on the primary screen at the live bottom. Any +//! other input this replica could not apply itself (Enter, Tab, a chord, a +//! paste, a click) stops prediction until the authority has confirmed it and +//! its echo has had `grace` to come back: predictions start again from the +//! screen's own cursor. So a key typed after Tab waits a round trip; it is +//! never drawn where the completion was not. +//! +//! Judged as mosh does, once the authority confirmed the key and `grace` +//! passed: at a delta, a cell the screen shows goes, and one it does not is +//! a misprediction, and every prediction goes. With no delta (nothing +//! changed: no echo), a frame judges it `stale` later. Shown at once at a +//! shell's prompt (pardes's OSC 133 marks say its line editor echoes); +//! elsewhere only once an echo was confirmed there since the last key that +//! was not predicted (`cat`, a shell without marks), and underlined; never +//! after a shown misprediction until an echo is confirmed again. A password +//! prompt or a program that ignores input confirms none, so nothing typed +//! there is ever shown. +const std = @import("std"); +const pardes = @import("../pardes.zig"); +const terminal = pardes.panes.terminal; +const Pardes = pardes.Pardes; +const Pane = pardes.Pane; +const Surface = pardes.Surface; +const Key = pardes.Key; + +const ms = std.time.ns_per_ms; +/// After the authority confirmed a key, how long its echo has to come back +/// before a delta that does not show it makes a misprediction (mosh's +/// ECHO_TIMEOUT): the shell's echo, then the authority's next frame. +// ponytail: fixed; a loaded host's later echo makes a drawn character go and +// come back. The echo delay measured per terminal would be the bound. +pub const grace_ns = 50 * ms; +/// With no delta at all, how much longer a frame waits to judge a key. +pub const stale_ns = 250 * ms; +const never = std.math.maxInt(u64); +/// Cells predicted at once: a line typed a round trip ahead. +const max = 64; + +/// A cell predicted: `cp` at column `x` of absolute row `row` (the +/// authority's numbering, screen_state's), a space for a cell backspace +/// cleared; `was` what the screen showed there before; `seq` the key's. +pub const Cell = struct { x: u16, row: u64, cp: u21, was: u21, seq: u32, due: u64 = never }; +pub const Cursor = struct { x: u16, row: u64, seq: u32, due: u64 = never }; + +pub const Stats = struct { + /// Keys into a terminal predicted, and not. + predicted: u32 = 0, + unpredicted: u32 = 0, + /// Cells the screen showed when judged; cells and cursors it did not. + confirmed: u32 = 0, + mispredicted: u32 = 0, + cursor_missed: u32 = 0, +}; + +pub const Echo = struct { + /// Off: nothing predicted (the harness's baseline). + on: bool = true, + /// The harness's clock (ns); the monotonic one when null. + now: ?u64 = null, + /// Its client's view, and the terminal it predicts into. + view: u8 = 0, + pane: u8 = 0, + serial: u32 = 0, + /// Each terminal's last delta: its active area's first row, absolute. + tops: [pardes.MAX_PANES]u64 = @splat(0), + cells: [max]Cell = undefined, + n: u8 = 0, + cursor: ?Cursor = null, + /// The newest of its client's inputs the authority confirmed. + acked: u32 = 0, + /// The newest input not predicted, and when its echo is due: nothing is + /// predicted until then. `wait_key`: it was a key forwarded to this + /// terminal (no other pane's), so the keys typed into it meanwhile wait + /// in `deferred`, to be predicted over the screen that shows its doing. + wait: u32 = 0, + wait_due: u64 = 0, + wait_key: bool = false, + deferred: [max]Deferred = undefined, + nd: u8 = 0, + /// An echo confirmed since the last key not predicted: shown off a + /// prompt too. A shown misprediction since the last confirmed echo: + /// shown nowhere. + trusted: bool = false, + doubt: bool = false, + /// What it draws changed: the client draws a frame. + changed: bool = false, + stats: Stats = .{}, + /// Each key's prediction as it is made, when set (the harness's; on + /// the C heap). + log: ?*std.ArrayListUnmanaged(Predicted) = null, + + pub const Predicted = struct { seq: u32, cell: ?Cell, cursor: Cursor }; + + fn time(e: *const Echo) u64 { + return e.now orelse pardes.FrameStats.nowNs(); + } + + const Deferred = struct { seq: u32, kind: ?Kind, due: u64 = never }; + + fn drop(e: *Echo) void { + if (e.n > 0 or e.cursor != null) e.changed = true; + e.n = 0; + e.cursor = null; + } + + /// Every prediction goes, and the keys that made them may still be on + /// their way: the keys typed next wait for the newest of them to show, + /// as keys typed behind one not predicted do (`deferred`). Predicted + /// at once from the screen's cursor, they would land where those keys' + /// echo is about to (found at 207 ms, where three keys are in flight: + /// one drop, and every key after it misjudged, drawn nowhere). + fn lose(e: *Echo) void { + // The cursor after the newest key: its `due` is that key's, never + // until the authority has it. + const newest = e.cursor; + e.drop(); + const cur = newest orelse return; + if (e.waiting() or e.time() >= cur.due) return; + e.wait = cur.seq; + e.wait_key = true; + e.nd = 0; + e.wait_due = cur.due; + } + + fn waiting(e: *const Echo) bool { + return e.acked < e.wait or e.time() < e.wait_due; + } + + fn waitFor(e: *Echo, seq: u32, key: bool) void { + e.wait = seq; + e.wait_due = never; + e.wait_key = key; + e.nd = 0; + } + + /// Its client's input `seq`, after the replica took it: `applied`, + /// it was applied here at once (so it was no key into a terminal). + pub fn typed(e: *Echo, c: *Pardes, view: u8, ev: pardes.Event, seq: u32, applied: bool) void { + e.view = view; + if (applied) return; + switch (ev) { + // ponytail: a motion or focus report a program asked for could + // move its cursor; the judgement catches that. + .mouse => |m| if (m.kind == .motion) return, + .pointer_leave, .focus => return, + else => {}, + } + const id = keyboardPane(c, view) orelse { + // Another pane's, or not a key: where the keys after it go is + // not known here until the authority has it. ponytail: so they + // wait for it too, each, until a pause. + e.waitFor(seq, false); + return; + }; + if (id != e.pane or c.panes[id].?.serial != e.serial) { + e.drop(); + e.nd = 0; + e.pane = @intCast(id); + e.serial = c.panes[id].?.serial; + } + const kind = kindOf(ev); + const key = forwarded(c, ev); + // A key that is not one of those predicted says nothing of the + // program the keys after it reach (Enter: a password prompt's). + if (kind == null) e.trusted = false; + if (!e.on) { + e.stats.unpredicted += 1; + return e.waitFor(seq, key); + } + // Behind keys still deferred: after them, in order. + e.catchUp(c); + if (e.waiting() or e.nd > 0) { + if (e.wait_key and key and e.nd < max) { + e.deferred[e.nd] = .{ .seq = seq, .kind = kind }; + e.nd += 1; + return; + } + e.stats.unpredicted += 1; + return e.waitFor(seq, false); + } + if (kind) |k| if (e.predict(c.panes[id].?, k, seq)) { + e.stats.predicted += 1; + e.changed = true; + return; + }; + e.stats.unpredicted += 1; + e.waitFor(seq, key); + } + + /// The wait over (its key's doing on the screen), the keys deferred + /// behind it that the screen does not show yet are predicted over it, + /// in order; those it shows are not. One the authority has whose echo + /// may be on its way puts it off until its grace has passed. + fn catchUp(e: *Echo, c: *Pardes) void { + if (e.nd == 0 or e.waiting()) return; + const now = e.time(); + var i: usize = 0; + while (i < e.nd and e.deferred[i].due <= now) i += 1; + if (i < e.nd and e.deferred[i].due != never) return; + e.stats.unpredicted += @intCast(i); + const pane = c.panes[e.pane] orelse return e.waitFor(e.wait, false); + if (pane.serial != e.serial) return e.waitFor(e.wait, false); + while (i < e.nd) : (i += 1) { + const d = e.deferred[i]; + if (d.kind) |k| if (e.predict(pane, k, d.seq)) { + e.stats.predicted += 1; + e.changed = true; + continue; + }; + // The next to wait for, the rest behind it. + e.stats.unpredicted += 1; + const rest = e.nd - i - 1; + std.mem.copyForwards(Deferred, e.deferred[0..rest], e.deferred[i + 1 .. e.nd]); + e.wait = d.seq; + e.wait_due = never; + e.wait_key = true; + e.nd = @intCast(rest); + return; + } + e.nd = 0; + } + + /// Whether a key `kind` into terminal `pane` was predicted. + fn predict(e: *Echo, pane: *Pane, kind: Kind, seq: u32) bool { + const state = pane.terminal orelse return false; + const vt = &state.vt; + if (vt.screens.active_key != .primary or !terminal.atBottom(pane) or !vt.modes.get(.cursor_visible)) return false; + const top = e.tops[e.pane]; + const screen = vt.screens.active; + const cur: Cursor = e.cursor orelse .{ .x = @intCast(screen.cursor.x), .row = top + screen.cursor.y, .seq = 0 }; + if (cur.row < top or cur.row - top >= vt.rows or e.n == max) return false; + var next: Cursor = .{ .x = cur.x, .row = cur.row, .seq = seq }; + var cell: ?Cell = null; + switch (kind) { + .char => |cp| { + // The last column wraps as a program pleases; a cell that + // holds another character is a line edited in its middle, + // or a suggestion (fish's) being left: the shell redraws + // what is right of it. ponytail: mosh's shift of the rest + // would cover bash's middle, and mispredict fish's. + if (cur.x + 1 >= vt.cols) return false; + const under = e.cpAt(state, cur.x, cur.row); + if (!blank(under) and under != cp) return false; + cell = .{ .x = cur.x, .row = cur.row, .cp = cp, .was = under, .seq = seq }; + next.x += 1; + }, + // At the end of a line only: in its middle a line editor moves + // the rest left. + .backspace => { + if (cur.x == 0 or !e.isInput(state, cur.x - 1, cur.row) or !blank(e.cpAt(state, cur.x, cur.row))) return false; + cell = .{ .x = cur.x - 1, .row = cur.row, .cp = ' ', .was = e.cpAt(state, cur.x - 1, cur.row), .seq = seq }; + next.x -= 1; + }, + .left => { + if (cur.x == 0 or !e.isInput(state, cur.x - 1, cur.row)) return false; + next.x -= 1; + }, + // Over typed text only: past its end, fish takes the right + // arrow for its suggestion (another colour). + .right => { + if (cur.x + 1 >= vt.cols or !e.isText(state, cur.x, cur.row)) return false; + next.x += 1; + }, + } + if (cell) |want| e.put(want); + e.cursor = next; + if (e.log) |l| l.append(std.heap.c_allocator, .{ .seq = seq, .cell = cell, .cursor = next }) catch {}; + return true; + } + + fn put(e: *Echo, want: Cell) void { + for (e.cells[0..e.n]) |*have| if (have.x == want.x and have.row == want.row) { + const was = have.was; + have.* = want; + have.was = was; + return; + }; + e.cells[e.n] = want; + e.n += 1; + } + + fn mine(e: *const Echo, x: u16, row: u64) ?*const Cell { + for (e.cells[0..e.n]) |*have| if (have.x == x and have.row == row) return have; + return null; + } + + /// What the screen shows at a cell as predicted so far. + fn cpAt(e: *const Echo, state: *terminal.State, x: u16, row: u64) u21 { + if (e.mine(x, row)) |have| return have.cp; + return e.screenCp(state, x, row); + } + + /// What the screen itself shows there; off its active area, a + /// codepoint no key types. + fn screenCp(e: *const Echo, state: *terminal.State, x: u16, row: u64) u21 { + const ci = e.screenCell(state, x, row) orelse return 0x10ffff; + return ci.cell.codepoint(); + } + + fn screenCell(e: *const Echo, state: *terminal.State, x: u16, row: u64) ?terminal.PageCell { + const top = e.tops[e.pane]; + if (row < top or row - top >= state.vt.rows) return null; + return state.vt.screens.active.pages.getCell(.{ .active = .{ .x = x, .y = @intCast(row - top) } }); + } + + /// A prompt's input there, or a character predicted. + fn isInput(e: *const Echo, state: *terminal.State, x: u16, row: u64) bool { + if (e.mine(x, row)) |have| return !blank(have.cp); + const ci = e.screenCell(state, x, row) orelse return false; + return ci.cell.semantic_content == .input; + } + + /// Typed text there: a character predicted, or a prompt's input in the + /// colour of the cell before it. + fn isText(e: *const Echo, state: *terminal.State, x: u16, row: u64) bool { + if (e.mine(x, row)) |have| return !blank(have.cp); + const ci = e.screenCell(state, x, row) orelse return false; + if (ci.cell.semantic_content != .input or blank(ci.cell.codepoint())) return false; + if (x == 0) return true; + const before = e.screenCell(state, x - 1, row) orelse return false; + return before.cell.style_id == ci.cell.style_id; + } + + /// The authority confirmed its client's input `seq`. + pub fn confirmed(e: *Echo, c: *Pardes, seq: u32) void { + e.acked = seq; + const due = e.time() + grace_ns; + for (e.deferred[0..e.nd]) |*d| if (d.seq <= seq and d.due == never) { + d.due = due; + }; + for (e.cells[0..e.n]) |*cell| if (cell.seq <= seq and cell.due == never) { + cell.due = due; + }; + if (e.cursor) |*cur| if (cur.seq <= seq and cur.due == never) { + cur.due = due; + }; + if (e.wait <= seq and e.wait_due == never) { + // The authority stepped a key this did not predict, after every + // one it did: its deltas from here show that key's doing, which + // no prediction knew of. They go, judged neither way. + e.wait_due = due; + e.drop(); + } + e.catchUp(c); + } + + /// Terminal `id`'s delta was applied, its active area's first row now + /// `top`. + pub fn delta(e: *Echo, c: *Pardes, id: u8, top: u64) void { + if (id == e.pane and top != e.tops[id] and (e.n > 0 or e.cursor != null)) { + // Scrolled: the rows predicted are others now. + e.tops[id] = top; + return e.lose(); + } + e.tops[id] = top; + if (id == e.pane) e.judge(c, 0); + e.catchUp(c); + } + + /// Once a frame: what is stale judged. Whether to draw. + pub fn frame(e: *Echo, c: *Pardes) bool { + if (e.n > 0 or e.cursor != null) e.judge(c, stale_ns); + e.catchUp(c); + defer e.changed = false; + return e.changed; + } + + fn judge(e: *Echo, c: *Pardes, slack: u64) void { + if (e.n == 0 and e.cursor == null) return; + const now = e.time(); + const pane = c.panes[e.pane] orelse return e.drop(); + if (pane.serial != e.serial) return e.drop(); + const state = pane.terminal orelse return e.drop(); + if (state.vt.screens.active_key != .primary) return e.drop(); + const shown = e.visible(c); + var i: usize = 0; + while (i < e.n) { + const cell = e.cells[i]; + if (now < cell.due) { + i += 1; + continue; + } + if (!blank(cell.cp) and e.screenCp(state, cell.x, cell.row) != cell.cp) { + if (now < cell.due + slack) { + i += 1; + continue; + } + e.stats.mispredicted += 1; + if (std.c.getenv("PARDES_REPLICA_TRACE") != null) { + const cur = state.vt.screens.active.cursor; + std.debug.print("echo: key {d}'s {u} at {d},{d} not shown ({u} there) {d} ms past due; top {d}, the screen's cursor {d},{d}, acked {d}\n", .{ cell.seq, cell.cp, cell.x, cell.row, e.screenCp(state, cell.x, cell.row), (now - cell.due) / ms, e.tops[e.pane], cur.x, cur.y, e.acked }); + } + if (shown) e.doubt = true; + e.trusted = false; + return e.lose(); + } + // Shown, or a space (mosh: too easy to show by chance to vouch). + // A key before the last one not predicted vouches for nothing + // after it (a line's characters for the password prompt its + // Enter brought up). + if (!blank(cell.cp)) { + e.stats.confirmed += 1; + if (cell.was != cell.cp and cell.seq > e.wait) { + e.trusted = true; + e.doubt = false; + } + } + e.cells[i] = e.cells[e.n - 1]; + e.n -= 1; + e.changed = true; + } + const cur = e.cursor orelse return; + if (now < cur.due) return; + const screen = state.vt.screens.active; + if (screen.cursor.x == cur.x and e.tops[e.pane] + screen.cursor.y == cur.row) { + e.cursor = null; + e.changed = true; + } else if (now >= cur.due + slack) { + e.stats.cursor_missed += 1; + e.lose(); + } + } + + /// Whether its predictions are drawn now. + pub fn visible(e: *const Echo, c: *const Pardes) bool { + if (e.doubt) return false; + if (e.trusted) return true; + const pane = c.panes[e.pane] orelse return false; + return atPrompt(pane); + } + + /// Over terminal pane `id`'s body, as renderBody drew it: the + /// characters predicted and the cursor after them. + pub fn paint(e: *const Echo, p: *const Pardes, s: *Surface, pane: *const Pane, id: usize, tx: u16, tw: u16, body_y: u16, body_h: u16, active: bool) void { + if (e.n == 0 and e.cursor == null) return; + if (id != e.pane or pane.serial != e.serial or p.live != e.view or pane.body.mode != .tty) return; + if (!e.visible(p)) return; + const state = pane.terminal orelse return; + const sb = state.vt.screens.active.pages.scrollbar(); + const top = e.tops[id]; + // Off a prompt, the trust is inferred: underlined, as mosh does. + const flag = !atPrompt(pane); + const at = struct { + fn f(sb_: @TypeOf(sb), pan: u16, h: u16, top_: u64, row: u64) ?u16 { + if (row < top_ or row - top_ >= sb_.len) return null; + const screen_row = sb_.total - sb_.len + (row - top_); + if (screen_row < sb_.offset + pan) return null; + const y = screen_row - sb_.offset - pan; + return if (y < h) @intCast(y) else null; + } + }.f; + for (e.cells[0..e.n]) |cell| { + const y = at(sb, pane.pan, body_h, top, cell.row) orelse continue; + if (cell.x >= tw) continue; + const dst = s.at(tx + cell.x, body_y + y); + // Typed text continues the text before it (mosh's heuristic). + var style = dst.style; + if (cell.x > 0) { + const before = s.at(tx + cell.x - 1, body_y + y); + if (!before.default and !blank(before.text[0])) style = before.style; + } + if (flag and !blank(cell.cp)) style.ul = .single; + dst.default = false; + dst.text[0] = @intCast(cell.cp); + dst.len = 1; + dst.style = style; + } + if (!active or pane.focus != .body) return; + const cur = e.cursor orelse return; + const y = at(sb, pane.pan, body_h, top, cur.row) orelse return; + if (cur.x < tw) s.cursor = .{ .x = tx + cur.x, .y = body_y + y }; + } +}; + +const Kind = union(enum) { char: u21, backspace, left, right }; + +fn kindOf(ev: pardes.Event) ?Kind { + const k = switch (ev) { + .key => |k| k, + else => return null, + }; + if (k.ctrl or k.alt) return null; + switch (k.cp) { + Key.backspace => return .backspace, + Key.left => return .left, + Key.right => return .right, + else => {}, + } + // As forwardKey sends it. ponytail: ASCII only; a width table would + // let the rest in. + const cp: u21 = if (k.text.len == 1) + k.text[0] + else if (k.text.len == 0 and k.cp >= 'a' and k.cp <= 'z' and k.shift) + k.cp - 'a' + 'A' + else if (k.text.len == 0) + k.cp + else + return null; + return if (cp >= 0x20 and cp < 0x7f) .{ .char = cp } else null; +} + +/// Whether key `ev` goes to the terminal's program as it is (forwardKey's) +/// and not to pardes: Esc backs out of the pane, the tty chord leaves tty +/// mode, Ctrl-V pastes the register. +fn forwarded(c: *const Pardes, ev: pardes.Event) bool { + const k = switch (ev) { + .key => |k| k, + else => return false, + }; + if (k.cp == Key.escape) return false; + return !(k.ctrl and (k.cp == c.tty_toggle or k.cp == 'v' or k.cp == 'V')); +} + +fn blank(cp: u21) bool { + return cp == 0 or cp == ' '; +} + +/// At a shell's prompt, reading its input: pardes's marks (a B and no C +/// since) and the cursor's own, on the primary screen. A shell without +/// pardes's marks is trusted only once its echo is confirmed. +fn atPrompt(pane: *const Pane) bool { + const state = pane.terminal orelse return false; + return state.vt.screens.active_key == .primary and state.stream.handler.phase == .input and terminal.promptInputReady(pane); +} + +/// The terminal view `view`'s keys go to, in tty mode: none when they go +/// elsewhere (a prompt, a header, window mode, another pane). +fn keyboardPane(c: *const Pardes, view: u8) ?usize { + const live = c.live == view; + const v = &(c.views[view] orelse return null); + const id = v.active; + if (v.window_pick != null or v.header != null) return null; + const pane = c.panes[id] orelse return null; + if (!pane.isTerminal() or pane.terminal == null or pane.prompt != .none) return null; + const mode = if (live) pane.body.mode else pane.pv[view].body.mode; + const focus = if (live) pane.focus else pane.pv[view].focus; + return if (mode == .tty and focus == .body) id else null; +} + +// ---- tests: one authority and one replica, its shell played by hand ---- + +const testing = std.testing; +const replica = @import("../replica.zig"); + +const Rig = struct { + a: *replica.Authority, + r: *replica.Replica, + screens: replica.Screens, + peer: u16, + shell: u8, + /// What the authority wrote its shell's pty. + written: std.ArrayListUnmanaged(u8) = .empty, + now: u64 = 1_000 * ms, + + fn ptyWrite(ctx: ?*anyopaque, _: u8, bytes: []const u8) void { + const g: *Rig = @ptrCast(@alignCast(ctx.?)); + g.written.appendSlice(testing.allocator, bytes) catch {}; + } + fn watched(_: ?*anyopaque) bool { + return false; + } + const vtable: pardes.Host.VTable = .{ .watched = watched, .pty_write = ptyWrite }; + + fn init(g: *Rig) !void { + const gpa = testing.allocator; + g.written = .empty; + g.now = 1_000 * ms; + g.a = try replica.Authority.init(gpa, .{ .cols = 60, .rows = 16, .boot = .tty_shell }, .{ .ctx = g, .vtable = &vtable }, true); + g.screens = .init(gpa); + g.peer = try g.screens.addPeer(); + g.r = try replica.Replica.init(gpa, 1, try g.a.unit(0, 0), true); + g.shell = for (g.a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + g.r.echo.now = g.now; + } + + fn deinit(g: *Rig) void { + g.r.deinit(); + g.screens.deinit(g.a.core); + g.a.deinit(); + g.written.deinit(testing.allocator); + } + + fn later(g: *Rig, dt: u64) void { + g.now += dt; + g.r.echo.now = g.now; + } + + /// The shell prints `bytes`; the replica gets the unit and the delta. + fn prints(g: *Rig, bytes: []const u8) !void { + g.a.output(.{ .output = .{ .pane = g.shell, .bytes = bytes } }); + try g.sync(); + } + + fn sync(g: *Rig) !void { + const gpa = testing.allocator; + if (try g.a.pending()) |u| g.r.confirm(u); + g.r.settle(); + var msgs: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (msgs.items) |m| gpa.free(m); + msgs.deinit(gpa); + } + try g.screens.flush(g.a.core, g.peer, g.r.view, &msgs); + for (msgs.items) |m| if (g.r.screen(m)) |reply| { + _ = try g.screens.take(g.a.core, g.peer, reply); + }; + } + + /// The replica's client types `k`; the message for the authority. + fn key(g: *Rig, k: Key) ![]u8 { + return testing.allocator.dupe(u8, try g.r.input(.{ .key = k })); + } + + /// The authority takes it, and the replica its unit. + fn take(g: *Rig, msg: []u8) !void { + defer testing.allocator.free(msg); + if (try g.a.pending()) |u| g.r.confirm(u); + g.r.confirm(try g.a.take(msg)); + g.r.settle(); + } + + /// What the replica's screen shows at the shell's cursor row, drawn, + /// and whether the cell at `x` is underlined. + fn drawn(g: *Rig, arena: *std.heap.ArenaAllocator, x: u16) !struct { cp: u8, ul: bool, cursor: ?u16 } { + _ = arena.reset(.retain_capacity); + const c = g.r.core(); + c.needs_frame = true; + const s = try c.render(arena.allocator()); + const pane = c.panes[g.shell].?; + const body = s.region(.body, g.shell).?.rect; + const y = body.y + terminal.gridCursor(pane).y; + const cell = s.at(body.x + x, y); + return .{ .cp = cell.text[0], .ul = cell.style.ul != .off, .cursor = if (s.cursor) |cur| if (cur.y == y) cur.x - body.x else null else null }; + } +}; + +const prompt = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07"; + +test "keys typed at a shell's prompt are drawn at once, and the authority's echo takes their place" { + var g: Rig = undefined; + try g.init(); + defer g.deinit(); + try g.prints(prompt); + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + const before = try g.drawn(&arena, 2); + try testing.expectEqual(@as(?u16, 2), before.cursor); + // Typed: shown at once, the cursor past it, and nothing underlined at + // a prompt. + const l = try g.key(.{ .cp = 'l', .text = "l" }); + const s = try g.key(.{ .cp = 's', .text = "s" }); + try testing.expectEqual(@as(u32, 2), g.r.echo.stats.predicted); + var shown = try g.drawn(&arena, 2); + try testing.expectEqual(@as(u8, 'l'), shown.cp); + try testing.expect(!shown.ul); + try testing.expectEqual(@as(?u16, 4), shown.cursor); + try testing.expectEqual(@as(u8, 's'), (try g.drawn(&arena, 3)).cp); + // The authority's core draws no overlay of its own. + try testing.expect(g.a.core.echo == null); + // The authority takes them, its shell echoes, the delta lands after the + // grace: confirmed, and the screen's own cells and cursor shown. + try g.take(l); + try g.take(s); + try testing.expectEqualStrings("ls", g.written.items); + g.later(grace_ns); + try g.prints("ls"); + try testing.expectEqual(@as(u8, 0), g.r.echo.n); + try testing.expect(g.r.echo.cursor == null); + try testing.expectEqual(@as(u32, 2), g.r.echo.stats.confirmed); + shown = try g.drawn(&arena, 3); + try testing.expectEqual(@as(u8, 's'), shown.cp); + try testing.expectEqual(@as(?u16, 4), shown.cursor); + // Backspace at the end of the line, shown at once. + const bs = try g.key(.{ .cp = Key.backspace }); + shown = try g.drawn(&arena, 3); + try testing.expectEqual(@as(u8, ' '), shown.cp); + try testing.expectEqual(@as(?u16, 3), shown.cursor); + try g.take(bs); + g.later(grace_ns); + try g.prints("\x08\x1b[K"); + try testing.expectEqual(@as(u32, 0), g.r.echo.stats.mispredicted + g.r.echo.stats.cursor_missed); + try testing.expect(g.r.quiet()); + try testing.expectEqual(replica.digestOf(g.a), g.r.digest()); +} + +test "a password prompt shows nothing typed, and a full-screen program is not predicted" { + var g: Rig = undefined; + try g.init(); + defer g.deinit(); + try g.prints(prompt ++ "sudo ls\r\n\x1b]133;C;aid=pardes\x07[sudo] password: "); + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + const x = terminal.gridCursor(g.r.core().panes[g.shell].?).x; + const k = try g.key(.{ .cp = 'h', .text = "h" }); + // Predicted, and not drawn: nothing has confirmed an echo here. + try testing.expectEqual(@as(u32, 1), g.r.echo.stats.predicted); + try testing.expectEqual(@as(u8, ' '), (try g.drawn(&arena, x)).cp); + try g.take(k); + // No echo, so no delta: a frame lets it go once it is stale. + g.later(grace_ns + stale_ns); + _ = g.r.echoFrame(); + try testing.expectEqual(@as(u8, 0), g.r.echo.n); + try testing.expectEqual(@as(u32, 1), g.r.echo.stats.mispredicted); + // The next key still shows nothing. + const k2 = try g.key(.{ .cp = 'i', .text = "i" }); + try testing.expectEqual(@as(u8, ' '), (try g.drawn(&arena, x)).cp); + try g.take(k2); + // vim: the alternate screen, never predicted. + try g.prints("\r\n\x1b]133;D;1;aid=pardes\x07" ++ prompt ++ "vim\r\n\x1b]133;C;aid=pardes\x07\x1b[?1049h\x1b[H~"); + g.later(grace_ns + stale_ns); + _ = g.r.echoFrame(); + const before = g.r.echo.stats.predicted; + try g.take(try g.key(.{ .cp = 'i', .text = "i" })); + try testing.expectEqual(before, g.r.echo.stats.predicted); +} + +test "keys still on their way when their predictions go: the next waits for their echo, never predicted where it is about to land" { + var g: Rig = undefined; + try g.init(); + defer g.deinit(); + try g.prints(prompt); + // Three keys typed a round trip ahead; the authority has the first, + // and its shell echoes something else: every prediction goes. + const a = try g.key(.{ .cp = 'a', .text = "a" }); + const b = try g.key(.{ .cp = 'b', .text = "b" }); + const c = try g.key(.{ .cp = 'c', .text = "c" }); + try testing.expectEqual(@as(u32, 3), g.r.echo.stats.predicted); + try g.take(a); + g.later(grace_ns); + try g.prints("A"); + try testing.expectEqual(@as(u32, 1), g.r.echo.stats.mispredicted); + try testing.expectEqual(@as(u8, 0), g.r.echo.n); + // The next key is not predicted at the screen's cursor, where b's + // echo lands: it waits for b and c. + const d = try g.key(.{ .cp = 'd', .text = "d" }); + try testing.expectEqual(@as(u32, 3), g.r.echo.stats.predicted); + try testing.expectEqual(@as(u8, 0), g.r.echo.n); + try g.take(b); + try g.take(c); + g.later(grace_ns); + try g.prints("bc"); + // Their echo shown: d is predicted after it. + try testing.expectEqual(@as(u32, 4), g.r.echo.stats.predicted); + try testing.expectEqual(@as(u8, 1), g.r.echo.n); + try testing.expectEqual(@as(u16, 5), g.r.echo.cells[0].x); + try g.take(d); + g.later(grace_ns); + try g.prints("d"); + try testing.expectEqual(@as(u32, 1), g.r.echo.stats.mispredicted); + try testing.expect(!g.r.echo.doubt); + try testing.expectEqual(@as(u8, 0), g.r.echo.n); +} + +test "a misprediction takes every prediction back and shows none until an echo is confirmed; after Tab nothing is predicted until its echo" { + var g: Rig = undefined; + try g.init(); + defer g.deinit(); + try g.prints(prompt); + var arena: std.heap.ArenaAllocator = .init(testing.allocator); + defer arena.deinit(); + // A line editor that echoes something else (vi's command mode): shown, + // then taken back. + const a = try g.key(.{ .cp = 'a', .text = "a" }); + try testing.expectEqual(@as(u8, 'a'), (try g.drawn(&arena, 2)).cp); + try g.take(a); + g.later(grace_ns); + try g.prints("A"); + try testing.expectEqual(@as(u32, 1), g.r.echo.stats.mispredicted); + try testing.expect(g.r.echo.doubt); + try testing.expectEqual(@as(u8, 'A'), (try g.drawn(&arena, 2)).cp); + // Doubted: predicted, not drawn, until one is confirmed. + const b = try g.key(.{ .cp = 'b', .text = "b" }); + try testing.expectEqual(@as(u8, ' '), (try g.drawn(&arena, 3)).cp); + try g.take(b); + g.later(grace_ns); + try g.prints("b"); + try testing.expect(!g.r.echo.doubt); + const c = try g.key(.{ .cp = 'c', .text = "c" }); + try testing.expectEqual(@as(u8, 'c'), (try g.drawn(&arena, 4)).cp); + // Tab: not predicted, and neither is what follows until the authority + // has it and its echo has had its grace. + const tab = try g.key(.{ .cp = Key.tab }); + const d = try g.key(.{ .cp = 'd', .text = "d" }); + try testing.expectEqual(@as(u32, 1), g.r.echo.stats.unpredicted); + try g.take(c); + try testing.expectEqual(@as(u8, 'c'), (try g.drawn(&arena, 4)).cp); + // The authority has Tab (and the key after it): what was predicted + // before it goes, the screen's own shown. + try g.take(tab); + try g.take(d); + try testing.expectEqual(@as(u8, 0), g.r.echo.n); + g.later(grace_ns); + try g.prints("c" ++ "ompleted d"); + // `d`, typed behind Tab, the screen showed by then: not predicted. + try testing.expectEqual(@as(u32, 2), g.r.echo.stats.unpredicted); + const e = try g.key(.{ .cp = 'e', .text = "e" }); + try testing.expectEqual(@as(u8, 'e'), (try g.drawn(&arena, 15)).cp); + try g.take(e); + g.later(grace_ns); + try g.prints("e"); + // A key typed behind Tab that the authority does not have yet is + // predicted once Tab's doing is on the screen, where it now goes. + const tab2 = try g.key(.{ .cp = Key.tab }); + const f = try g.key(.{ .cp = 'f', .text = "f" }); + try g.take(tab2); + try g.prints("ff"); + try testing.expectEqual(@as(u8, 0), g.r.echo.n); + g.later(grace_ns); + _ = g.r.echoFrame(); + try testing.expectEqual(@as(u8, 1), g.r.echo.n); + try testing.expectEqual(@as(u8, 'f'), (try g.drawn(&arena, 18)).cp); + try g.take(f); + g.later(grace_ns); + try g.prints("f"); + try testing.expectEqual(@as(u8, 0), g.r.echo.n); + try testing.expectEqual(@as(u32, 1), g.r.echo.stats.mispredicted); + try testing.expectEqual(@as(u32, 0), g.r.echo.stats.cursor_missed); + try testing.expect(g.r.quiet()); + try testing.expectEqual(replica.digestOf(g.a), g.r.digest()); +} diff --git a/src/term/screen_state.zig b/src/term/screen_state.zig new file mode 100644 index 00000000..bbb7f959 --- /dev/null +++ b/src/term/screen_state.zig @@ -0,0 +1,1664 @@ +//! Screen-state terminals (docs/next-daemon.md, "Terminals cross as +//! authoritative screen state", phase 5). A replica is sent a terminal's +//! state, never its pty's bytes, so a `cat` of a gigabyte costs it screens. +//! +//! The authority encodes a delta for one peer: a header (the cursor, the +//! active area's first row as an absolute row, the history's extent), the +//! content address of the terminal-wide state, the content address of each +//! row of the active area, and the blobs behind the addresses the peer does +//! not hold. Both kinds of blob are ghostty's own snapshot records, so they +//! cross exactly: the terminal-wide state is a TERMINAL record (modes, +//! colours, title, tab stops...), and a row is the PAGE record of a one-row +//! page (its styles, graphemes, hyperlinks, wide cells, wrap and prompt +//! marks). A row's address is a hash of what the row shows, not of its blob, +//! whose style ids are its source page's. An unchanged row costs 8 bytes. +//! +//! Each state the authority sends is a version of its terminal (`Track`), +//! made once whatever the number of peers. A peer's next delta is against +//! the version it was last sent: the stream is ordered and loses nothing, +//! so the peer has that one by the time it applies the next. Several are +//! in flight (`bound`); acknowledgements free their room, and a peer at the +//! bound gets nothing until one comes, then the newest version, the ones +//! between skipped. Peers at the same base are sent the same bytes. A +//! replica holds the blobs of the state it applied last and no others; a +//! delta against another (one lost) is refused, and the peer started afresh. +//! +//! The replica writes each delta into a ghostty Terminal, which a replica's +//! terminal pane draws as a local one (src/term/terminal.zig reads only that). +//! Rows that scroll off its active area go into its history, made what the +//! authority's are by the delta's tail (their addresses, up to a screen of +//! them). Older ones it never saw, or saw before they last changed, are +//! holes there, fetched by absolute row on demand (`encodeRows`, +//! `applyRows`). +const std = @import("std"); +const vt = @import("ghostty-vt"); +const snapshot = vt.snapshot; +pub const Terminal = vt.Terminal; +const Page = vt.page.Page; +const Row = vt.page.Row; +const Pin = vt.Pin; +const Screen = vt.Screen; + +// ponytail: 64 bits: two of a million distinct rows collide about once in +// 4*10^7 sessions; phase 6's cache, shared across sessions, wants 128. +pub const Hash = u64; + +/// What a row shows, hashed: its wrap and prompt flags, and each of its +/// first `cols` cells up to the last one that is not blank, with its style, +/// grapheme and hyperlink by value. Not the page's style or hyperlink ids, +/// nor its derived flags. Its width is in it: a blob is a row that wide. +/// (A page can be wider than its terminal: ghostty narrows them lazily.) +pub fn rowHash(page: *Page, row: *Row, cols: u16) Hash { + var h = std.hash.Wyhash.init(0); + h.update(&.{rowFlags(row)}); + h.update(std.mem.asBytes(&cols)); + const all = page.getCells(row); + const cells = all[0..@min(cols, all.len)]; + var n = cells.len; + while (n > 0 and @as(u64, @bitCast(cells[n - 1])) == 0) n -= 1; + for (cells[0..n]) |*c| { + var word = c.*; + word.style_id = 0; + h.update(std.mem.asBytes(&@as(u64, @bitCast(word)))); + if (c.style_id != 0) h.update(std.mem.asBytes(&page.styles.get(page.memory, c.style_id).hash())); + if (c.hasGrapheme()) h.update(std.mem.sliceAsBytes(page.lookupGrapheme(c).?)); + if (c.hyperlink) if (page.lookupHyperlink(c)) |id| + h.update(std.mem.asBytes(&page.hyperlink_set.get(page.memory, id).hash(page.memory))); + } + return h.final(); +} + +fn rowFlags(row: *const Row) u8 { + return @as(u8, @intFromBool(row.wrap)) | @as(u8, @intFromBool(row.wrap_continuation)) << 1 | + @as(u8, @backingInt(row.semantic_prompt)) << 2; +} + +/// The authority's absolute row numbers for one terminal: its primary +/// screen's active area starts at `top`, and a row `d` above it in its +/// history is `top - d`. Kept by a tracked pin at the last active top, so +/// it counts the rows scrolled since. A resize renumbers (`epoch`). +pub const Rows = struct { + pin: ?*Pin = null, + top: u64 = 0, + epoch: u32 = 0, + cols: u16 = 0, + rows: u16 = 0, + + pub fn update(n: *Rows, t: *Terminal) !void { + const pages = &t.screens.get(.primary).?.pages; + const top = pages.getTopLeft(.active); + const y = pages.pointFromPin(.screen, top).?.screen.y; + if (n.pin) |p| { + if (t.cols != n.cols or t.rows != n.rows) n.epoch +%= 1; + // The last top was pruned away: every row the peer has is + // older than the oldest row here. + const was: ?u32 = if (p.garbage) null else if (pages.pointFromPin(.screen, p.*)) |pt| pt.screen.y else null; + n.top += if (was) |w| y -| w else @as(u64, y) + t.rows; + // A reflow (a resize to fewer columns) puts more rows above the + // last top than ever scrolled past it: numbered from here, the + // history's oldest row is still row 0 or later. A resize + // renumbers anyway (`epoch`), so no peer holds the old numbers. + n.top = @max(n.top, y); + p.* = top; + } else { + n.pin = try pages.trackPin(top); + n.top = y; + } + n.cols = t.cols; + n.rows = t.rows; + } + + pub fn deinit(n: *Rows, t: *Terminal) void { + if (n.pin) |p| t.screens.get(.primary).?.pages.untrackPin(p); + n.* = .{}; + } + + /// The pin of absolute row `abs`, if the terminal still has it. + fn pinAt(n: *const Rows, t: *Terminal, abs: u64) ?Pin { + return pinOf(&t.screens.get(.primary).?.pages, n.top, abs); + } +}; + +fn pinOf(pages: *const vt.PageList, top: u64, abs: u64) ?Pin { + if (abs >= top) return pages.pin(.{ .active = .{ .y = std.math.cast(u32, abs - top) orelse return null } }); + const d = std.math.cast(usize, top - abs) orelse return null; + return pages.getTopLeft(.active).up(d); +} + +/// The authority's scratch: a one-row page each blob is copied into +/// (fresh, so the blob holds that row's styles alone) and encoded from. +pub const Encoder = struct { + gpa: std.mem.Allocator, + scratch: ?Page = null, + blob: std.Io.Writer.Allocating, + /// The rows a delta sends a blob for, and the addresses it lists once. + needed: std.ArrayList(struct { pin: Pin, hash: Hash }) = .empty, + listed: std.AutoHashMapUnmanaged(Hash, void) = .empty, + tail: std.ArrayList(Pin) = .empty, + + pub fn init(gpa: std.mem.Allocator) Encoder { + return .{ .gpa = gpa, .blob = .init(gpa) }; + } + + pub fn deinit(e: *Encoder) void { + if (e.scratch) |*s| s.deinit(); + e.blob.deinit(); + e.needed.deinit(e.gpa); + e.listed.deinit(e.gpa); + e.tail.deinit(e.gpa); + } + + /// The PAGE record of `row` alone, its first `cols` cells. + fn rowBlob(e: *Encoder, page: *Page, row: *Row, cols: u16, w: *std.Io.Writer) !void { + var cap: vt.page.Capacity = if (e.scratch) |s| s.capacity else .{ .cols = cols, .rows = 1, .styles = 64 }; + if (cap.cols != cols) cap = .{ .cols = cols, .rows = 1, .styles = 64 }; + for (0..16) |_| { + const s = try scratchPage(&e.scratch, cap); + s.cloneRowFrom(page, s.getRow(0), row) catch { + // Room for this row: everything doubled until it fits. + cap.styles *|= 2; + cap.grapheme_bytes *|= 2; + cap.hyperlink_bytes *|= 2; + cap.string_bytes *|= 2; + continue; + }; + break; + } else return error.RowTooBig; + var rw: snapshot.record.Writer = .init(e.gpa, w); + defer rw.deinit(); + try snapshot.page.encode(&e.scratch.?, &rw); + } +}; + +/// `slot`'s one-row scratch page, ready for `cap`: reused when it has that +/// capacity, else made again. +fn scratchPage(slot: *?Page, cap: vt.page.Capacity) !*Page { + if (slot.*) |*s| { + if (std.meta.eql(s.capacity, cap)) { + s.reinit(); + return s; + } + s.deinit(); + slot.* = null; + } + slot.* = try Page.init(cap); + return &slot.*.?; +} + +/// Holes older than a history's oldest row are gone with it. +fn clampHoles(holes: *std.ArrayList([2]u64), oldest: u64) void { + var i: usize = 0; + while (i < holes.items.len) { + const hole = &holes.items[i]; + hole[0] = @max(hole[0], oldest); + if (hole[0] >= hole[1]) _ = holes.orderedRemove(i) else i += 1; + } +} + +/// Deltas a peer may have in flight. At 207 ms and 60 frames a second about +/// 13 are; past the bound a peer gets nothing until an acknowledgement, and +/// then the newest state, the ones between skipped. +pub const bound = 32; + +/// What a peer holds once it has a version: the addresses of its active +/// rows and its meta. A delta against it sends no blob for these. +const Known = struct { + version: u32, + epoch: u32, + top: u64, + addrs: std.AutoHashMapUnmanaged(Hash, void) = .empty, +}; + +/// One terminal as the authority numbers it, for all of its peers: each +/// state it sends is a version, made once whatever the number of peers. +pub const Track = struct { + rows: Rows = .{}, + version: u32 = 0, + /// The emulator's change count the newest version was made at, and its + /// history's rows. + changes: u32 = 0, + history: u64 = 0, + /// The newest version: its active rows (pins, good while the emulator + /// is unchanged) and their addresses, and its meta. + pins: std.ArrayList(Pin) = .empty, + hashes: std.ArrayList(Hash) = .empty, + meta: std.Io.Writer.Allocating, + meta_hash: Hash = 0, + /// The newest versions, oldest first: the bases a peer's next delta may + /// have. A peer whose base has gone starts afresh. + known: std.ArrayList(Known) = .empty, + /// The last delta encoded, for the next peer at the same base. + cache: std.Io.Writer.Allocating, + cache_base: u32 = 0, + cache_version: u32 = 0, + + const kept = bound + 8; + + pub fn init(gpa: std.mem.Allocator) Track { + return .{ .meta = .init(gpa), .cache = .init(gpa) }; + } + + /// `t` is the terminal, or null when it is gone already (with its pins). + pub fn deinit(tr: *Track, gpa: std.mem.Allocator, t: ?*Terminal) void { + if (t) |term| tr.rows.deinit(term); + tr.pins.deinit(gpa); + tr.hashes.deinit(gpa); + tr.meta.deinit(); + for (tr.known.items) |*k| k.addrs.deinit(gpa); + tr.known.deinit(gpa); + tr.cache.deinit(); + } + + fn find(tr: *const Track, version: u32) ?*const Known { + if (version == 0) return null; + for (tr.known.items) |*k| if (k.version == version) return if (k.epoch == tr.rows.epoch) k else null; + return null; + } + + /// A version of `t` as it is now. + fn advance(tr: *Track, gpa: std.mem.Allocator, t: *Terminal, changes: u32) !void { + try tr.rows.update(t); + tr.version +%= 1; + if (tr.version == 0) tr.version = 1; + tr.changes = changes; + const primary = &t.screens.get(.primary).?.pages; + tr.history = primary.total_rows - primary.rows; + tr.meta.clearRetainingCapacity(); + { + var rw: snapshot.record.Writer = .init(gpa, &tr.meta.writer); + defer rw.deinit(); + try snapshot.terminal.encode(t, &rw); + } + tr.meta_hash = std.hash.Wyhash.hash(0, tr.meta.written()); + tr.pins.clearRetainingCapacity(); + tr.hashes.clearRetainingCapacity(); + var it = t.screens.active.pages.rowIterator(.right_down, .{ .active = .{} }, null); + while (it.next()) |p| { + try tr.pins.append(gpa, p); + try tr.hashes.append(gpa, rowHash(p.node.page(), p.rowAndCell().row, t.cols)); + } + if (tr.known.items.len == kept) { + tr.known.items[0].addrs.deinit(gpa); + _ = tr.known.orderedRemove(0); + } + var k: Known = .{ .version = tr.version, .epoch = tr.rows.epoch, .top = tr.rows.top }; + errdefer k.addrs.deinit(gpa); + try k.addrs.ensureTotalCapacity(gpa, @intCast(tr.hashes.items.len + 1)); + for (tr.hashes.items) |h| k.addrs.putAssumeCapacity(h, {}); + k.addrs.putAssumeCapacity(tr.meta_hash, {}); + try tr.known.append(gpa, k); + } +}; + +/// What the authority knows of one peer of one terminal. +pub const Peer = struct { + /// The version last sent, which the next delta is against; 0: none, so + /// the next starts the replica afresh. + sent: u32 = 0, + /// The versions in flight, oldest first. + flight: [bound]u32 = undefined, + n: u8 = 0, + /// The history rows its replica holds as holes, absolute, as the + /// replica keeps them (Replica.apply's rule, from what it was sent): + /// what `fill` sends before a step reads them. + top: u64 = 0, + holes: std.ArrayListUnmanaged([2]u64) = .empty, + + pub fn deinit(peer: *Peer, gpa: std.mem.Allocator) void { + peer.holes.deinit(gpa); + } + + /// Every version up to `seq` has landed. + pub fn ack(peer: *Peer, seq: u32) void { + var i: u8 = 0; + while (i < peer.n and peer.flight[i] <= seq) i += 1; + std.mem.copyForwards(u32, peer.flight[0 .. peer.n - i], peer.flight[i..peer.n]); + peer.n -= i; + } + + /// Start the peer afresh: it lost its state, or never had one. + pub fn reset(peer: *Peer) void { + var holes = peer.holes; + holes.clearRetainingCapacity(); + peer.* = .{ .holes = holes }; + } + + /// Its replica's holes once it applies a delta to `tr`'s newest version + /// against `base`, as Replica.apply makes them. + fn noteSent(peer: *Peer, gpa: std.mem.Allocator, tr: *const Track, base: ?*const Known, rows: u16) !void { + const top = tr.rows.top; + const history = tr.history; + if (base) |b| { + if (top > peer.top) { + const tail = @min(top -| b.top, rows, history); + if (top - peer.top >= @as(u64, rows) + history) { + peer.holes.clearRetainingCapacity(); + if (history > tail) try peer.holes.append(gpa, .{ top - history, top - tail }); + } else if (top - tail > peer.top) try peer.holes.append(gpa, .{ peer.top, top - tail }); + } + } else { + peer.holes.clearRetainingCapacity(); + if (history > 0) try peer.holes.append(gpa, .{ top - history, top }); + } + peer.top = top; + clampHoles(&peer.holes, top -| history); + } +}; + +/// The delta's fixed header: u32 seq, u32 base (0: none, the replica +/// starts afresh), u8 flags, u32 epoch, u64 meta, u16 cursor x and y, u8 +/// cursor style, u8 pending wrap, u8 the cursor's semantic content, the +/// kitty keyboard flags, u64 top, u64 history rows, u64 the viewport's top +/// as an absolute row (all ones: following the active area), u16 and the +/// caller's extra bytes; then u16 rows and a u64 per active row, u16 and a +/// u64 per row of the tail (those that scrolled off the active area since +/// the base, up to a screen of them, oldest first), and a u16 blob count +/// and each blob as u64 hash, u32 length, bytes. +const flag_reset: u8 = 1; +const KittyFlags = @FieldType(Screen, "kitty_keyboard"); +const following = std.math.maxInt(u64); + +/// The delta that takes `peer` from what it was last sent to `t` as it is, +/// onto `out`; false (nothing written) when it has that already, or has +/// `bound` in flight. `changes` counts the emulator's writes (a new count +/// is a new version); `extra` rides along (the caller's own state of the +/// terminal), the same for every peer of one version. A peer at the same +/// base as the last one encoded is sent the same bytes. +pub fn encode(e: *Encoder, tr: *Track, peer: *Peer, t: *Terminal, changes: u32, extra: []const u8, out: *std.Io.Writer) !bool { + if (tr.version == 0 or changes != tr.changes) try tr.advance(e.gpa, t, changes); + if (peer.sent == tr.version or peer.n == bound) return false; + const base = tr.find(peer.sent); + const base_version = if (base) |b| b.version else 0; + if (tr.cache_version != tr.version or tr.cache_base != base_version) { + tr.cache.clearRetainingCapacity(); + try write(e, tr, base, t, extra, &tr.cache.writer); + tr.cache_version = tr.version; + tr.cache_base = base_version; + } + try out.writeAll(tr.cache.written()); + try peer.noteSent(e.gpa, tr, base, t.rows); + peer.sent = tr.version; + peer.flight[peer.n] = tr.version; + peer.n += 1; + return true; +} + +fn write(e: *Encoder, tr: *Track, base: ?*const Known, t: *Terminal, extra: []const u8, out: *std.Io.Writer) !void { + const gpa = e.gpa; + const reset = base == null; + const screen = t.screens.active; + const pages = &t.screens.get(.primary).?.pages; + const history = pages.total_rows - pages.rows; + const top = tr.rows.top; + try out.writeInt(u32, tr.version, .little); + try out.writeInt(u32, if (base) |b| b.version else 0, .little); + try out.writeByte(if (reset) flag_reset else 0); + try out.writeInt(u32, tr.rows.epoch, .little); + try out.writeInt(u64, tr.meta_hash, .little); + try out.writeInt(u16, screen.cursor.x, .little); + try out.writeInt(u16, screen.cursor.y, .little); + try out.writeByte(@backingInt(screen.cursor.cursor_style)); + try out.writeByte(@intFromBool(screen.cursor.pending_wrap)); + try out.writeByte(@backingInt(screen.cursor.semantic_content)); + // ponytail: raw bytes, as the hello refuses a peer of another build. + try out.writeAll(std.mem.asBytes(&screen.kitty_keyboard)); + try out.writeInt(u64, top, .little); + try out.writeInt(u64, history, .little); + try out.writeInt(u64, if (pages.viewport == .active) following else top - history + pages.scrollbar().offset, .little); + try out.writeInt(u16, @intCast(extra.len), .little); + try out.writeAll(extra); + + // The active rows, then the tail: what scrolled off since the base, + // which may have changed after it was sent. + e.listed.clearRetainingCapacity(); + e.needed.clearRetainingCapacity(); + const tail: u16 = if (base) |b| @intCast(@min(top -| b.top, t.rows, history)) else 0; + e.tail.clearRetainingCapacity(); + for (0..tail) |j| try e.tail.append(gpa, pages.getTopLeft(.active).up(tail - j).?); + try out.writeInt(u16, @intCast(tr.pins.items.len), .little); + for (tr.pins.items, tr.hashes.items) |p, h| { + try out.writeInt(u64, h, .little); + try needBlob(e, base, p, h); + } + try out.writeInt(u16, tail, .little); + for (e.tail.items) |p| { + const h = rowHash(p.node.page(), p.rowAndCell().row, t.cols); + try out.writeInt(u64, h, .little); + try needBlob(e, base, p, h); + } + const meta_new = if (base) |b| !b.addrs.contains(tr.meta_hash) else true; + try out.writeInt(u16, @intCast(e.needed.items.len + @intFromBool(meta_new)), .little); + if (meta_new) { + try out.writeInt(u64, tr.meta_hash, .little); + try out.writeInt(u32, @intCast(tr.meta.written().len), .little); + try out.writeAll(tr.meta.written()); + } + for (e.needed.items) |need| { + e.blob.clearRetainingCapacity(); + try e.rowBlob(need.pin.node.page(), need.pin.rowAndCell().row, t.cols, &e.blob.writer); + try out.writeInt(u64, need.hash, .little); + try out.writeInt(u32, @intCast(e.blob.written().len), .little); + try out.writeAll(e.blob.written()); + } +} + +/// A blob goes for an address the base does not hold, once. +fn needBlob(e: *Encoder, base: ?*const Known, p: Pin, h: Hash) !void { + if ((try e.listed.getOrPut(e.gpa, h)).found_existing) return; + if (base) |b| if (b.addrs.contains(h)) return; + try e.needed.append(e.gpa, .{ .pin = p, .hash = h }); +} + +/// The primary screen's rows `[lo, hi)` the authority still has, for a +/// replica that holds them as holes: u32 epoch, u32 blobs, each u64 hash, +/// u32 length, bytes, then u32 rows, each u64 row and u64 hash. A blob goes +/// once a message (a flood's rows are one). +// ponytail: blobs once a message, not once a peer: phase 6's content cache. +pub fn encodeRows(e: *Encoder, tr: *Track, t: *Terminal, lo: u64, hi: u64, out: *std.Io.Writer) !void { + try tr.rows.update(t); + var blobs: std.Io.Writer.Allocating = .init(e.gpa); + defer blobs.deinit(); + var rows: std.Io.Writer.Allocating = .init(e.gpa); + defer rows.deinit(); + e.listed.clearRetainingCapacity(); + var nblobs: u32 = 0; + var nrows: u32 = 0; + var abs = lo; + // The active area is the deltas'. + while (abs < @min(hi, tr.rows.top)) : (abs += 1) { + const p = tr.rows.pinAt(t, abs) orelse continue; + const h = rowHash(p.node.page(), p.rowAndCell().row, t.cols); + try rows.writer.writeInt(u64, abs, .little); + try rows.writer.writeInt(u64, h, .little); + nrows += 1; + if ((try e.listed.getOrPut(e.gpa, h)).found_existing) continue; + e.blob.clearRetainingCapacity(); + try e.rowBlob(p.node.page(), p.rowAndCell().row, t.cols, &e.blob.writer); + try blobs.writer.writeInt(u64, h, .little); + try blobs.writer.writeInt(u32, @intCast(e.blob.written().len), .little); + try blobs.writer.writeAll(e.blob.written()); + nblobs += 1; + } + try out.writeInt(u32, tr.rows.epoch, .little); + try out.writeInt(u32, nblobs, .little); + try out.writeAll(blobs.written()); + try out.writeInt(u32, nrows, .little); + try out.writeAll(rows.written()); +} + +/// A replica's terminal: what it was sent, written into a ghostty Terminal. +pub const Replica = struct { + seq: u32 = 0, + epoch: u32 = 0, + meta: Hash = 0, + top: u64 = 0, + /// The address of each active row as the Terminal holds it, a list per + /// screen (primary, alternate). + held: [2]std.ArrayList(Hash) = .{ .empty, .empty }, + /// The blobs of the last state applied: what the authority takes it to + /// hold. Owned copies. + blobs: std.AutoHashMapUnmanaged(Hash, []u8) = .empty, + /// History rows it cannot vouch for (written blank, or seen before + /// they last changed), `[lo, hi)` absolute, oldest first. + holes: std.ArrayList([2]u64) = .empty, + /// The one-row page each row blob is decoded into. + scratch: ?Page = null, + + pub fn deinit(r: *Replica, gpa: std.mem.Allocator) void { + for (&r.held) |*h| h.deinit(gpa); + var it = r.blobs.valueIterator(); + while (it.next()) |b| gpa.free(b.*); + r.blobs.deinit(gpa); + r.holes.deinit(gpa); + if (r.scratch) |*s| s.deinit(); + } + + pub const Applied = struct { seq: u32, extra: []const u8 }; + + /// Write one delta into `t`: the seq to acknowledge, and the caller's + /// extra bytes (borrowed from `bytes`). error.BaseMismatch: the delta + /// is against a state this replica does not have (one was lost, or it + /// started afresh), so `t` is as it was and it must be sent a reset. + pub fn apply(r: *Replica, gpa: std.mem.Allocator, io: std.Io, t: *Terminal, bytes: []const u8) !Applied { + var in: std.Io.Reader = .fixed(bytes); + const seq = try in.takeInt(u32, .little); + const base = try in.takeInt(u32, .little); + const flags = try in.takeByte(); + const reset = flags & flag_reset != 0; + if (!reset and base != r.seq) return error.BaseMismatch; + const epoch = try in.takeInt(u32, .little); + const meta = try in.takeInt(u64, .little); + const cx = try in.takeInt(u16, .little); + const cy = try in.takeInt(u16, .little); + const style = try in.takeByte(); + const wrap = try in.takeByte(); + const semantic = try in.takeByte(); + var kitty: KittyFlags = undefined; + @memcpy(std.mem.asBytes(&kitty), try in.take(@sizeOf(KittyFlags))); + const top = try in.takeInt(u64, .little); + const history = try in.takeInt(u64, .little); + const viewport = try in.takeInt(u64, .little); + const extra = try in.take(try in.takeInt(u16, .little)); + const nrows = try in.takeInt(u16, .little); + const hashes = try in.take(@as(usize, nrows) * 8); + const tail = try in.takeInt(u16, .little); + const tails = try in.take(@as(usize, tail) * 8); + // The delta's blobs, borrowed from `bytes` while it applies. + var fresh: std.AutoHashMapUnmanaged(Hash, []const u8) = .empty; + defer fresh.deinit(gpa); + for (0..try in.takeInt(u16, .little)) |_| { + const h = try in.takeInt(u64, .little); + try fresh.put(gpa, h, try in.take(try in.takeInt(u32, .little))); + } + const blobOf = struct { + fn of(rr: *Replica, f: *const std.AutoHashMapUnmanaged(Hash, []const u8), h: Hash) ![]const u8 { + if (f.get(h)) |b| return b; + if (rr.blobs.get(h)) |b| return b; + return error.MissingBlob; + } + }.of; + + if (reset or meta != r.meta) { + var reader: std.Io.Reader = .fixed(try blobOf(r, &fresh, meta)); + var next = try snapshot.terminal.decode(&reader, try snapshot.record.nextHeader(&reader), io, gpa); + if (reset) { + std.mem.swap(Terminal, t, &next); + next.deinit(gpa); + for (&r.held) |*h| h.clearRetainingCapacity(); + r.holes.clearRetainingCapacity(); + r.top = top; + r.epoch = epoch; + // The authority's history, blank, to be fetched. Its own + // limit is the authority's row count (below), not bytes: + // its pages may pack rows otherwise. + const primary = t.screens.get(.primary).?; + primary.pages.setMaxBytes(null); + try scrollUp(primary, history); + if (history > 0) try r.holes.append(gpa, .{ top -| history, top }); + } else { + defer next.deinit(gpa); + try takeMeta(t, &next, io, gpa); + } + r.meta = meta; + } + if (epoch != r.epoch) return error.Renumbered; + + // The primary screen scrolled `top - r.top` rows since the last + // delta: as many go up into the history here (no more than its + // history holds and the screen), each beyond the tail a hole. + const primary = t.screens.get(.primary).?; + if (top > r.top) { + const k = top - r.top; + const held = &r.held[0]; + if (k >= @as(u64, t.rows) + history) { + // A flood: nothing held here is in this state. The rows stay + // where they are, renumbered: the active ones written below, + // the tail too, the rest holes (blanked when they come into + // view, `blank`), not grown and trimmed by the thousand. As + // many more as the history is longer there, grown blank. + const have = primary.pages.total_rows - primary.pages.rows; + if (have < history) try scrollUp(primary, history - have); + held.clearRetainingCapacity(); + try held.appendNTimes(gpa, unknown, t.rows); + r.holes.clearRetainingCapacity(); + if (history > tail) try r.holes.append(gpa, .{ top - history, top - tail }); + } else { + try scrollUp(primary, @min(k, @as(u64, t.rows) + history)); + if (k >= held.items.len) held.clearRetainingCapacity() else { + std.mem.copyForwards(Hash, held.items, held.items[@intCast(k)..]); + held.shrinkRetainingCapacity(held.items.len - @as(usize, @intCast(k))); + } + if (top - tail > r.top) try r.holes.append(gpa, .{ r.top, top - tail }); + } + r.top = top; + } + // The history as long as the authority's (it prunes a page at a + // time, and an ED 3 takes it all): row numbers and the scrollbar + // say the same on both. ponytail: one pruned shorter than that + // (its pages packed otherwise) stays shorter. + const have = primary.pages.total_rows - primary.pages.rows; + if (have > history) primary.pages.eraseHistory(.{ .history = .{ .y = @intCast(have - history - 1) } }); + r.trimHoles(primary); + for (0..tail) |j| { + const p = pinOf(&primary.pages, top, top - tail + j) orelse continue; + const h = std.mem.readInt(u64, tails[j * 8 ..][0..8], .little); + if (rowHash(p.node.page(), p.rowAndCell().row, t.cols) != h) try r.writeRow(gpa, primary, p, try blobOf(r, &fresh, h)); + } + + const screen = t.screens.active; + const held = &r.held[@backingInt(t.screens.active_key)]; + const blank_row = blankHash(t.cols); + while (held.items.len < nrows) try held.append(gpa, blank_row); + held.shrinkRetainingCapacity(nrows); + for (0..nrows) |y| { + const h = std.mem.readInt(u64, hashes[y * 8 ..][0..8], .little); + if (h == held.items[y]) continue; + try r.writeRow(gpa, screen, screen.pages.pin(.{ .active = .{ .y = @intCast(y) } }).?, try blobOf(r, &fresh, h)); + held.items[y] = h; + } + screen.cursorReload(); + screen.cursorAbsolute(@min(cx, t.cols - 1), @min(cy, t.rows - 1)); + screen.cursor.cursor_style = @fromBackingInt(@intCast(style)); + screen.cursor.pending_wrap = wrap != 0; + screen.cursor.semantic_content = @fromBackingInt(@intCast(semantic)); + screen.kitty_keyboard = kitty; + const oldest = r.top -| (primary.pages.total_rows - primary.pages.rows); + primary.pages.scroll(if (viewport == following) .active else .{ .row = @intCast(viewport -| oldest) }); + + // Hold the blobs of this state, and no others. + var keep: std.AutoHashMapUnmanaged(Hash, []u8) = .empty; + errdefer keep.deinit(gpa); + try keep.ensureTotalCapacity(gpa, nrows + tail + 1); + for (0..nrows + tail + 1) |i| { + const h = if (i == nrows + tail) meta else if (i < nrows) + std.mem.readInt(u64, hashes[i * 8 ..][0..8], .little) + else + std.mem.readInt(u64, tails[(i - nrows) * 8 ..][0..8], .little); + if (keep.contains(h)) continue; + const b = if (r.blobs.fetchRemove(h)) |kv| kv.value else try gpa.dupe(u8, try blobOf(r, &fresh, h)); + keep.putAssumeCapacity(h, b); + } + var old = r.blobs.valueIterator(); + while (old.next()) |b| gpa.free(b.*); + r.blobs.deinit(gpa); + r.blobs = keep; + r.seq = seq; + return .{ .seq = seq, .extra = extra }; + } + + /// The holes in `[lo, hi)` blank: until their rows come, a hole shows + /// nothing rather than what that row held before a flood renumbered it. + pub fn blank(r: *const Replica, t: *Terminal, lo: u64, hi: u64) void { + const primary = t.screens.get(.primary).?; + const oldest = r.top -| (primary.pages.total_rows - primary.pages.rows); + for (r.holes.items) |hole| { + const a = @max(lo, hole[0], oldest); + const b = @min(hi, hole[1], r.top); + if (a >= b) continue; + primary.clearRows(.{ .screen = .{ .y = @intCast(a - oldest) } }, .{ .screen = .{ .y = @intCast(b - 1 - oldest) } }, false); + } + } + + /// The holes in `[lo, hi)`: the rows to fetch before showing them. + pub fn missing(r: *const Replica, lo: u64, hi: u64, out: *std.ArrayList([2]u64), gpa: std.mem.Allocator) !void { + for (r.holes.items) |hole| { + const a = @max(lo, hole[0]); + const b = @min(hi, hole[1]); + if (a < b) try out.append(gpa, .{ a, b }); + } + } + + /// The absolute row the viewport of `t`'s primary screen starts at, and + /// how many it shows. + pub fn shown(r: *const Replica, t: *Terminal) [2]u64 { + const pages = &t.screens.get(.primary).?.pages; + const sb = pages.scrollbar(); + const oldest = r.top -| (pages.total_rows - pages.rows); + return .{ oldest + sb.offset, oldest + sb.offset + sb.len }; + } + + /// Write fetched history rows (`encodeRows`) into `t`. + pub fn applyRows(r: *Replica, gpa: std.mem.Allocator, t: *Terminal, bytes: []const u8) !void { + var in: std.Io.Reader = .fixed(bytes); + if (try in.takeInt(u32, .little) != r.epoch) return; // numbered before a resize + const primary = t.screens.get(.primary).?; + var blobs: std.AutoHashMapUnmanaged(Hash, []const u8) = .empty; + defer blobs.deinit(gpa); + for (0..try in.takeInt(u32, .little)) |_| { + const h = try in.takeInt(u64, .little); + try blobs.put(gpa, h, try in.take(try in.takeInt(u32, .little))); + } + for (0..try in.takeInt(u32, .little)) |_| { + const abs = try in.takeInt(u64, .little); + const blob = blobs.get(try in.takeInt(u64, .little)) orelse return error.MissingBlob; + // Only a hole is written: a row the replica holds is the + // authority's already, and newer than what this fetch saw. + if (abs >= r.top or !r.inHole(abs)) continue; + const p = pinOf(&primary.pages, r.top, abs) orelse continue; + try r.writeRow(gpa, primary, p, blob); + try r.fill(gpa, abs); + } + } + + fn inHole(r: *const Replica, abs: u64) bool { + for (r.holes.items) |hole| if (abs >= hole[0] and abs < hole[1]) return true; + return false; + } + + /// Row `abs` is written: out of the holes. + fn fill(r: *Replica, gpa: std.mem.Allocator, abs: u64) !void { + for (r.holes.items, 0..) |*hole, i| { + if (abs < hole[0] or abs >= hole[1]) continue; + if (abs == hole[0]) hole[0] += 1 else if (abs + 1 == hole[1]) hole[1] -= 1 else { + try r.holes.insert(gpa, i + 1, .{ abs + 1, hole[1] }); + r.holes.items[i][1] = abs; + } + if (r.holes.items[i][0] == r.holes.items[i][1]) _ = r.holes.orderedRemove(i); + return; + } + } + + fn trimHoles(r: *Replica, primary: *Screen) void { + clampHoles(&r.holes, r.top -| (primary.pages.total_rows - primary.pages.rows)); + } + + fn writeRow(r: *Replica, gpa: std.mem.Allocator, screen: *Screen, p: Pin, blob: []const u8) !void { + var reader: std.Io.Reader = .fixed(blob); + var dec: snapshot.page.Decoder = undefined; + try dec.init(&reader, try snapshot.record.nextHeader(&reader)); + const src = try scratchPage(&r.scratch, dec.capacity()); + try dec.decode(src, gpa); + if (src.size.cols != screen.pages.cols) return error.BadRow; + _ = screen.clonePartialRowGrowCapacity(p.node, p.y, src, src.getRow(0), 0, src.size.cols); + } +}; + +/// `k` blank rows onto the bottom of a screen's active area, its top `k` +/// going up into the history, as `k` linefeeds at the bottom would. A +/// flood's thousands are grown a row at a time without the cursor (the +/// page list's fast path, a counter), which is put back after. +fn scrollUp(screen: *Screen, k: u64) !void { + if (k == 0) return; + screen.cursorAbsolute(0, screen.pages.rows - 1); + if (k <= screen.pages.rows or screen.no_scrollback or screen.cursor.style_id != 0) { + for (0..@intCast(k)) |_| try screen.cursorDownScroll(); + return; + } + for (0..@intCast(k)) |_| _ = try screen.pages.grow(); + screen.cursorReload(); +} + +/// No row's address: what a row whose content is not known is held as. +const unknown: Hash = std.math.maxInt(Hash); + +/// `rowHash` of a row never written. +fn blankHash(cols: u16) Hash { + var h = std.hash.Wyhash.init(0); + h.update(&.{0}); + h.update(std.mem.asBytes(&cols)); + return h.final(); +} + +/// The terminal-wide state of `next` (a decoded TERMINAL record) into `t`, +/// whose screens and pages stay: what `snapshot.terminal.decode` sets. +fn takeMeta(t: *Terminal, next: *Terminal, io: std.Io, gpa: std.mem.Allocator) !void { + if (t.cols != next.cols or t.rows != next.rows) try t.resize(gpa, .{ .cols = next.cols, .rows = next.rows }); + t.width_px = next.width_px; + t.height_px = next.height_px; + t.scrolling_region = next.scrolling_region; + t.status_display = next.status_display; + t.previous_char = next.previous_char; + t.cursor = next.cursor; + t.modes = next.modes; + t.flags.shell_redraws_prompt = next.flags.shell_redraws_prompt; + t.flags.modify_other_keys_2 = next.flags.modify_other_keys_2; + t.flags.mouse_event = next.flags.mouse_event; + t.flags.mouse_format = next.flags.mouse_format; + t.flags.mouse_shift_capture = next.flags.mouse_shift_capture; + t.flags.password_input = next.flags.password_input; + t.mouse_shape = next.mouse_shape; + t.colors = next.colors; + try t.setPwd(next.getPwd() orelse ""); + try t.setTitle(next.getTitle() orelse ""); + std.mem.swap(@TypeOf(t.tabstops), &t.tabstops, &next.tabstops); + const key = next.screens.active_key; + if (key != t.screens.active_key) { + if (t.screens.get(key) == null) _ = try t.screens.getInit(io, gpa, key, .{ .cols = t.cols, .rows = t.rows, .max_scrollback_bytes = 0 }); + t.screens.switchTo(key); + } +} + +// ---- checks: a replica against its authority, cell for cell ---- + +/// What a row shows, written out value by value (not `rowHash`'s walk): +/// its flags, then each cell's word without its page's ids, its style, its +/// grapheme and its hyperlink's URI. +fn dumpRow(page: *Page, row: *Row, cols: u16, w: *std.Io.Writer) !void { + try w.print("{d}:", .{rowFlags(row)}); + const all = page.getCells(row); + for (all[0..@min(cols, all.len)]) |*c| { + var word = c.*; + word.style_id = 0; + try w.print(" {x}", .{@as(u64, @bitCast(word))}); + if (c.style_id != 0) try snapshot.style.encode(page.styles.get(page.memory, c.style_id).*, w); + if (c.hasGrapheme()) for (page.lookupGrapheme(c).?) |cp| try w.print("+{x}", .{cp}); + if (c.hyperlink) if (page.lookupHyperlink(c)) |id| try w.print("@{s}", .{page.hyperlink_set.get(page.memory, id).uri.slice(page.memory)}); + } + try w.writeByte('\n'); +} + +/// The state a replica must match: the terminal-wide state, the cursor, +/// the history's extent and the viewport, the active screen's rows. +pub fn dumpScreen(t: *Terminal, w: *std.Io.Writer) !void { + const screen = t.screens.active; + const sb = t.screens.get(.primary).?.pages.scrollbar(); + try w.print("{d}x{d} {s} cursor {d},{d} {s} {} {s} title {s} pwd {s} scrollbar {d} {d} {d}\n", .{ + t.cols, t.rows, @tagName(t.screens.active_key), + screen.cursor.x, screen.cursor.y, @tagName(screen.cursor.cursor_style), + screen.cursor.pending_wrap, @tagName(screen.cursor.semantic_content), t.getTitle() orelse "", + t.getPwd() orelse "", sb.total, sb.offset, + sb.len, + }); + try w.writeAll(std.mem.asBytes(&t.modes.values)); + try w.writeAll(std.mem.asBytes(&screen.kitty_keyboard)); + try w.print(" mouse {s} {s} {s}\n", .{ @tagName(t.flags.mouse_event), @tagName(t.flags.mouse_format), @tagName(t.mouse_shape) }); + for (t.colors.palette.current) |c| try w.print("{x}{x}{x}", .{ c.r, c.g, c.b }); + try w.writeByte('\n'); + var it = screen.pages.rowIterator(.right_down, .{ .active = .{} }, null); + while (it.next()) |p| try dumpRow(p.node.page(), p.rowAndCell().row, t.cols, w); +} + +/// An authority and its replica, fed one stream of pty bytes, the deltas +/// and fetches between them at random: a delta encoded at a random step +/// (so the states between are skipped), several in flight and each landing +/// a random number of steps later, in order, one lost now and then (the +/// replica refuses the next and is started afresh), random resizes of the +/// authority, random fetches of holes. After each delta, the replica's +/// screen is the authority's as it was encoded, and every history row it +/// holds outside its holes is what the authority holds at that absolute +/// row. +pub const Soak = struct { + gpa: std.mem.Allocator, + io: std.Io, + a: *Terminal, + stream: vt.TerminalStream, + track: Track, + enc: Encoder, + peer: Peer = .{}, + /// The authority's writes, as terminal.State counts them. + changes: u32 = 0, + b: *Terminal, + rep: Replica = .{}, + prng: std.Random.DefaultPrng, + /// The deltas in flight, oldest first, each with the authority's dump + /// when it was encoded and the step it lands at. + flight: std.ArrayList(struct { delta: []u8, expect: []u8, at: u64 }) = .empty, + got: std.Io.Writer.Allocating, + now: u64 = 0, + resizes: bool = true, + drops: bool = true, + /// The replica refused a delta and the authority started it afresh: + /// what was in flight before is refused too. + refused: bool = false, + stats: struct { deltas: u64 = 0, bytes: u64 = 0, fetched: u64 = 0, history_checked: u64 = 0, lost: u64 = 0, refused: u64 = 0, most_in_flight: u64 = 0 } = .{}, + + pub fn init(gpa: std.mem.Allocator, io: std.Io, cols: u16, rows: u16, scrollback: usize, seed: u64) !*Soak { + const s = try gpa.create(Soak); + errdefer gpa.destroy(s); + const a = try gpa.create(Terminal); + a.* = try Terminal.init(io, gpa, .{ .cols = cols, .rows = rows, .max_scrollback_bytes = scrollback }); + const b = try gpa.create(Terminal); + // Room to spare: its pages may pack rows otherwise, and it keeps the + // authority's history to the row. + b.* = try Terminal.init(io, gpa, .{ .cols = 2, .rows = 2, .max_scrollback_bytes = 2 * scrollback }); + s.* = .{ + .gpa = gpa, + .io = io, + .a = a, + .stream = a.vtStream(), + .track = .init(gpa), + .enc = .init(gpa), + .b = b, + .prng = .init(seed), + .got = .init(gpa), + }; + return s; + } + + pub fn deinit(s: *Soak) void { + const gpa = s.gpa; + s.stream.deinit(); + s.track.deinit(gpa, s.a); + s.a.deinit(gpa); + gpa.destroy(s.a); + s.b.deinit(gpa); + gpa.destroy(s.b); + s.enc.deinit(); + s.peer.deinit(gpa); + s.rep.deinit(gpa); + for (s.flight.items) |f| { + gpa.free(f.delta); + gpa.free(f.expect); + } + s.flight.deinit(gpa); + s.got.deinit(); + gpa.destroy(s); + } + + /// `bytes` into the authority in random pieces, a step after each. + pub fn feed(s: *Soak, bytes: []const u8) !void { + var at: usize = 0; + const random = s.prng.random(); + while (at < bytes.len) { + const n = @min(bytes.len - at, 1 + random.uintLessThan(usize, 512)); + s.stream.nextSlice(bytes[at..][0..n]); + s.changes +%= 1; + at += n; + try s.step(); + } + } + + pub fn step(s: *Soak) !void { + const random = s.prng.random(); + s.now += 1; + if (s.resizes and random.uintLessThan(u32, 200) == 0) { + try s.a.resize(s.gpa, .{ .cols = 10 + random.uintLessThan(u16, 200), .rows = 3 + random.uintLessThan(u16, 60) }); + s.changes +%= 1; + } + // A frame now and then; the states between are skipped. + if (random.boolean()) try s.send(s.now + random.uintLessThan(u64, 12)); + while (s.flight.items.len > 0 and s.flight.items[0].at <= s.now) try s.land(); + if (random.uintLessThan(u32, 8) == 0) try s.fetch(); + } + + /// A delta, if the peer is owed one, sent to land at step `at`. + fn send(s: *Soak, at: u64) !void { + var d: std.Io.Writer.Allocating = .init(s.gpa); + defer d.deinit(); + if (!try encode(&s.enc, &s.track, &s.peer, s.a, s.changes, "extra", &d.writer)) return; + var expect: std.Io.Writer.Allocating = .init(s.gpa); + defer expect.deinit(); + try dumpScreen(s.a, &expect.writer); + const last = if (s.flight.items.len > 0) s.flight.items[s.flight.items.len - 1].at else 0; + try s.flight.append(s.gpa, .{ .delta = try d.toOwnedSlice(), .expect = try expect.toOwnedSlice(), .at = @max(at, last) }); + s.stats.deltas += 1; + s.stats.bytes += s.flight.items[s.flight.items.len - 1].delta.len; + s.stats.most_in_flight = @max(s.stats.most_in_flight, s.flight.items.len); + } + + /// The oldest delta in flight applied (or lost), acknowledged, checked. + fn land(s: *Soak) !void { + const f = s.flight.orderedRemove(0); + defer s.gpa.free(f.delta); + defer s.gpa.free(f.expect); + if (s.drops and s.prng.random().uintLessThan(u32, 50) == 0) { + s.stats.lost += 1; + return; + } + const applied = s.rep.apply(s.gpa, s.io, s.b, f.delta) catch |err| switch (err) { + error.BaseMismatch => { + // Asked once: the authority starts it afresh. + if (!s.refused) s.peer.reset(); + s.refused = true; + s.stats.refused += 1; + return; + }, + else => return err, + }; + s.refused = false; + if (!std.mem.eql(u8, applied.extra, "extra")) return error.ExtraLost; + s.peer.ack(applied.seq); + s.got.clearRetainingCapacity(); + try dumpScreen(s.b, &s.got.writer); + if (!std.mem.eql(u8, f.expect, s.got.written())) { + reportDiff(f.expect, s.got.written()); + return error.ReplicaDiffers; + } + try s.checkHistory(); + } + + /// Everything in flight landed, then a last delta of the end, until the + /// replica holds it: what the replica shows at quiescence. + fn settle(s: *Soak) !void { + s.drops = false; + for (0..4) |_| { + while (s.flight.items.len > 0) try s.land(); + try s.send(0); + if (s.flight.items.len == 0) return; + } + return error.NeverSettled; + } + + /// Every history row the replica holds, outside its holes and inside + /// what the authority still has, is the authority's (rows in history + /// never change; a resize renumbers and starts the replica afresh). + fn checkHistory(s: *Soak) !void { + // Numbered as of now: the authority may have scrolled on since. + try s.track.rows.update(s.a); + if (s.rep.epoch != s.track.rows.epoch) return; + const pages = &s.b.screens.get(.primary).?.pages; + const have = pages.total_rows - pages.rows; + var ra: std.Io.Writer.Allocating = .init(s.gpa); + defer ra.deinit(); + var rb: std.Io.Writer.Allocating = .init(s.gpa); + defer rb.deinit(); + var d: u64 = 1; + while (d <= have) : (d += 1 + d / 8) { + const abs = s.rep.top - d; + if (s.rep.inHole(abs)) continue; + const pb = pinOf(pages, s.rep.top, abs) orelse continue; + const pa = s.track.rows.pinAt(s.a, abs) orelse continue; + ra.clearRetainingCapacity(); + rb.clearRetainingCapacity(); + try dumpRow(pa.node.page(), pa.rowAndCell().row, s.a.cols, &ra.writer); + try dumpRow(pb.node.page(), pb.rowAndCell().row, s.b.cols, &rb.writer); + if (!std.mem.eql(u8, ra.written(), rb.written())) { + std.debug.print("history row {d} ({d} above the top) differs\n", .{ abs, d }); + reportDiff(ra.written(), rb.written()); + return error.ReplicaHistoryDiffers; + } + s.stats.history_checked += 1; + } + } + + /// Some holes fetched from the authority, as a replica scrolled back. + fn fetch(s: *Soak) !void { + if (s.rep.holes.items.len == 0) return; + const random = s.prng.random(); + const hole = s.rep.holes.items[random.uintLessThan(usize, s.rep.holes.items.len)]; + const lo = hole[0] + random.uintLessThan(u64, hole[1] - hole[0]); + const hi = @min(hole[1], lo + 1 + random.uintLessThan(u64, 80)); + s.rep.blank(s.b, lo, hi); + var out: std.Io.Writer.Allocating = .init(s.gpa); + defer out.deinit(); + try encodeRows(&s.enc, &s.track, s.a, lo, hi, &out.writer); + try s.rep.applyRows(s.gpa, s.b, out.written()); + s.stats.fetched += hi - lo; + try s.checkHistory(); + } +}; + +fn reportDiff(want: []const u8, got: []const u8) void { + var wl = std.mem.splitScalar(u8, want, '\n'); + var gl = std.mem.splitScalar(u8, got, '\n'); + var line: usize = 0; + while (true) : (line += 1) { + const a = wl.next(); + const b = gl.next(); + if (a == null and b == null) return; + if (a != null and b != null and std.mem.eql(u8, a.?, b.?)) continue; + std.debug.print("line {d} differs:\n authority {s}\n replica {s}\n", .{ line, a orelse "(none)", b orelse "(none)" }); + return; + } +} + +/// Drive one stream through a soak, then settle: the replica's screen at +/// quiescence is the authority's. +pub fn soak(gpa: std.mem.Allocator, io: std.Io, bytes: []const u8, cols: u16, rows: u16, scrollback: usize, seed: u64, resizes: bool) !@FieldType(Soak, "stats") { + const s = try Soak.init(gpa, io, cols, rows, scrollback, seed); + defer s.deinit(); + s.resizes = resizes; + try s.feed(bytes); + try s.settle(); + return s.stats; +} + +const testing = std.testing; + +/// A shell's: prompts marked (OSC 133), colours of the three kinds, wide +/// and combined graphemes, hyperlinks, tabs, a title and a directory, long +/// lines that wrap, and a clear (ED2, ED3) now and then. +pub fn shellStream(gpa: std.mem.Allocator) ![]u8 { + var out: std.Io.Writer.Allocating = .init(gpa); + errdefer out.deinit(); + const w = &out.writer; + try w.writeAll("\x1b]0;a shell\x07\x1b]7;file://host/home/someone\x07"); + for (0..400) |i| { + try w.print("\x1b]133;A\x07$ \x1b]133;B\x07ls -l {d}\r\n\x1b]133;C\x07", .{i}); + try w.print("\x1b[1;31mred\x1b[0m \x1b[4;38;5;{d}m256\x1b[0m \x1b[38;2;{d};100;200;48;5;17mtrue\x1b[0m 漢字 e\u{301} 👩\u{200d}💻 ", .{ i % 256, i % 256 }); + try w.print("\x1b]8;;https://example.com/{d}\x07link\x1b]8;;\x07\tx\r\n", .{i}); + if (i % 7 == 0) { + for (0..12) |_| try w.writeAll("a long line that wraps "); + try w.writeAll("\r\n"); + } + try w.writeAll("\x1b]133;D;0\x07"); + if (i % 97 == 96) try w.writeAll("\x1b[H\x1b[2J\x1b[3J"); + if (i % 50 == 0) try w.print("\x1b]2;title {d}\x07\x1b]4;1;rgb:{x:0>2}/20/30\x07", .{ i, i % 256 }); + } + return out.toOwnedSlice(); +} + +/// A full-screen program's: the alternate screen and its modes, a status +/// line, a scrolling region scrolled both ways, lines inserted and +/// deleted, cursor shapes, the kitty keyboard pushed and popped. +pub fn tuiStream(gpa: std.mem.Allocator) ![]u8 { + var out: std.Io.Writer.Allocating = .init(gpa); + errdefer out.deinit(); + const w = &out.writer; + try w.writeAll("before the program\r\n\x1b[?1049h\x1b[?25l\x1b[?1000h\x1b[?1006h\x1b[?2004h\x1b[?1h\x1b[>1u\x1b[4 q\x1b[H\x1b[2J"); + for (0..300) |f| { + try w.print("\x1b[1;1H\x1b[44;97m status {d} \x1b[K\x1b[0m", .{f}); + try w.print("\x1b[3;18r\x1b[18;1H\r\n\x1b[3{d}mline {d}\x1b[0m", .{ f % 8, f }); + if (f % 3 == 0) try w.writeAll("\x1b[3;1H\x1bM\x1b[2mreversed in\x1b[0m"); + if (f % 5 == 0) try w.writeAll("\x1b[6;1H\x1b[2L\x1b[9;1H\x1b[M"); + try w.writeAll("\x1b[r"); + try w.print("\x1b[{d};{d}H\x1b[7m*\x1b[0m\x1b[{d}X", .{ 2 + f % 20, 1 + (f * 7) % 70, f % 5 }); + if (f % 11 == 0) try w.writeAll("\x1b[?25h\x1b[6 q") else if (f % 11 == 5) try w.writeAll("\x1b[?25l\x1b[2 q"); + } + try w.writeAll("\x1b[ 4 << 20) return error.BadGpuFrame; + const gpu = try gpa.dupe(u8, try r.take(n)); + gpa.free(sh.gpu); + sh.gpu = gpu; + if (lacking == sh.wanted) return 0; + sh.wanted = lacking; + return lacking; + } +}; + +/// A raster's strip: at most this many bytes of its rows (17.3). +pub const strip_max = 4 << 20; + +/// How many of a `w`-pixel raster's rows a strip holds. +pub fn stripRows(w: u32) u32 { + return @max(1, strip_max / (@as(u32, @max(w, 1)) * 4)); +} + +/// The strips of `pl`'s raster over its `src` rows, a bit each. +fn stripsOver(pl: *const Place) u64 { + const per = stripRows(pl.w); + const lo = @min(pl.src.y, pl.h) / per; + const hi = (@min(pl.src.y +| pl.src.h, pl.h) + per - 1) / per; + var bits: u64 = 0; + for (lo..@min(hi, 64)) |i| bits |= @as(u64, 1) << @intCast(i); + return bits; +} + +/// Strip `i` of `pl`'s raster, a `raster` message's payload: its key, its +/// size, its first row, and its rows' bytes. +pub fn encodeStrip(pl: *const Place, i: u32, w: *std.Io.Writer) !void { + const y0 = i * stripRows(pl.w); + const row = @as(usize, pl.w) * 4; + try w.writeInt(u64, pl.key, .little); + try w.writeInt(u32, pl.w, .little); + try w.writeInt(u32, pl.h, .little); + try w.writeInt(u32, y0, .little); + try w.writeAll(pl.rgba[y0 * row .. @min(pl.h, y0 + stripRows(pl.w)) * row]); +} + +/// A peer's rasters of the panes of a kind, made from their strips by +/// name (the key with its pane's serial), kept to `max` bytes by least +/// recent use but for those a Shadow shows (17.3). +pub const Blobs = struct { + list: std.ArrayList(Blob) = .empty, + clock: u64 = 0, + + pub const max = 64 << 20; + const Blob = struct { name: u64, w: u32, h: u32, bytes: []u8, have: u64 = 0, used: u64 = 0 }; + + pub fn deinit(b: *Blobs, gpa: std.mem.Allocator) void { + for (b.list.items) |x| gpa.free(x.bytes); + b.list.deinit(gpa); + } + + /// A `raster` message's payload (`encodeStrip`'s) into raster `name`, + /// made at its first strip; `shown`, the pixels Shadows show now. + pub fn strip(b: *Blobs, gpa: std.mem.Allocator, name: u64, payload: []const u8, shown: []const [*]const u8) !void { + var r: std.Io.Reader = .fixed(payload); + _ = try r.takeInt(u64, .little); + const w = try r.takeInt(u32, .little); + const h = try r.takeInt(u32, .little); + const y0 = try r.takeInt(u32, .little); + const rows = r.buffered(); + const row = @as(usize, w) * 4; + if (row == 0 or rows.len % row != 0 or y0 % stripRows(w) != 0 or y0 + rows.len / row > h) return error.BadShadow; + const x = for (b.list.items) |*x| { + if (x.name == name) break x; + } else made: { + var held: usize = row * h; + for (b.list.items) |x| held += x.bytes.len; + while (held > max) { + var old: ?usize = null; + for (b.list.items, 0..) |x, i| if (std.mem.indexOfScalar([*]const u8, shown, x.bytes.ptr) == null and (old == null or x.used < b.list.items[old.?].used)) { + old = i; + }; + const i = old orelse break; + held -= b.list.items[i].bytes.len; + gpa.free(b.list.swapRemove(i).bytes); + } + const bytes = try gpa.alloc(u8, row * h); + errdefer gpa.free(bytes); + try b.list.append(gpa, .{ .name = name, .w = w, .h = h, .bytes = bytes }); + break :made &b.list.items[b.list.items.len - 1]; + }; + if (x.w != w or x.h != h) return error.BadShadow; + @memcpy(x.bytes[y0 * row ..][0..rows.len], rows); + x.have |= @as(u64, 1) << @intCast(@min(63, y0 / stripRows(w))); + b.clock += 1; + x.used = b.clock; + } + + /// Raster `name`'s pixels when it has every strip `pl` shows. + fn shows(b: *Blobs, name: u64, pl: *const Place) ?[]u8 { + for (b.list.items) |*x| if (x.name == name and x.w == pl.w and x.h == pl.h and stripsOver(pl) & ~x.have == 0) { + b.clock += 1; + x.used = b.clock; + return x.bytes; + }; + return null; + } +}; + +/// What the authority last sent one peer of a pane of a kind: its rows' +/// hashes, its placements, and which strips of each raster it holds. +pub const ShadowSent = struct { + serial: u32 = 0, + cols: u16 = 0, + hashes: std.ArrayList(Hash) = .empty, + places: Hash = 0, + keys: std.ArrayList(struct { key: u64, have: u64 }) = .empty, + + pub fn deinit(s: *ShadowSent, gpa: std.mem.Allocator) void { + s.hashes.deinit(gpa); + s.keys.deinit(gpa); + s.* = .{}; + } + + /// The strips of `pl`'s raster over its `src` the peer lacks (a bit + /// each, `encodeStrip`'s `i`), counted sent now. + pub fn owed(s: *ShadowSent, gpa: std.mem.Allocator, pl: *const Place) !u64 { + const need = stripsOver(pl); + for (s.keys.items) |*k| if (k.key == pl.key) { + defer k.have |= need; + return need & ~k.have; + }; + try s.keys.append(gpa, .{ .key = pl.key, .have = need }); + return need; + } +}; + +/// Frame `f` of a pane (the pointer its paint asked for) for a peer that +/// was last sent `sent`: false, and nothing written, when it holds this +/// frame already. Its placements cross by key, their pixels as strips +/// before it (`ShadowSent.owed`), so a frame is small whatever it shows. +pub fn encodeShadow(gpa: std.mem.Allocator, sent: *ShadowSent, f: *const Kind.Frame, pointer: u8, w: *std.Io.Writer) !bool { + var hashes: std.ArrayList(Hash) = .empty; + errdefer hashes.deinit(gpa); + for (0..f.rows) |y| try hashes.append(gpa, std.hash.Wyhash.hash(0, std.mem.sliceAsBytes(f.cells[y * f.cols ..][0..f.cols]))); + const places = f.places[0..@min(f.n, 64)]; + var ph = std.hash.Wyhash.init(pointer); + for (places) |*pl| inline for (.{ pl.w, pl.h, pl.src, pl.at, pl.dst, pl.key }) |v| ph.update(std.mem.asBytes(&v)); + if (f.gpu.len != 0) ph.update(f.gpu); + const places_hash = ph.final(); + if (sent.cols == f.cols and std.mem.eql(Hash, sent.hashes.items, hashes.items) and sent.places == places_hash) { + hashes.deinit(gpa); + return false; + } + try w.writeInt(u16, f.cols, .little); + try w.writeInt(u16, f.rows, .little); + try w.writeByte(pointer); + for (hashes.items, 0..) |h, y| { + try w.writeInt(u64, h, .little); + // Held only at the same size: a peer's Shadow of another starts afresh. + const held = sent.cols == f.cols and sent.hashes.items.len == f.rows and sent.hashes.items[y] == h; + try w.writeByte(@intFromBool(!held)); + if (!held) for (f.cells[y * f.cols ..][0..f.cols]) |*c| { + try w.writeAll(&c.text); + try w.writeInt(u32, c.fg, .little); + try w.writeInt(u32, c.bg, .little); + try w.writeInt(u16, c.attrs, .little); + try w.writeByte(c.len); + try w.writeByte(c.flags); + }; + } + try w.writeByte(@intCast(places.len)); + for (places) |pl| { + try w.writeInt(u32, pl.w, .little); + try w.writeInt(u32, pl.h, .little); + inline for (.{ pl.src.x, pl.src.y, pl.src.w, pl.src.h, pl.at.x, pl.at.y, pl.at.w, pl.at.h }) |v| try w.writeInt(u32, v, .little); + inline for (.{ pl.dst.x, pl.dst.y, pl.dst.w, pl.dst.h }) |v| try w.writeInt(u16, v, .little); + try w.writeInt(u64, pl.key, .little); + } + if (f.gpu.len > 4 << 20) return error.BadGpuFrame; + try w.writeInt(u32, @intCast(f.gpu.len), .little); + try w.writeAll(f.gpu); + sent.hashes.deinit(gpa); + sent.hashes = hashes; + sent.cols = f.cols; + sent.places = places_hash; + // The strips of the rasters this frame shows: a key gone is sent whole again. + var i: usize = 0; + while (i < sent.keys.items.len) { + const kept = for (places) |pl| { + if (pl.key == sent.keys.items[i].key) break true; + } else false; + if (kept) i += 1 else _ = sent.keys.swapRemove(i); + } + return true; +} + +test "a replica's screen is its authority's, cell for cell, as states are skipped, resized and fetched" { + const gpa = testing.allocator; + const io = testing.io; + const shell = try shellStream(gpa); + defer gpa.free(shell); + const tui = try tuiStream(gpa); + defer gpa.free(tui); + const yes = try yesStream(gpa, 30_000); + defer gpa.free(yes); + var fetched: u64 = 0; + var history: u64 = 0; + for (0..4) |seed| { + // A small scrollback, so it wraps (pruned at its oldest pages). + for ([_][]const u8{ shell, tui, yes }) |bytes| { + const st = soak(gpa, io, bytes, 80, 24, 64 * 1024, seed, seed != 0) catch |err| { + std.debug.print("soak seed {d} stream {d} bytes: {s}\n", .{ seed, bytes.len, @errorName(err) }); + return err; + }; + try testing.expect(st.deltas > 10); + fetched += st.fetched; + history += st.history_checked; + } + } + // The holes were fetched, and history rows compared, at all. + try testing.expect(fetched > 0 and history > 0); +} + +test "under a flood a delta is a screen of hashes: an unchanged row costs its address alone" { + const gpa = testing.allocator; + const s = try Soak.init(gpa, testing.io, 200, 60, 1 << 20, 1); + defer s.deinit(); + s.resizes = false; + s.drops = false; + const yes = try yesStream(gpa, 10_000); + defer gpa.free(yes); + var last: []u8 = &.{}; + defer gpa.free(last); + for (0..3) |_| { + s.stream.nextSlice(yes); + s.changes += 1; + try s.send(0); + try testing.expectEqual(@as(usize, 1), s.flight.items.len); + gpa.free(last); + last = try gpa.dupe(u8, s.flight.items[0].delta); + try s.land(); + } + // The last: 60 rows and the tail's 60, of hashes the replica holds, no + // blob; the header and counts around them. + const d = last; + const at = 4 + 4 + 1 + 4 + 8 + 2 + 2 + 1 + 1 + 1 + @sizeOf(KittyFlags) + 8 + 8 + 8 + 2 + "extra".len; + const nrows = std.mem.readInt(u16, d[at..][0..2], .little); + const tail = std.mem.readInt(u16, d[at + 2 + nrows * 8 ..][0..2], .little); + const blobs = std.mem.readInt(u16, d[at + 4 + (nrows + tail) * 8 ..][0..2], .little); + if (d.len >= (nrows + tail) * 8 + 128) std.debug.print("delta {d} bytes: {d} rows, {d} tail, {d} blobs\n", .{ d.len, nrows, tail, blobs }); + try testing.expectEqual(@as(u16, 0), blobs); + try testing.expect(d.len < (nrows + tail) * 8 + 128); + try testing.expect(s.rep.holes.items.len > 0); +} + +test "a reflow to fewer columns keeps every history row numbered at or past 0" { + const gpa = testing.allocator; + const s = try Soak.init(gpa, testing.io, 80, 24, 1 << 20, 3); + defer s.deinit(); + s.resizes = false; + s.drops = false; + // Lines of 69 columns scrolled into the history, then the screen + // narrowed so that each wraps in three: more rows above the last top + // than ever scrolled past it (a window client's font, 2026-10-30: the + // session's peer started afresh at `top - history`, which overflowed). + var line: [80]u8 = undefined; + for (0..200) |i| { + s.stream.nextSlice(try std.fmt.bufPrint(&line, "{d:0>4} xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n", .{i})); + s.changes += 1; + } + try s.send(0); + try s.land(); + try s.a.resize(gpa, .{ .cols = 30, .rows = 24 }); + s.changes += 1; + try s.send(0); + try s.land(); + try s.settle(); + try testing.expect(s.track.rows.top >= s.track.history); +} + +test "several deltas in flight land in order, and a peer at the bound waits for an acknowledgement" { + const gpa = testing.allocator; + const s = try Soak.init(gpa, testing.io, 80, 24, 1 << 20, 2); + defer s.deinit(); + s.resizes = false; + s.drops = false; + var line: [64]u8 = undefined; + for (0..bound + 5) |i| { + s.stream.nextSlice(try std.fmt.bufPrint(&line, "line {d}\r\n", .{i})); + s.changes += 1; + try s.send(1000); + } + // The bound's worth went; the rest wait, and are skipped. + try testing.expectEqual(@as(usize, bound), s.flight.items.len); + try s.land(); + try s.send(1000); + try testing.expectEqual(@as(usize, bound), s.flight.items.len); + try s.settle(); + try testing.expectEqual(@as(u64, 0), s.stats.refused); +} + +test "a replica's terminal pane draws as its authority's does" { + const pardes = @import("../pardes.zig"); + const gpa = testing.allocator; + const a = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 16 }); + defer a.deinit(); + const b = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 16 }); + defer b.deinit(); + while (a.nextEffect()) |_| {} + while (b.nextEffect()) |_| {} + var fa = std.heap.ArenaAllocator.init(gpa); + defer fa.deinit(); + var fb = std.heap.ArenaAllocator.init(gpa); + defer fb.deinit(); + // Laid out once, so both panes have their size before the first delta. + _ = try a.render(fa.allocator()); + _ = try b.render(fb.allocator()); + const ta = &a.panes[0].?.terminal.?.vt; + const tb = &b.panes[0].?.terminal.?.vt; + var enc: Encoder = .init(gpa); + defer enc.deinit(); + var peer: Peer = .{}; + defer peer.deinit(gpa); + var tr: Track = .init(gpa); + defer tr.deinit(gpa, ta); + var rep: Replica = .{}; + defer rep.deinit(gpa); + var delta: std.Io.Writer.Allocating = .init(gpa); + defer delta.deinit(); + var line: [256]u8 = undefined; + for (0..60) |i| { + const bytes = try std.fmt.bufPrint(&line, "\x1b[1;3{d}m{d}\x1b[0m 漢字 e\u{301} \x1b[48;2;{d};40;60mbg\x1b[0m and then some text that wraps past the width of the pane\r\n", .{ i % 8, i, i * 4 % 256 }); + a.update(.{ .output = .{ .pane = 0, .bytes = bytes } }); + delta.clearRetainingCapacity(); + try testing.expect(try encode(&enc, &tr, &peer, ta, @intCast(i + 1), "", &delta.writer)); + peer.ack((try rep.apply(gpa, std.Io.Threaded.global_single_threaded.io(), tb, delta.written())).seq); + b.needs_frame = true; + _ = fa.reset(.retain_capacity); + _ = fb.reset(.retain_capacity); + const sa = try a.render(fa.allocator()); + const sb = try b.render(fb.allocator()); + const r = a.view().rects[0]; + // The body: below the pane's tag row. + for (r.y + 1..r.y + r.h) |y| for (r.x..r.x + r.w) |x| { + const ca = sa.at(@intCast(x), @intCast(y)); + const cb = sb.at(@intCast(x), @intCast(y)); + if (!ca.visuallyEqual(cb)) { + std.debug.print("step {d}: cell {d},{d}: '{s}' vs '{s}'\n", .{ i, x, y, ca.grapheme(), cb.grapheme() }); + return error.ReplicaDrawsDifferently; + } + }; + } +} + +test "a pane of a kind's frame a row taller or shorter is drawn whole at its peer, never a row it let go" { + const gpa = std.testing.allocator; + var cells: [2 * 3]Cell = @splat(std.mem.zeroes(Cell)); + cells[0] = .{ .text = "a".* ++ @as([7]u8, @splat(0)), .fg = 1, .bg = 2, .attrs = 0, .len = 1, .flags = Kind.c.PA_PAINTED }; + var f: Kind.Frame = .{ .cells = &cells, .cols = 2, .rows = 2 }; + var sent: ShadowSent = .{}; + defer sent.deinit(gpa); + var blobs: Blobs = .{}; + const sh = try gpa.create(Shadow); + sh.* = .{}; + defer sh.deinit(gpa); + var out: std.Io.Writer.Allocating = .init(gpa); + defer out.deinit(); + for ([_]u16{ 2, 3, 2 }) |rows| { + f.rows = rows; + out.clearRetainingCapacity(); + try std.testing.expect(try encodeShadow(gpa, &sent, &f, 0, &out.writer)); + _ = try sh.apply(gpa, out.written(), 1, &blobs); + try std.testing.expectEqualStrings("a", sh.cells[0].text[0..1]); + } +} + +test "a pane of a kind crosses as its frame, a row the peer holds as its hash, a raster by key after its strips over what it shows" { + const gpa = std.testing.allocator; + var cells: [3 * 2]Cell = @splat(std.mem.zeroes(Cell)); + cells[0] = .{ .text = "a".* ++ @as([7]u8, @splat(0)), .fg = 1, .bg = 2, .attrs = 0, .len = 1, .flags = Kind.c.PA_PAINTED }; + // A raster of three strips (4 MiB a strip at 1,024 px), of which it shows the second. + const w: u32 = 1024; + const per = stripRows(w); + const pixels = try gpa.alloc(u8, @as(usize, w) * per * 3 * 4); + defer gpa.free(pixels); + @memset(pixels, 5); + var f: Kind.Frame = .{ .cells = &cells, .cols = 3, .rows = 2, .n = 1 }; + f.buf[0] = std.mem.zeroInit(Place, .{ .rgba = pixels.ptr, .w = w, .h = per * 3, .src = .{ .x = 0, .y = per + 1, .w = w, .h = 10 }, .key = 9 }); + f.places = &f.buf; + var sent: ShadowSent = .{}; + defer sent.deinit(gpa); + var blobs: Blobs = .{}; + defer blobs.deinit(gpa); + const sh = try gpa.create(Shadow); + sh.* = .{}; + defer sh.deinit(gpa); + var out: std.Io.Writer.Allocating = .init(gpa); + defer out.deinit(); + const name = std.hash.Wyhash.hash(1, std.mem.asBytes(&f.buf[0].key)); + try std.testing.expectEqual(@as(u64, 0b010), try sent.owed(gpa, &f.buf[0])); + try encodeStrip(&f.buf[0], 1, &out.writer); + try std.testing.expect(out.written().len <= strip_max + 20); + try blobs.strip(gpa, name, out.written(), &.{}); + out.clearRetainingCapacity(); + try std.testing.expect(try encodeShadow(gpa, &sent, &f, 0, &out.writer)); + try std.testing.expectEqual(@as(u64, 0), try sh.apply(gpa, out.written(), 1, &blobs)); + try std.testing.expectEqualStrings("a", sh.cells[0].text[0..1]); + try std.testing.expectEqual(@as(u8, 5), sh.places.items[0].rgba[per * w * 4]); + // The same frame again: nothing to send, no strip owed. + out.clearRetainingCapacity(); + try std.testing.expect(!try encodeShadow(gpa, &sent, &f, 0, &out.writer)); + try std.testing.expectEqual(@as(u64, 0), try sent.owed(gpa, &f.buf[0])); + // Its src moves into the third strip: that one is owed, and a peer + // without it does not show the raster and asks for it, once. + f.buf[0].src.y = 2 * per + 1; + try std.testing.expectEqual(@as(u64, 0b100), try sent.owed(gpa, &f.buf[0])); + out.clearRetainingCapacity(); + try std.testing.expect(try encodeShadow(gpa, &sent, &f, 0, &out.writer)); + try std.testing.expect(out.written().len < 4 + 1 + 2 * 9 + 60 + 8); + try std.testing.expectEqual(name, try sh.apply(gpa, out.written(), 1, &blobs)); + try std.testing.expectEqual(@as(usize, 0), sh.places.items.len); + try std.testing.expectEqual(@as(u64, 0), try sh.apply(gpa, out.written(), 1, &blobs)); +} + +test "optional GPU frames cross shadows, change independently, and disappear" { + const gpa = std.testing.allocator; + var sent: ShadowSent = .{}; + defer sent.deinit(gpa); + const shadow = try gpa.create(Shadow); + shadow.* = .{}; + defer shadow.deinit(gpa); + var blobs: Blobs = .{}; + defer blobs.deinit(gpa); + var cell = std.mem.zeroes(Cell); + var frame: Kind.Frame = .{ .cols = 1, .rows = 1, .cells = @as([*]Cell, @ptrCast(&cell))[0..1], .gpu = "first scene" }; + var out: std.Io.Writer.Allocating = .init(gpa); + defer out.deinit(); + for ([_][]const u8{ "first scene", "second scene", "" }) |bytes| { + frame.gpu = bytes; + out.clearRetainingCapacity(); + try std.testing.expect(try encodeShadow(gpa, &sent, &frame, 0, &out.writer)); + _ = try shadow.apply(gpa, out.written(), 1, &blobs); + try std.testing.expectEqualStrings(bytes, shadow.gpu); + out.clearRetainingCapacity(); + try std.testing.expect(!try encodeShadow(gpa, &sent, &frame, 0, &out.writer)); + } +} diff --git a/src/term/terminal.zig b/src/term/terminal.zig new file mode 100644 index 00000000..bc5e7fd6 --- /dev/null +++ b/src/term/terminal.zig @@ -0,0 +1,3411 @@ +//! A terminal pane: its VT emulator, prompt rows, the edit overlay +//! typed over shell rows, and the Filter that recolours output to the theme. +const panes = @import("../panes.zig"); +const std = @import("std"); +const pardes = @import("../pardes.zig"); +const config = @import("../config.zig"); +const Pardes = pardes.Pardes; +const Surface = pardes.Surface; +const modal = @import("../modal.zig"); +const tracy = @import("../tracy.zig"); +const tagline = @import("../tagline.zig"); +const Key = pardes.Key; +const Pane = panes.Pane; +const EditText = panes.EditText; +const File = panes.File; + +const ghostty_vt = @import("ghostty-vt"); + +const EditBuffer = panes.Text.EditBuffer; +const Snapshot = panes.Text.Snapshot; +const history_max = panes.Text.history_max; + +pub const VtSlot = ghostty_vt.Terminal; +/// A cell of an emulator's pages, as `getCell` reads it (src/term/echo.zig). +pub const PageCell = ghostty_vt.PageList.Cell; +/// Kitty graphics: a program's images are placements, as a kind's are. +pub const images_enabled = true; +pub const TerminalImages = if (images_enabled) @import("TerminalImages.zig") else void; +pub const StreamSlot = ghostty_vt.Stream(MarkedHandler); + +/// The `aid` pardes's injected bash and fish marks carry (host_io.zig). +pub const prompt_aid = "pardes"; + +/// ghostty's handler, plus where the shell is in its command cycle as +/// its OSC 133 marks tell it: ghostty parses D's exit status and drops +/// it, and /pane//pty/run waits on exactly that. Only marks carrying +/// pardes's own `aid` move it, so fish's native marks, a nested shell's +/// and a stray `133;D` in some file being printed all pass it by. +pub const MarkedHandler = struct { + inner: ghostty_vt.TerminalStream.Handler, + phase: enum { none, prompt, input, running } = .none, + /// Counts commands started (C at the input phase). + started: u32 = 0, + /// Counts prompts drawn (B), so a line the shell refused without + /// running (a fish syntax error: no C, no D, a new prompt) shows. + prompts: u32 = 0, + /// D marks outside a running command: the shell handled a line + /// without starting it (bash on a syntax error prints no C). + declined: u32 = 0, + /// The newest prompt continues an unfinished line (bash's PS2, marked + /// `k=c`): the shell still holds the start of the command. + continuation: bool = false, + /// Where the newest command's output began (its C) and ended (its + /// D) on the primary screen, pinned so scrolling keeps them. + out_start: ?*ghostty_vt.Pin = null, + out_end: ?*ghostty_vt.Pin = null, + /// The running command's output was erased under it -- a clear, ED 3, + /// a reset -- so the pins no longer span what it printed. ED 3 flags a + /// pin on an erased history row `garbage` (ghostty 1.4), but `pinTop` + /// re-pins the start at the screen's top, so the flag is gone with the + /// old pin: this remembers it, as it does for a reset or a pin that + /// could not be made. + lost: bool = false, + + /// The newest command to end: which one, and its status if given. + finished: ?struct { seq: u32, status: ?i32 } = null, + + const Action = ghostty_vt.TerminalStream.Action; + + /// Move one of the output pins to the cursor. Only the primary screen + /// is pinned: a command's own full-screen display is not its output. + fn pinCursor(self: *@This(), slot: *?*ghostty_vt.Pin, now: bool) void { + const screen = self.inner.terminal.screens.get(.primary) orelse return; + if (slot.*) |old| screen.pages.untrackPin(old); + slot.* = null; + if (!now or self.inner.terminal.screens.active_key != .primary) return; + slot.* = screen.pages.trackPin(screen.cursor.page_pin.*) catch null; + } + + pub fn vt(self: *@This(), comptime action: Action.Tag, value: Action.Value(action)) void { + if (comptime action == .semantic_prompt) mark: { + const aid = value.readOption(.aid) orelse break :mark; + if (!std.mem.eql(u8, aid, prompt_aid)) break :mark; + switch (value.action) { + .fresh_line_new_prompt => { + self.phase = .prompt; + self.continuation = value.readOption(.prompt_kind) == .continuation; + }, + .end_prompt_start_input => { + self.phase = .input; + self.prompts +%= 1; + }, + // bash's DEBUG trap also fires on its own PROMPT_COMMAND, + // outside the input phase; only a C that ends input counts + .end_input_start_output => if (self.phase == .input) { + self.phase = .running; + self.started +%= 1; + self.lost = false; + self.pinCursor(&self.out_start, true); + self.pinCursor(&self.out_end, false); + }, + .end_command => if (self.phase == .running) { + self.phase = .none; + self.finished = .{ .seq = self.started, .status = value.readOption(.exit_code) }; + self.pinCursor(&self.out_end, true); + } else { + self.declined +%= 1; + }, + else => {}, + } + } + if (comptime action == .erase_display_complete or action == .erase_display_scrollback or + action == .erase_display_scroll_complete or action == .full_reset) + { + self.inner.vt(action, value); + // A `clear` under a running command: its output is what it + // prints from here, the screen's top, not lost. Only what the + // scrollback later loses is (the pin goes `garbage`). + if (self.phase == .running) self.pinTop(&self.out_start); + return; + } + self.inner.vt(action, value); + } + + /// Pins `slot` to the primary screen's top-left; lost when it cannot be. + fn pinTop(self: *@This(), slot: *?*ghostty_vt.Pin) void { + const screen = self.inner.terminal.screens.get(.primary) orelse return; + if (slot.*) |old| screen.pages.untrackPin(old); + slot.* = null; + if (self.inner.terminal.screens.active_key != .primary) { + self.lost = true; + return; + } + const top = screen.pages.pin(.{ .active = .{} }) orelse { + self.lost = true; + return; + }; + slot.* = screen.pages.trackPin(top) catch null; + if (slot.* == null) self.lost = true; + } + + pub fn deinit(self: *@This()) void { + self.inner.deinit(); + } +}; + +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; + // 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); +} + +fn ptyDeviceAttrs(_: *ghostty_vt.TerminalStream.Handler) ghostty_vt.device_attributes.Attributes { + return .{}; +} + +/// CSI 14/16/18 t: the pane's size, and its cells' in pixels, which a +/// program sizes its kitty graphics by (yazi asks 16 t). Unknown, not asked. +fn ptySize(handler: *ghostty_vt.TerminalStream.Handler) ?ghostty_vt.size_report.Size { + const t = handler.terminal; + if (t.width_px == 0 or t.height_px == 0 or t.cols == 0 or t.rows == 0) return null; + return .{ .rows = t.rows, .columns = t.cols, .cell_width = t.width_px / t.cols, .cell_height = t.height_px / t.rows }; +} + +pub const State = struct { + /// What the emulator and this struct are allocated with: the core's, + /// but on a screen-state replica the C heap (`emulator_gpa`), outside + /// its rollback epoch: written by deltas while a copy is held, no part + /// of it is ever a copy's to save or put back. + gpa: std.mem.Allocator, + vt: VtSlot, + stream: StreamSlot, + /// 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: ?ghostty_vt.Coordinate = null, + /// The kitty graphics its program placed, as drawn (TerminalImages.zig). + images: TerminalImages = if (images_enabled) .{} else {}, + /// Counts the emulator's writes (output, a resize, its view scrolled): + /// a new count is a new state for its replicas (screen_state.Track). + changes: u32 = 0, + /// A step read its rows (its history too): a replica replaying that + /// step must hold them, so its authority sends the ones it does not + /// before the step's unit (replica.Screens.fill). + read: bool = false, + /// The size a step gave it, on an authority with screen-state replicas: + /// made after the step's unit (`resizeDeferred`), so that the rest of + /// the step reads the grid it had, as its replicas' replays do, and the + /// rows sent them for it (Screens.fill) are of that grid's numbering. + resize_to: ?struct { cols: u16, rows: u16 } = null, + + /// The emulator as its program has made it, for the replay digest + /// (src/walk.zig): the active screen's text, how much history is above + /// it and where the view is, the cursor, the answers not yet written. + /// Not its pages (their addresses are its own), nor the history's text + /// (the screen's was all of it once, and a session's digest is taken + /// every step), nor the kitty images, whose media is read off the host: + /// a replica's terminal is the authority's screen state from phase 5 + /// on (docs/next-daemon.md). + pub fn digest(s: *const State, h: *pardes.walk.Hasher) void { + // Under screen-state terminals the emulator is the authority's, and + // a replica's is what the last delta made it: compared as screens + // (test/replicas.zig), not step by step. + if (pardes.tape.screenState()) return; + s.digestScreen(h, true); + } + + /// `view`: where its view is too (a replica's speculation may move it). + pub fn digestScreen(s: *const State, h: *pardes.walk.Hasher, view: bool) void { + const screen = s.vt.screens.active; + const sb = screen.pages.scrollbar(); + const cur = screen.cursor; + const offset = if (view) sb.offset else 0; + const facts = .{ sb.total, offset, sb.len, cur.x, cur.y, s.vt.modes.get(.cursor_visible), s.vt.screens.active_key, s.reply_dropped }; + inline for (facts) |fact| h.update(std.mem.asBytes(&fact)); + h.update(s.reply[0..s.reply_len]); + var buf: [4096]u8 = undefined; + var text: std.Io.Writer.Hashing(pardes.walk.Hasher) = .initHasher(h.*, &buf); + if (screen.pages.getBottomRight(.active)) |br| + screen.dumpString(&text.writer, .{ .tl = screen.pages.getTopLeft(.active), .br = br, .unwrap = false }) catch unreachable; // a hashing writer never fails + text.writer.flush() catch unreachable; // a hashing writer never fails + h.* = text.hasher; + } + + /// What the dump keeps of a terminal beside its emulator, by name as + /// every dump is (src/walk.zig). + const Parts = struct { + reply: []const u8 = "", + reply_dropped: u32 = 0, + mouse_cell: ?ghostty_vt.Coordinate = null, + petscii: bool = false, + /// The view's top row, counted from the top of the history. + view: usize = 0, + phase: @FieldType(MarkedHandler, "phase") = .none, + started: u32 = 0, + prompts: u32 = 0, + declined: u32 = 0, + continuation: bool = false, + lost: bool = false, + finished: @FieldType(MarkedHandler, "finished") = null, + out_start: ?ghostty_vt.Coordinate = null, + out_end: ?ghostty_vt.Coordinate = null, + }; + + fn pinAt(s: *const State, slot: ?*ghostty_vt.Pin) ?ghostty_vt.Coordinate { + const pin = slot orelse return null; + const screen = s.vt.screens.get(.primary) orelse return null; + return (screen.pages.pointFromPin(.screen, pin.*) orelse return null).screen; + } + + /// The dump's: ghostty's snapshot of the emulator (both screens, their + /// history, styles, links and kitty images), then `Parts`. A sequence + /// its program had begun and not ended is not kept: the snapshot is at + /// the ground state. + pub fn dumpWrite(s: *const State, gpa: std.mem.Allocator, w: *std.Io.Writer) !void { + const h = &s.stream.handler; + const parts: Parts = .{ + .reply = s.reply[0..s.reply_len], + .reply_dropped = s.reply_dropped, + .mouse_cell = s.mouse_cell, + .petscii = if (comptime images_enabled) s.images.petscii else false, + .view = s.vt.screens.active.pages.scrollbar().offset, + .phase = h.phase, + .started = h.started, + .prompts = h.prompts, + .declined = h.declined, + .continuation = h.continuation, + .lost = h.lost, + .finished = h.finished, + .out_start = s.pinAt(h.out_start), + .out_end = s.pinAt(h.out_end), + }; + const bytes = try pardes.walk.save(Parts, gpa, &parts); + defer gpa.free(bytes); + try w.writeAll(std.mem.asBytes(&@as(u64, bytes.len))); + try w.writeAll(bytes); + // A join's (dump.zig `dumpJoin`): its size alone, an emulator made + // afresh there, which its first delta replaces. + if (pardes.dump.slim_terminals) return w.writeAll(std.mem.asBytes(&[2]u16{ s.vt.cols, s.vt.rows })); + try ghostty_vt.snapshot.encode(gpa, w, &s.vt, .{ .continuation = .ground }); + } + + /// What `dumpRead` made, freed: a load that failed after it. + pub fn dumpFree(s: *State, gpa: std.mem.Allocator) void { + if (comptime images_enabled) s.images.deinit(gpa); + s.stream.deinit(); + s.vt.deinit(gpa); + } + + /// `dumpWrite`'s back into `out`, a new State, as `create` makes one. + pub fn dumpRead(out: *State, gpa: std.mem.Allocator, bytes: []const u8) !void { + if (bytes.len < 8) return error.EndOfDump; + const n = std.math.cast(usize, std.mem.bytesToValue(u64, bytes[0..8])) orelse return error.EndOfDump; + if (bytes.len - 8 < n) return error.EndOfDump; + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const parts = try pardes.walk.load(Parts, arena.allocator(), bytes[8..][0..n], null); + var reader: std.Io.Reader = .fixed(bytes[8 + n ..]); + if (reader.buffer.len == 4) { + const size = std.mem.bytesToValue([2]u16, bytes[8 + n ..][0..4]); + out.* = .{ .gpa = gpa, .vt = try emulator(gpa, @max(1, size[0]), @max(1, size[1])), .stream = undefined }; + } else { + var decoded = try ghostty_vt.snapshot.decodeExact(gpa, terminalIo(), &reader, .{ + .max_continuation_bytes = 0, + .kitty_image_storage_limit = if (images_enabled) TerminalImages.storage_limit else 0, + }); + defer decoded.deinit(gpa); + out.* = .{ .gpa = gpa, .vt = decoded.toOwned(), .stream = undefined }; + } + if (comptime images_enabled) inline for (.{ .primary, .alternate }) |key| { + if (out.vt.screens.get(key)) |screen| screen.kitty_images.image_limits = .allWithTempDir(tempDir()); + }; + out.wire(); + const reply = parts.reply[0..@min(parts.reply.len, out.reply.len)]; + @memcpy(out.reply[0..reply.len], reply); + out.reply_len = @intCast(reply.len); + out.reply_dropped = parts.reply_dropped; + out.mouse_cell = parts.mouse_cell; + out.images = if (images_enabled) .{ .petscii = parts.petscii } else {}; + const h = &out.stream.handler; + h.phase = parts.phase; + h.started = parts.started; + h.prompts = parts.prompts; + h.declined = parts.declined; + h.continuation = parts.continuation; + h.lost = parts.lost; + h.finished = parts.finished; + if (out.vt.screens.get(.primary)) |screen| { + inline for (.{ .{ "out_start", parts.out_start }, .{ "out_end", parts.out_end } }) |pair| if (pair[1]) |at| { + if (screen.pages.pin(.{ .screen = at })) |pin| @field(h, pair[0]) = screen.pages.trackPin(pin) catch null; + }; + } + const screen = out.vt.screens.active; + const bottom = screen.pages.scrollbar().offset; + screen.scroll(.{ .delta_row = @as(isize, @intCast(parts.view)) - @as(isize, @intCast(bottom)) }); + } + + /// The stream over the emulator, its answers to the program and its + /// size reports routed here, PNG decoded: what `create` and a dump's + /// load both do once the emulator is in place. + fn wire(s: *State) void { + s.stream = .init(.{ .handler = .{ .inner = s.vt.vtHandler() }, .allocator = s.gpa }); + s.stream.handler.inner.effects.write_pty = ptyReport; + s.stream.handler.inner.effects.device_attributes = ptyDeviceAttrs; + s.stream.handler.inner.effects.size = ptySize; + if (comptime images_enabled) TerminalImages.installPngDecoder(); + } +}; + +const GColor = ghostty_vt.color; + +const FilterPaletteKey = struct { + bg: GColor.RGB, + fg: GColor.RGB, + base: [16]GColor.RGB, +}; + +pub const FilterPalette = LivePalette; + +const LivePalette = struct { + key: ?FilterPaletteKey = null, + colors: GColor.Palette = GColor.default, + + fn get(self: *LivePalette, theme: *const pardes.Theme) *const GColor.Palette { + const bg = asGhostRgb(theme.bg orelse theme.tag_bg); + const fg = asGhostRgb(theme.fg orelse theme.tag_fg); + var base: [16]GColor.RGB = undefined; + if (theme.palette) |palette| { + for (&base, palette) |*dst, src| dst.* = asGhostRgb(src); + } else { + const synthesized = [16][3]u8{ + theme.bg orelse theme.tag_bg, + theme.kw, + theme.str, + theme.num, + theme.box, + theme.sel_bg, + theme.comment, + theme.fg orelse theme.tag_fg, + theme.lineno, + theme.kw, + theme.str, + theme.num, + theme.scroll_thumb, + theme.sel_fg, + theme.tag_fg, + theme.fg orelse theme.tag_fg, + }; + for (&base, synthesized) |*dst, src| dst.* = asGhostRgb(src); + } + + const key: FilterPaletteKey = .{ .bg = bg, .fg = fg, .base = base }; + if (self.key) |old| if (std.meta.eql(old, key)) return &self.colors; + + var seed = GColor.default; + for (base, 0..) |rgb, i| seed[i] = rgb; + self.colors = GColor.generate256Color(seed, .empty, bg, fg, false); + self.key = key; + return &self.colors; + } +}; + +fn asGhostRgb(rgb: [3]u8) GColor.RGB { + return .{ .r = rgb[0], .g = rgb[1], .b = rgb[2] }; +} + +pub const PendingCommand = struct { + bytes: []u8 = &.{}, + wait: enum { none, spawn, input } = .none, +}; + +pub fn armShellSpawn(pane: *Pane) void { + std.debug.assert(pane.pending_command.bytes.len == 0); + pane.pending_command.wait = .spawn; +} + +/// Remembers a line pardes types into this terminal as the command it +/// started, for Kill: it runs from the shell's next C mark to its D. +/// ponytail: of several lines queued for a shell not up yet, only the last +/// is remembered, as the first of them. +pub fn noteCommand(pane: *Pane, line: []const u8) void { + const state = pane.terminal orelse return; + const word = std.mem.trim(u8, line, " \t")[0 .. std.mem.indexOfAny(u8, std.mem.trim(u8, line, " \t"), " \t") orelse std.mem.trim(u8, line, " \t").len]; + var sent: @typeInfo(@FieldType(Pane, "sent_command")).optional.child = .{ .seq = state.stream.handler.started +% 1 }; + sent.len = @intCast(@min(word.len, sent.word.len)); + @memcpy(sent.word[0..sent.len], word[0..sent.len]); + pane.sent_command = sent; +} + +/// Whether the command pardes last typed here is running now. +pub fn commandRunning(pane: *const Pane) bool { + const state = pane.terminal orelse return false; + const sent = pane.sent_command orelse return false; + const marks = &state.stream.handler; + return marks.phase == .running and marks.started == sent.seq; +} + +pub fn queuePendingCommand(pane: *Pane, command: []const u8) !bool { + if (pane.pending_command.wait == .none) return false; + const old_len = pane.pending_command.bytes.len; + const new_len = try std.math.add(usize, old_len, try std.math.add(usize, command.len, 1)); + const bytes = if (old_len == 0) + try pane.gpa.alloc(u8, new_len) + else + try pane.gpa.realloc(pane.pending_command.bytes, new_len); + @memcpy(bytes[old_len..][0..command.len], command); + bytes[new_len - 1] = '\r'; + pane.pending_command.bytes = bytes; + pane.greet = false; + return true; +} + +pub fn shellSpawned(p: *Pardes, id: usize, prompt_marks: bool) void { + const pane = p.panes[id] orelse return; + if (!pane.isTerminal() or pane.pending_command.wait != .spawn) return; + if (prompt_marks) { + pane.pending_command.wait = .input; + releasePendingCommand(p, id, pane, false); + } else { + pane.greet = false; + releasePendingCommand(p, id, pane, true); + } +} + +pub fn releasePendingCommandIfReady(p: *Pardes, id: usize, pane: *Pane) void { + if (pane.pending_command.wait == .input) + releasePendingCommand(p, id, pane, pardes.tape.value(.prompt, promptInputReady, .{pane})); +} + +fn releasePendingCommand(p: *Pardes, id: usize, pane: *Pane, ready: bool) void { + if (!ready) return; + const bytes = pane.pending_command.bytes; + pane.pending_command = .{}; + if (bytes.len > 0) { + p.emitWrite(id, bytes); + pane.gpa.free(bytes); + } +} + +pub fn deinitPendingCommand(pane: *Pane) void { + if (pane.pending_command.bytes.len > 0) + pane.gpa.free(pane.pending_command.bytes); + 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 { + 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 std.Io.Threaded.global_single_threaded.io(); +} + +/// The three numbers ghostty's scrollbar reports; all zero without an emulator. +pub const Scrollbar = struct { total: usize = 0, offset: usize = 0, len: usize = 0 }; + +pub fn scrollbar(pane: *const Pane) Scrollbar { + const state = pane.terminal orelse return .{}; + const sb = state.vt.screens.active.pages.scrollbar(); + return .{ .total = sb.total, .offset = sb.offset, .len = sb.len }; +} + +/// The emulator's viewport offset, in SHELL rows: the top of what it shows. +pub fn gridOffset(pane: *const Pane) i32 { + return @intCast(scrollbar(pane).offset); +} + +/// Where the emulator itself puts the cursor, in active-area cells — the origin +/// without one, which is where an empty pane's cursor belongs anyway. +pub const GridCursor = struct { x: u16 = 0, y: u16 = 0 }; + +pub fn gridCursor(pane: *const Pane) GridCursor { + const state = pane.terminal orelse return .{}; + const cur = state.vt.screens.active.cursor; + return .{ .x = @intCast(cur.x), .y = @intCast(cur.y) }; +} + +pub fn visibleCursor(pane: *const Pane) ?GridCursor { + const state = pane.terminal orelse return null; + if (!state.vt.modes.get(.cursor_visible)) return null; + const cur = state.vt.screens.active.cursor; + const sb = scrollbar(pane); + const row = sb.total - sb.len + cur.y; + if (row < sb.offset + pane.pan or row - sb.offset >= sb.len) return null; + return .{ .x = @intCast(cur.x), .y = @intCast(row - sb.offset - pane.pan) }; +} + +/// The rows terminal `pane`'s top is passed over by in a view whose rect +/// `r` is shorter than it, one that does not size it (Pardes.sizes): as +/// many as keep its cursor's row in sight, as tmux pans a bigger window in +/// a smaller client. 0 in the view that sizes it, and in one tall enough. +pub fn panFor(p: *const Pardes, pane: *const Pane, r: pardes.Rect) u16 { + if (pane.sized_by == p.live or pane.collapsed or !pane.isTerminal()) return 0; + const shown = r.h -| pane.tag_rows; + if (shown == 0 or shown >= pane.rows) return 0; + const state = pane.terminal orelse return 0; + const sb = scrollbar(pane); + // The cursor's row in the viewport; past its end (scrolled back), its + // last row. + const row = sb.total - sb.len + state.vt.screens.active.cursor.y; + const at: usize = if (row < sb.offset) 0 else @min(row - sb.offset, pane.rows - 1); + return @intCast(@min(pane.rows - shown, (at + 1) -| shown)); +} + +/// What pardes made of a terminal's stream that its emulator does not hold +/// (the OSC 133 cycle, MarkedHandler), for a replica's delta: phase u8, +/// started, prompts, declined u32, continuation and lost u8, a finished +/// command's seq u32 and status i32 (all ones: none; i32 min: no status). +// ponytail: the output pins stay the authority's (its 9P pty/run reads them). +pub const marks_len = 1 + 4 * 3 + 2 + 4 + 4; + +pub fn marksOf(state: *const State, buf: *[marks_len]u8) []const u8 { + const m = &state.stream.handler; + var w: std.Io.Writer = .fixed(buf); + w.writeByte(@backingInt(m.phase)) catch unreachable; + inline for (.{ m.started, m.prompts, m.declined }) |n| w.writeInt(u32, n, .little) catch unreachable; + w.writeAll(&.{ @intFromBool(m.continuation), @intFromBool(m.lost) }) catch unreachable; + w.writeInt(u32, if (m.finished) |f| f.seq else std.math.maxInt(u32), .little) catch unreachable; + w.writeInt(i32, if (m.finished) |f| f.status orelse std.math.minInt(i32) else 0, .little) catch unreachable; + return w.buffered(); +} + +/// A replica's terminal takes its authority's marks (`marksOf`). +pub fn takeMarks(state: *State, bytes: []const u8) void { + if (bytes.len != marks_len) return; + const m = &state.stream.handler; + m.phase = @fromBackingInt(@intCast(bytes[0])); + m.started = std.mem.readInt(u32, bytes[1..5], .little); + m.prompts = std.mem.readInt(u32, bytes[5..9], .little); + m.declined = std.mem.readInt(u32, bytes[9..13], .little); + m.continuation = bytes[13] != 0; + m.lost = bytes[14] != 0; + const seq = std.mem.readInt(u32, bytes[15..19], .little); + const status = std.mem.readInt(i32, bytes[19..23], .little); + m.finished = if (seq == std.math.maxInt(u32)) null else .{ .seq = seq, .status = if (status == std.math.minInt(i32)) null else status }; +} + +/// A screen-state replica's emulators' allocator (`State.gpa`). +pub const emulator_gpa = std.heap.c_allocator; + +/// What a terminal's emulator is made with, by `create` or a pane's load +/// (dump.loadPane, a closed terminal brought back): `gpa`, but on a screen- +/// state replica `emulator_gpa`. +pub fn emulatorGpa(gpa: std.mem.Allocator) std.mem.Allocator { + return if (pardes.tape.screenReplica()) emulator_gpa else gpa; +} + +/// A replica's speculation leaves an emulator be: it is the authority's +/// (docs/next-daemon.md, phase 5), so the input waits for its order. +fn authorityOnly(comptime why: []const u8) bool { + if (!pardes.tape.speculating()) return false; + pardes.tape.poison(why); + return true; +} + +/// A speculation that moves a terminal's view: a screen-state replica's +/// may (its rollback puts the view back, replica.zig `views`, and a delta +/// sets it), any other stops (`authorityOnly`). +fn viewOnly(comptime why: []const u8) bool { + if (!pardes.tape.speculating()) return false; + if (pardes.tape.screenState()) { + pardes.tape.touchTerminal(); + return false; + } + pardes.tape.poison(why); + return true; +} + +/// Where a terminal's view is: its top row counted from the top of its +/// history, or null following its output (a replica's rollback copy, +/// which holds no emulator, keeps it so). +pub fn viewOf(pane: *const Pane) ?usize { + const state = pane.terminal orelse return null; + const pages = &state.vt.screens.active.pages; + if (pages.viewport == .active) return null; + return pages.scrollbar().offset; +} + +pub fn viewTo(pane: *Pane, at: ?usize) void { + const state = pane.terminal orelse return; + state.vt.screens.active.scroll(if (at) |row| .{ .row = row } else .active); +} + +/// Move the emulator's viewport by `delta` shell rows (negative scrolls back). +pub fn scrollGrid(pane: *Pane, delta: i32) void { + if (viewOnly("scroll a terminal")) return; + const state = pane.terminal orelse return; + state.vt.screens.active.scroll(.{ .delta_row = delta }); + state.changes +%= 1; +} + +/// Is the viewport already showing the last row of output? +pub fn atBottom(pane: *const Pane) bool { + const sb = scrollbar(pane); + return sb.offset + sb.len >= sb.total; +} + +/// Snap the viewport back onto live output. +pub fn followOutput(pane: *Pane) void { + if (viewOnly("follow a terminal")) return; + const state = pane.terminal orelse return; + const moved = state.vt.screens.active.pages.viewport != .active; + state.vt.screens.active.scroll(.active); + state.changes +%= @intFromBool(moved); +} + +/// Reflow the grid. A failed reflow keeps the grid it had rather than dropping +/// a scrollback; the next resize retries with the same numbers. +pub fn resizeGrid(pane: *Pane, cols: u16, rows: u16) void { + if (authorityOnly("resize a terminal")) return; + const state = pane.terminal orelse return; + if (cols != pane.cols) for (0..pane.sel.len) |button| { + pane.clearPointerSelection(button); + pane.sel[button].state = .none; + }; + // A replica's emulator is sized by its authority: the next delta says. + if (pardes.tape.screenReplica()) return; + // ...and the authority's once the step's unit is out (a client typing + // at another size than the last typist's read history rows its replay + // held as holes, and the rows came renumbered, 2026-10-30). + if (pardes.tape.screenAuthority()) { + state.resize_to = .{ .cols = cols, .rows = rows }; + return; + } + state.changes +%= 1; + state.vt.resize(state.gpa, .{ .cols = cols, .rows = rows }) catch {}; +} + +/// The sizes steps gave terminals (`resizeGrid`), made: between units, on an +/// authority with screen-state replicas (replica.Peers.unit). +pub fn resizeDeferred(p: *Pardes) void { + for (p.panes) |slot| if (slot) |pane| if (pane.terminal) |state| if (state.resize_to) |size| { + state.resize_to = null; + state.changes +%= 1; + p.shell_rows.markStale(pane); + state.vt.resize(state.gpa, .{ .cols = size.cols, .rows = size.rows }) catch {}; + }; +} + +/// DECSET 2004: the program wants its pastes bracketed. +pub fn bracketedPaste(pane: *const Pane) bool { + const state = pane.terminal orelse return false; + return state.vt.modes.get(.bracketed_paste); +} + +/// The program tracks the mouse itself, so a click in its body is its event. +pub fn reportsMouse(pane: *const Pane) bool { + const state = pane.terminal orelse return false; + return state.vt.flags.mouse_event != .none; +} + +/// A paste as xterm makes one (ghostty's encoder): the control bytes a +/// paste could smuggle a command in by (ESC, so no ESC[201~ can end +/// 2004's brackets early, NUL, ^C and the rest of xterm's list) become +/// spaces, then the text goes inside the brackets when the program set +/// 2004, else with its newlines as Returns. `text` is rewritten in place. +pub fn encodePaste(pane: *const Pane, text: []u8) [3][]const u8 { + return ghostty_vt.input.encodePaste(text, .{ .bracketed = bracketedPaste(pane) }); +} + +/// DECSET 1004: the program wants to hear when it gains and loses focus. +pub fn reportsFocus(pane: *const Pane) bool { + const state = pane.terminal orelse return false; + return state.vt.modes.get(.focus_event); +} + +/// ...every move of it, a button down or not (1003). +pub fn tracksAllMotion(pane: *const Pane) bool { + const state = pane.terminal orelse return false; + return state.vt.flags.mouse_event == .any; +} + +/// A wheel over the program is the program's: it tracks the mouse, or it is +/// on the alternate screen, which has no scrollback, with alternate scroll +/// (DECSET 1007, on unless turned off) asking for arrow keys instead. +pub fn ownsWheel(pane: *const Pane) bool { + const state = pane.terminal orelse return false; + return reportsMouse(pane) or + (state.vt.screens.active_key == .alternate and state.vt.modes.get(.mouse_alternate_scroll)); +} + +pub const MouseMods = struct { shift: bool = false, alt: bool = false, ctrl: bool = false }; +pub const MouseButton = enum { left, middle, right, wheel_up, wheel_down, wheel_left, wheel_right }; +pub const MouseAction = enum { press, release, motion }; + +/// One mouse event for the program, as xterm reports it (ctlseqs; ghostty's +/// encoder): the tracking mode it set (1000 clicks, 1002 drags, 1003 all +/// motion) decides whether it hears of it, the format it set (1006 SGR, +/// 1015 urxvt, 1005 UTF-8, else X10) how, modifiers as shift+4, meta+8, +/// ctrl+16. `col`,`row` are the 0-based grid cell; a drag that left the +/// grid is reported clamped to its edge, motion only when the cell +/// changed. Empty when the program does not hear of it. +pub fn mouseBytes(pane: *Pane, buf: *[64]u8, action: MouseAction, button: ?MouseButton, mods: MouseMods, col: i32, row: i32, any_pressed: bool) []const u8 { + const state = pane.terminal orelse return &.{}; + const input = ghostty_vt.input; + var writer = std.Io.Writer.fixed(buf); + var opts: input.MouseEncodeOptions = .{ + .event = state.vt.flags.mouse_event, + .format = state.vt.flags.mouse_format, + .size = .{ + .screen = .{ .width = state.vt.cols, .height = state.vt.rows }, + .cell = .{ .width = 1, .height = 1 }, + .padding = .{}, + }, + .any_button_pressed = any_pressed, + .last_cell = &state.mouse_cell, + }; + // A release is always reported; the rest only over the grid, or held. + if (action == .release) opts.any_button_pressed = true; + input.encodeMouse(&writer, .{ + .action = switch (action) { + .press => .press, + .release => .release, + .motion => .motion, + }, + .button = if (button) |b| switch (b) { + .left => .left, + .middle => .middle, + .right => .right, + .wheel_up => .four, + .wheel_down => .five, + .wheel_left => .six, + .wheel_right => .seven, + } else null, + .mods = .{ .shift = mods.shift, .alt = mods.alt, .ctrl = mods.ctrl }, + .pos = .{ .x = @as(f32, @floatFromInt(col)) + 0.5, .y = @as(f32, @floatFromInt(row)) + 0.5 }, + }, opts) catch return &.{}; // no report rather than half of one + return writer.buffered(); +} + +/// The bytes a wheel turn at grid cell `col`,`row` (0-based) sends the +/// program: its mouse report while it tracks the mouse, else, on the +/// alternate screen, an arrow key in the cursor keys' mode (none for a +/// sideways turn). Null when the wheel is not the program's (`ownsWheel`). +pub fn wheelBytes(pane: *Pane, buf: *[64]u8, wheel: MouseButton, col: u16, row: u16, mods: MouseMods) ?[]const u8 { + const state = pane.terminal orelse return null; + if (!ownsWheel(pane)) return null; + const m = &state.vt.modes; + if (!reportsMouse(pane)) return switch (wheel) { + .wheel_up => if (m.get(.cursor_keys)) "\x1bOA" else "\x1b[A", + .wheel_down => if (m.get(.cursor_keys)) "\x1bOB" else "\x1b[B", + else => null, + }; + return mouseBytes(pane, buf, .press, wheel, mods, col, row, false); +} + +/// The whole scrollback as plain text, `gpa`-owned: what a terminal's body +/// reads and `Save` writes out. Logical lines: rows the terminal wrapped +/// are joined again (ghostty's wrap flags, as pty/run's output is), and +/// the last line ends with a newline. +pub fn screenTextAlloc(pane: *Pane, gpa: std.mem.Allocator) ![]const u8 { + var out: std.Io.Writer.Allocating = .init(gpa); + errdefer out.deinit(); + try writeScreenText(pane, &out.writer); + return out.toOwnedSlice(); +} + +fn writeScreenText(pane: *Pane, w: *std.Io.Writer) !void { + const state = pane.terminal orelse return; + const screen = state.vt.screens.active; + const br = screen.pages.getBottomRight(.screen) orelse return; + var both: TeeWriter = .{ .a = w }; + try screen.dumpString(&both.writer, .{ .tl = screen.pages.getTopLeft(.screen), .br = br, .unwrap = true }); + if (both.count > 0 and both.final != '\n') try w.writeByte('\n'); +} + +/// Passes bytes through, remembering how many and the last one. +const TeeWriter = struct { + a: *std.Io.Writer, + count: usize = 0, + final: u8 = 0, + writer: std.Io.Writer = .{ .vtable = &.{ .drain = drain }, .buffer = &.{} }, + + fn drain(w: *std.Io.Writer, data: []const []const u8, splat: usize) std.Io.Writer.Error!usize { + const t: *TeeWriter = @alignCast(@fieldParentPtr("writer", w)); + var n: usize = 0; + for (data[0 .. data.len - 1]) |bytes| { + try t.a.writeAll(bytes); + t.note(bytes); + n += bytes.len; + } + const pattern = data[data.len - 1]; + for (0..splat) |_| { + try t.a.writeAll(pattern); + t.note(pattern); + n += pattern.len; + } + return n; + } + + fn note(t: *TeeWriter, bytes: []const u8) void { + if (bytes.len == 0) return; + t.count += bytes.len; + t.final = bytes[bytes.len - 1]; + } +}; + +/// How long `screenTextAlloc` would be, counted without keeping it: the +/// length a terminal's body reads as, which its ctl line reports. +pub fn screenTextLen(pane: *Pane) usize { + var buf: [256]u8 = undefined; + var counting: std.Io.Writer.Discarding = .init(&buf); + writeScreenText(pane, &counting.writer) catch unreachable; // a discarding writer never fails + return @intCast(counting.fullCount()); +} + +/// Release the emulator's heap. The Pane allocation itself is the core's. +pub fn deinitEmulator(pane: *Pane, gpa: std.mem.Allocator) void { + // A pane with none (a file's, a picker's, a preview's) closes in a + // speculation as anywhere. + const state = pane.terminal orelse return; + if (authorityOnly("close a terminal")) return; + if (comptime images_enabled) state.images.deinit(gpa); + state.stream.deinit(); + const own = state.gpa; + state.vt.deinit(own); + own.destroy(state); + pane.terminal = null; +} + +/// A terminal pane's emulator, `cols` by `rows`, as `create` makes one. +fn emulator(gpa: std.mem.Allocator, cols: u16, rows: u16) !ghostty_vt.Terminal { + return ghostty_vt.Terminal.init(terminalIo(), gpa, .{ + .cols = cols, + .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 if (@FieldType(ghostty_vt.Terminal.Options, "kitty_image_loading_limits") == void) {} else .direct, + }); +} + +/// Allocate the live emulator half of a terminal pane. Slot ownership, serial +/// assignment, and spawn effects remain core lifecycle invariants. +pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { + if (authorityOnly("open a terminal")) return error.OutOfMemory; + const pane = try createDoc(gpa, cols, rows); + errdefer gpa.destroy(pane); + const own = emulatorGpa(gpa); + const state = try own.create(State); + errdefer own.destroy(state); + // Every other field its default, never what the heap had: a digest + // reads them, and two replicas must agree (src/replica.zig). + state.* = .{ .gpa = own, .vt = try emulator(own, cols, rows), .stream = undefined }; + state.wire(); + pane.terminal = state; + pane.tty_filter = true; + return pane; +} + +pub fn createDoc(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { + const pane = try gpa.create(Pane); + pane.* = .{ + .gpa = gpa, + .cols = cols, + .rows = rows, + }; + return pane; +} + +/// Where a restored terminal's history ends and its new shell begins. +pub const restored_banner = "\x1b[0m\r\n\x1b[2m\u{2500}\u{2500} restored history \u{2500}\u{2500}\x1b[0m\r\n"; + +/// A terminal loaded from a dump, its shell not there, made ready for a new +/// one: its program's screen modes reset (the old program is gone), its +/// history marked off from what the new shell prints, and the pane waiting +/// for it like any new terminal (a line queued for the old one goes to it). +pub fn reviveShell(pane: *Pane) void { + if (pane.terminal == null) return; + // The primary screen, keys, cursor and mouse as a shell starts with + // them, the cursor where it was (a soft reset, or leaving a primary + // screen for itself, would take it home). + if (pane.terminal.?.vt.screens.active_key == .alternate) ingest(pane, "\x1b[?1049l"); + ingest(pane, "\x1b[?1l\x1b>\x1b[?25h\x1b[?7h\x1b[?1000l\x1b[?1002l\x1b[?1003l\x1b[?1006l\x1b[?2004l\x1b[ 0 or pane.body.last_edit != null or pane.look_at != null or + pane.body.ed_undo_len > 0 or pane.body.ed_redo_len > 0 or pointerRowCount(pane) > 0; + // A replica's emulator is what the authority's deltas make it; what + // pardes keeps over it moves as the authority's ingest told it. + if (pardes.tape.screenReplica()) { + if (has_positions and ingestWithPositions(pane, bytes)) if (panes.paneIdOf(p, pane)) |id| p.setMessage(id, positions_lost); + return; + } + // Sticky bottom, as every other terminal does it: output drags the + // viewport down only when the viewport was already on the last row, so + // a reader scrolled back into the scrollback stays where they scrolled. + const at_bottom = atBottom(pane); + syncPixels(p, pane); + if (!has_positions) { + ingest(pane, bytes); + } else if (ingestWithPositions(pane, bytes)) if (panes.paneIdOf(p, pane)) |id| p.setMessage(id, positions_lost); + p.shell_rows.markStale(pane); + if (pane.body.mode == .tty and at_bottom) followOutput(pane); +} + +const RowPin = struct { pin: ?*ghostty_vt.Pin = null, offset: i32 = 0 }; +const PositionPin = struct { + target: *i32, + row: RowPin, + overlay: ?*EditBuffer, + edit_row: ?i32 = null, +}; + +fn trackRow(maybe_pages: ?*ghostty_vt.PageList, row: i32, failed: *bool) RowPin { + // A screen-state replica's: told by the tape, not its emulator. + const pages = maybe_pages orelse return .{ .offset = row }; + if (row < 0) return .{ .offset = row }; + const bounded: usize = @min(@as(usize, @intCast(row)), pages.total_rows - 1); + var left = pages.total_rows - 1 - bounded; + var node = pages.pages.last.?; + while (left >= node.rows()) { + left -= node.rows(); + node = node.prev.?; + } + const pin = pages.trackPin(.{ .node = node, .y = @intCast(node.rows() - 1 - left) }) catch { + failed.* = true; + return .{}; + }; + return .{ .pin = pin, .offset = row - @as(i32, @intCast(bounded)) }; +} + +fn pinnedRow(pages: ?*ghostty_vt.PageList, row: RowPin) i32 { + const pin = row.pin orelse return row.offset; + const alive = pages orelse return 0; + if (pin.garbage) return 0; + var after: usize = 0; + var node = alive.pages.last; + while (node) |item| : (node = item.prev) { + if (item == pin.node) + return @as(i32, @intCast(alive.total_rows - after - item.rows() + pin.y)) +| row.offset; + after += item.rows(); + } + return 0; +} + +fn trackPosition(pages: ?*ghostty_vt.PageList, target: *i32, overlay: ?*EditBuffer, failed: *bool) PositionPin { + var grid = target.*; + if (overlay) |edit| { + const lines: i32 = @intCast(modal.lineCount(edit.text)); + if (grid >= edit.row and grid < edit.row +| lines) + return .{ .target = target, .row = .{}, .overlay = edit, .edit_row = grid - edit.row }; + if (grid > edit.row) grid = grid -| lines +| edit.rows; + } + return .{ .target = target, .row = trackRow(pages, grid, failed), .overlay = overlay }; +} + +fn pointerRowCount(pane: *const Pane) usize { + var count: usize = 0; + for (pane.sel) |gesture| if (pane.pointerSelection(gesture)) |rows| { + count += rows.len; + }; + return count; +} + +/// What an ingest told the positions over it (`ingestWithPositions`): each +/// tracked row where it is after it (the spans' starts and ends, the +/// positions, the pointer rows, in that order) and whether the program +/// erased it, whether the screen was cleared, whether its pages lived and +/// whether a pin could not be made. Taped: a screen-state replica, which +/// ingests nothing, is told the authority's. +pub const Told = struct { rows: []const i32, erased: []const bool, cleared: bool, alive: bool, failed: bool }; + +fn told(t: Told) Told { + return t; +} + +/// `ingest`, with every position kept over the rows (the edit copy, the +/// cursors and selections, their undo, the pointer's rows) moved as the +/// rows move. True when one could not be kept (no memory for its pin). +fn ingestWithPositions(pane: *Pane, bytes: []const u8) bool { + const screens = &pane.terminal.?.vt.screens; + const key = screens.active_key; + const generation = screens.generation(key); + const replica = pardes.tape.screenReplica(); + const pages: ?*ghostty_vt.PageList = if (replica) null else &screens.active.pages; + const Group = struct { overlay: ?*EditBuffer, cursor: *i32, selection: *panes.Text.CharSel }; + var groups: [history_max * 2 + 1]Group = undefined; + groups[0] = .{ + .overlay = if (pane.ovl) |*overlay| overlay else null, + .cursor = &pane.body.cur_row, + .selection = &pane.body.vsel, + }; + var ngroups: usize = 1; + for ([_][]Snapshot{ pane.body.ed_undo[0..pane.body.ed_undo_len], pane.body.ed_redo[0..pane.body.ed_redo_len] }) |history| { + for (history) |*snapshot| { + groups[ngroups] = .{ + .overlay = if (snapshot.ovl) |*overlay| overlay else null, + .cursor = &snapshot.cur_row, + .selection = &snapshot.vsel, + }; + ngroups += 1; + } + } + const Span = struct { overlay: *EditBuffer, start: RowPin, end: RowPin }; + var spans: [groups.len]Span = undefined; + var nspans: usize = 0; + var positions: [groups.len * 2 + panes.Text.max_selections * 2 + 4]PositionPin = undefined; + var npositions: usize = 0; + var failed = false; + for (groups[0..ngroups]) |group| { + if (group.overlay) |overlay| { + spans[nspans] = .{ + .overlay = overlay, + .start = trackRow(pages, overlay.row, &failed), + .end = trackRow(pages, overlay.row +| (overlay.rows - 1), &failed), + }; + nspans += 1; + } + positions[npositions] = trackPosition(pages, group.cursor, group.overlay, &failed); + npositions += 1; + if (group.selection.active) { + positions[npositions] = trackPosition(pages, &group.selection.row, group.overlay, &failed); + npositions += 1; + } + } + const overlay = groups[0].overlay; + // Where the live pane's own selections sit in `positions`: one whose + // row the program erased goes (below), as the edited copy does. + const vsel_at: ?usize = if (pane.body.vsel.active) 1 else null; + const sels_at = npositions; + var extra: [panes.Text.max_selections * 2 + 2]*i32 = undefined; + var nextra: usize = 0; + for (pane.body.sels[0..pane.body.nsel]) |*selection| { + extra[nextra] = &selection.row; + extra[nextra + 1] = &selection.arow; + nextra += 2; + } + if (pane.body.last_edit) |*at| { + extra[nextra] = &at.row; + nextra += 1; + } + if (pane.look_at) |*at| { + extra[nextra] = &at.row; + nextra += 1; + } + for (extra[0..nextra]) |target| { + positions[npositions] = trackPosition(pages, target, overlay, &failed); + npositions += 1; + } + const pointer_positions = pane.gpa.alloc(PositionPin, pointerRowCount(pane)) catch null; + defer if (pointer_positions) |pins| pane.gpa.free(pins); + if (pointer_positions) |pins| { + var index: usize = 0; + for (pane.sel) |gesture| if (pane.pointerSelection(gesture)) |rows| { + for (rows) |*row| { + pins[index] = trackPosition(pages, &row.row, if (row.raw_terminal) null else overlay, &failed); + index += 1; + } + }; + } else failed = true; + var cleared = false; + var alive: ?*ghostty_vt.PageList = null; + if (!replica) { + // Whether the program cleared the screen (ED 2, a switch of + // screens); pardes reads the flag nowhere else. + pane.terminal.?.vt.flags.dirty.clear = false; + ingest(pane, bytes); + cleared = pane.terminal.?.vt.flags.dirty.clear; + // RIS can destroy the alternate screen and every pin it owned. + alive = if (screens.generation(key) == generation) pages else null; + } + const pointers: []PositionPin = pointer_positions orelse &.{}; + const n = 2 * nspans + npositions + pointers.len; + // On the stack, as the local path never allocated here but for the + // pointer's rows. + var rows_buf: [512]i32 = undefined; + var erased_buf: [512]bool = undefined; + const rows_after = if (n <= rows_buf.len) rows_buf[0..n] else pane.gpa.alloc(i32, n) catch return true; + defer if (n > rows_buf.len) pane.gpa.free(rows_after); + const erased_after = if (n <= erased_buf.len) erased_buf[0..n] else pane.gpa.alloc(bool, n) catch return true; + defer if (n > erased_buf.len) pane.gpa.free(erased_after); + @memset(rows_after, 0); + @memset(erased_after, false); + if (!replica) { + var k: usize = 0; + for (spans[0..nspans]) |span| for ([_]RowPin{ span.start, span.end }) |rp| { + rows_after[k] = pinnedRow(alive, rp); + erased_after[k] = alive != null and rp.pin != null and rp.pin.?.garbage; + k += 1; + }; + for ([_][]PositionPin{ positions[0..npositions], pointers }) |batch| for (batch) |position| { + rows_after[k] = pinnedRow(alive, position.row); + erased_after[k] = alive != null and position.row.pin != null and position.row.pin.?.garbage; + k += 1; + }; + } + const after = pardes.tape.value(.positions, told, .{Told{ .rows = rows_after, .erased = erased_after, .cleared = cleared, .alive = alive != null, .failed = failed }}); + if (after.rows.len != n or after.erased.len != n) return false; // a replica its authority's states apart (diverged) + failed = after.failed; + const at_position = 2 * nspans; + const at_pointer = at_position + npositions; + var moved = false; + var pointer_rows_lost = !after.alive or pointer_positions == null; + for (0..pointers.len) |i| if (after.erased[at_pointer + i]) { + pointer_rows_lost = true; + }; + // The program cleared the screen, or the rows the live copy stood on are + // gone (pruned, a reset): the copy goes with them, as what the program + // drew next. Kept, ghostty would move its rows to the top of what is + // left, over the program's new text. + var copy_lost = after.cleared or !after.alive; + for (spans[0..nspans], 0..) |span, i| { + if (span.overlay == overlay and after.erased[2 * i]) copy_lost = true; + const start = after.rows[2 * i]; + const rows = @max(1, after.rows[2 * i + 1] -| start +| 1); + moved = moved or start != span.overlay.row or rows != span.overlay.rows; + span.overlay.row = start; + span.overlay.rows = rows; + if (alive) |owner| { + if (span.start.pin) |pin| owner.untrackPin(pin); + if (span.end.pin) |pin| owner.untrackPin(pin); + } + } + // The rule above for the copy holds for the selections: one whose row + // is gone (ED 3 erased its history row, which ghostty flags `garbage`) + // goes too, rather than being clamped to the top. + const erased = after.erased[at_position..]; + const vsel_lost = if (vsel_at) |i| + erased[i] or (copy_lost and positions[i].edit_row != null) + else + false; + var sels_keep: [panes.Text.max_selections]bool = undefined; + for (0..pane.body.nsel) |i| sels_keep[i] = !(erased[sels_at + 2 * i] or erased[sels_at + 2 * i + 1]); + for ([_][]PositionPin{ positions[0..npositions], pointers }, [_]usize{ at_position, at_pointer }) |batch, first| for (batch, first..) |position, k| { + var row = after.rows[k]; + if (position.overlay) |edit| { + if (position.edit_row) |relative| { + row = edit.row +| relative; + } else if (row > edit.row) { + row = if (row < edit.row +| edit.rows) + edit.row + else + row -| edit.rows +| @as(i32, @intCast(modal.lineCount(edit.text))); + } + } + moved = moved or row != position.target.*; + position.target.* = row; + if (alive) |owner| if (position.row.pin) |pin| owner.untrackPin(pin); + }; + if (pointer_rows_lost) for (0..pane.sel.len) |button| { + pane.clearPointerSelection(button); + pane.sel[button].state = .none; + }; + if (vsel_lost) { + pane.body.vsel.active = false; + moved = true; + } + if (pane.body.nsel > 0) { + var kept: u16 = 0; + for (0..pane.body.nsel) |i| if (sels_keep[i]) { + pane.body.sels[kept] = pane.body.sels[i]; + kept += 1; + }; + if (kept != pane.body.nsel) { + moved = true; + if (kept == 0) pane.body.dropSels() else pane.body.nsel = kept; + } + } + if (copy_lost) if (pane.ovl) |copy| { + pane.gpa.free(copy.text); + pane.ovl = null; + moved = true; + }; + if (moved) pane.nsel_snap = 0; // Saved regex-preview offsets name the old flat text. + return failed; +} + +/// A full-screen program has the emulator on its alternate screen. +pub fn onAlternateScreen(pane: *const Pane) bool { + const state = pane.terminal orelse return false; + return state.vt.screens.active_key == .alternate; +} + +/// Whether a new terminal's greeting may be typed now: only at the shell's +/// first prompt (pardes's own marks: no command started; a prompt drawn +/// again for a resize is the same first prompt), with nothing +/// typed at it and no program on the alternate screen. Any later moment is `.never`, for good: something has run or +/// been typed first, and an `ls` then would go into it. A shell pardes +/// cannot mark has no first prompt to find, and is never greeted. +pub fn greeting(pane: *const Pane) enum { wait, now, never } { + if (pane.fs.unmarked) return .never; + // Text typed into the edit buffer that stands in for the prompt row. + if (pane.ovl) |o| if (o.text.len > 0) return .never; + const state = pane.terminal orelse return .wait; + const marks = &state.stream.handler; + // (Not `declined`: bash's first prompt comes after a D of its own.) + if (marks.started > 0) return .never; + // At a prompt's input (ghostty's semantic marks, pardes's or the + // shell's own), before any command pardes's marks saw run. + if (!promptInputReady(pane)) return .wait; + return if (promptInputEmpty(pane)) .now else .never; +} + +/// At a prompt's input: on the alternate screen too, where a program killed +/// there (htop, `sig KILL`) left the emulator and the shell then drew its +/// prompt, marks and all. A full-screen program draws no prompt marks. +pub fn promptInputReady(pane: *const Pane) bool { + const state = pane.terminal orelse return false; + return state.vt.screens.active.cursor.semantic_content == .input; +} + +pub const CommandOutput = struct { + /// Owned by the caller. + text: [:0]const u8, + /// Whether `text` starts where the command did. It does not when that + /// start is not there to read: cut off by `max_rows`, scrolled out of + /// the history or erased from it by ED 3 (ghostty flags the pin + /// `garbage`; an erased end leaves no text at all), erased under it + /// while it ran (`MarkedHandler.lost`), or never pinned (a C or D that + /// came on the alternate screen). + whole: bool, +}; + +/// What the newest finished command printed: the screen text between its +/// C and D marks, read from at most `max_rows` rows above its end, so a +/// command that printed the whole history costs no more than its tail. +/// The shell exited in the middle of a command (its `exit 3`): no end mark +/// will come, so the command's output ends where the cursor is. +pub fn endOutputHere(pane: *Pane) void { + const state = pane.terminal orelse return; + const marks = &state.stream.handler; + if (marks.phase == .running and marks.out_end == null) marks.pinCursor(&marks.out_end, true); +} + +pub fn commandOutput(pane: *Pane, gpa: std.mem.Allocator, max_rows: usize) !CommandOutput { + // Unless there is text to read, whether the nothing it printed is the + // whole of it. + const whole = printed: { + const state = pane.terminal orelse break :printed true; + const marks = &state.stream.handler; + const start = marks.out_start orelse break :printed false; + const end = marks.out_end orelse break :printed false; + const screen = state.vt.screens.get(.primary) orelse break :printed false; + if (end.garbage) break :printed false; + var whole = !marks.lost and !start.garbage; + // Nothing printed: the whole of it. + if (start.eql(end.*)) break :printed whole; + // D is where the next prompt begins: the output ends a cell before. + // An end at the very top, or before the start, is output a redraw + // moved (a prompt drawn over it in a window two rows high): how + // much is not known, so it is not called whole. + const last = end.leftWrap(1) orelse break :printed false; + if (last.before(start.*)) break :printed false; + var from = start.*; + if (last.up(max_rows)) |floor| if (from.before(floor)) { + from = floor; + from.x = 0; + whole = false; + }; + return .{ + .text = try screen.selectionString(gpa, .{ .sel = ghostty_vt.Selection.init(from, last, false) }), + .whole = whole, + }; + }; + return .{ .text = try gpa.dupeSentinel(u8, "", 0), .whole = whole }; +} + +/// At a prompt with nothing typed on its line: no input left of the +/// cursor and none under it (text typed and then Ctrl-A'd starts at the +/// cursor). Nothing further right counts: that is where fish draws a +/// right prompt, and fish suggests nothing on an empty line. +pub fn promptInputEmpty(pane: *const Pane) bool { + if (!promptInputReady(pane)) return false; + const cursor = &pane.terminal.?.vt.screens.active.cursor; + const row = cursor.page_pin.cells(.all); + for (row[0..@min(cursor.x + 1, row.len)]) |cell| + if (cell.semantic_content == .input and !cell.isEmpty()) return false; + return true; +} + +/// Encode one key for the program that owns a raw terminal and queue its pty +/// write, respecting the application's keyboard protocol and terminal modes. +pub fn forwardKey(p: *Pardes, id: usize, key: Key) void { + const pane = p.panes[id] orelse return; + const state = pane.terminal orelse return; + const input = ghostty_vt.input; + const function_keys = comptime keys: { + var keys: [Key.f25 - Key.f1 + 1]input.Key = undefined; + for (&keys, 1..) |*k, n| k.* = @field(input.Key, std.fmt.comptimePrint("f{d}", .{n})); + break :keys keys; + }; + const physical: input.Key = switch (key.cp) { + Key.enter => .enter, + Key.backspace => .backspace, + Key.tab => .tab, + Key.escape => .escape, + Key.up => .arrow_up, + Key.down => .arrow_down, + Key.left => .arrow_left, + Key.right => .arrow_right, + Key.home => .home, + Key.end => .end, + Key.page_up => .page_up, + Key.page_down => .page_down, + Key.delete => .delete, + Key.insert => .insert, + Key.print_screen => .print_screen, + Key.scroll_lock => .scroll_lock, + Key.pause => .pause, + Key.menu => .context_menu, + Key.f1...Key.f25 => function_keys[key.cp - Key.f1], + else => if (key.cp < 128) input.Key.fromASCII(@intCast(key.cp)) orelse .unidentified else .unidentified, + }; + var utf8: [4]u8 = undefined; + const text_cp = if (key.shift and key.cp >= 'a' and key.cp <= 'z') key.cp - 'a' + 'A' else key.cp; + // A key with no text of its own types its code point only when that + // is a character: the kitty keyboard protocol numbers function and + // modifier keys in the private use area (F3 is U+E016), and a shell + // was typed those as junk. + const text = if (key.text.len > 0) key.text else if (key.cp >= 32 and key.cp < Key.up and key.cp != Key.backspace and !(key.cp >= 0xE000 and key.cp <= 0xF8FF)) + utf8[0 .. std.unicode.utf8Encode(text_cp, &utf8) catch return] + else + ""; + // A key's text has no bound of its own (an input method's commit, a + // paste as one key): past the stack's room the encoding grows on the + // heap, and emitWrite cuts it into writes. + var room: [256]u8 = undefined; + var first: std.heap.BufferFirstAllocator = .init(&room, p.gpa); + var out: std.Io.Writer.Allocating = .init(first.allocator()); + defer out.deinit(); + input.encodeKey(&out.writer, .{ + .key = physical, + .mods = .{ .ctrl = key.ctrl, .alt = key.alt, .shift = key.shift }, + .consumed_mods = .{ .shift = key.text.len > 0 and key.shift }, + .utf8 = text, + .unshifted_codepoint = if (key.cp >= 'A' and key.cp <= 'Z') key.cp - 'A' + 'a' else if (key.cp < Key.up) key.cp else 0, + }, options: { + var opts = input.KeyEncodeOptions.fromTerminal(&state.vt); + // fromTerminal cannot know this one and says so, defaulting to + // "option is not alt" — right for an app that lets macOS do the + // unicode translation and hands the encoder the composed text. + // Every pardes shell resolves the modifier itself and hands the + // core the BASE key with the alt bit and no text at all (see + // PardesView.keyDown, and app.mjs doing the same with the same + // two bits), so by the time this runs alt already means alt. + // Left at the default, a macOS build drops the ESC prefix on + // the floor and forwards Alt-n to the child as a bare `n`. + opts.macos_option_as_alt = .true; + break :options opts; + }) catch |err| return p.reportError(id, "key", err); + if (out.written().len > 0) p.emitWrite(id, out.written()); +} + +pub fn enterTty(p: *Pardes, id: usize) void { + const pane = p.panes[id] orelse return; + const selected = pane.rawPointerText(pane.sel[@backingInt(config.select_button)]) != null; + if (!selected and pane.terminal != null and pane.body.cur_pinned and pane.terminal.?.vt.cursorIsAtPrompt()) handoff: { + const screen = pane.terminal.?.vt.screens.active; + const goff: i32 = @intCast(screen.pages.scrollbar().offset); + const vp_row = pane.gridRow(pane.body.cur_row) - goff; + if (vp_row < 0) break :handoff; + + var grid_col: i32 = @max(0, pane.body.cur_col); + if (screen.pages.pin(.{ .viewport = .{ .x = 0, .y = @intCast(vp_row) } })) |row_pin| { + if (row_pin.rowAndCell().row.semantic_prompt != .none) switch (promptCut(row_pin)) { + .cut => |cols| grid_col += @intCast(cols), + .keep, .blank => {}, + }; + } + const click_pin = screen.pages.pin(.{ + .viewport = .{ .x = @intCast(grid_col), .y = @intCast(vp_row) }, + }) orelse break :handoff; + const cursor_pin = screen.cursor.page_pin.*; + var prompts = cursor_pin.promptIterator(.left_up, null); + const prompt_pin = prompts.next() orelse break :handoff; + if (click_pin.before(prompt_pin)) break :handoff; + const moves = screen.promptClickMove(click_pin); + for (0..moves.left) |_| p.emitWrite(id, "\x1b[D"); + for (0..moves.right) |_| p.emitWrite(id, "\x1b[C"); + } + + pane.body.mode = .tty; + followOutput(pane); // entering raw mode is a request for the live shell + pane.body.vsel.active = false; + pane.body.nsel = 0; + // The raw renderer follows the live cursor. A captured mouse selection + // keeps its modal endpoint for the next return to editor mode. + pane.body.cur_pinned = selected; + pane.body.select = false; + pane.body.restore_cursor = false; + pane.body.sticky_col = -1; + pane.body.normal.clear(); +} + +// Returned slices remain valid until the next cache rebuild or reset. +pub const RowsCache = struct { + /// whose grid this describes; null = the slot is free + pane: ?*const Pane = null, + /// the rows joined by '\n' — `flatSurface` hands this back verbatim + /// instead of rebuilding the join on every keystroke + text: []const u8 = &.{}, + /// slices INTO `text`, absolute grid rows from 0 + rows: [][]const u8 = &.{}, + /// `text` is a prefix of this: blanking a prompt row shortens the join, + /// and the slack is not worth a second allocation to reclaim + text_alloc: []u8 = &.{}, + stale: bool = false, + + pub fn reset(c: *RowsCache, gpa: std.mem.Allocator) void { + if (c.text_alloc.len > 0) gpa.free(c.text_alloc); + if (c.rows.len > 0) gpa.free(c.rows); + c.* = .{}; + } + + /// Free a stale entry. Called at the top of `update`, and nowhere else. + pub fn sweep(c: *RowsCache, gpa: std.mem.Allocator) void { + if (c.stale) c.reset(gpa); + } + + /// `pane`'s grid moved: the entry no longer describes it. + pub fn markStale(c: *RowsCache, pane: *const Pane) void { + if (c.pane == pane) c.stale = true; + } + + pub fn dropPane(c: *RowsCache, pane: *const Pane) void { + if (c.pane != pane) return; + c.pane = null; + c.stale = true; + } +}; + +const Rows = struct { + text_alloc: []u8, + text: []const u8, + rows: [][]const u8, +}; + +const empty_grid = [1][]const u8{""}; + +/// What LEAVING raw tty mode does to one prompt row, decided from its cells +/// alone: OSC 133 prompt cells are hidden in normal mode, what was typed at +/// them stays. +const PromptCut = union(enum) { + /// show the row exactly as ghostty dumped it + keep, + /// show nothing at all + blank, + /// drop this many leading COLUMNS — the prompt — and keep the rest, which + /// is what was typed at it + cut: usize, +}; + +fn promptCut(pin: ghostty_vt.Pin) PromptCut { + const cells = pin.cells(.all); + var cols: usize = 0; + while (cols < cells.len and cells[cols].semantic_content == .prompt) cols += 1; + // Flagged, but with no prompt cells at the FRONT: a right-side prompt, or + // a repaint that has moved on. Nothing here is the prompt, so hide nothing. + if (cols == 0) return .keep; + // ...and all prompt, nothing typed yet: the row is chrome end to end. + if (cols >= cells.len) return .blank; + return .{ .cut = cols }; +} + +pub fn promptPrefixBytes(pane: *Pane, row: i32, raw: []const u8) usize { + if (row < 0) return 0; + const state = pane.terminal orelse return 0; + const pin = state.vt.screens.active.pages.pin(.{ .screen = .{ .x = 0, .y = @intCast(row) } }) orelse return 0; + if (pin.rowAndCell().row.semantic_prompt == .none) return 0; + return switch (promptCut(pin)) { + .keep => 0, + .blank => raw.len, + .cut => |cols| @min(raw.len, File.rawAtDisplay(raw, cols)), + }; +} + +fn promptRow(pin: ghostty_vt.Pin, raw: []const u8) []const u8 { + const cols = switch (promptCut(pin)) { + .keep => return raw, + .blank => return "", + .cut => |n| n, + }; + const cells = pin.cells(.all); + var at: usize = 0; + var col: usize = 0; + while (col < cols and at < raw.len) { + const cell = &cells[col]; + at = @min(raw.len, at + dumpedBytes(pin, cell)); + // the tail cell of a wide glyph spells nothing of its own + col += if (cell.wide == .wide) @as(usize, 2) else 1; + } + return std.mem.trimEnd(u8, raw[at..], " \t"); +} + +fn dumpedBytes(pin: ghostty_vt.Pin, cell: *const ghostty_vt.Cell) usize { + switch (cell.wide) { + .spacer_head, .spacer_tail => return 0, + .narrow, .wide => {}, + } + var n: usize = switch (cell.content_tag) { + .codepoint, .codepoint_grapheme => std.unicode.utf8CodepointSequenceLength( + cell.codepoint(), + ) catch 1, + // A cell carrying only a colour still spells one blank in the dump. + else => 1, + }; + if (cell.content_tag == .codepoint_grapheme) { + if (pin.grapheme(cell)) |extra| for (extra) |cp| { + n += std.unicode.utf8CodepointSequenceLength(cp) catch 1; + }; + } + return n; +} + +pub fn shellRows(p: *Pardes, pane: *Pane) ![]const []const u8 { + if (pane.terminal == null) return &empty_grid; + pardes.tape.touchTerminal(); + const c = &p.shell_rows; + if (!c.stale and c.pane == pane) { + // A step's read whether the rows are kept or made: a client that + // came since holds them as holes, and is sent them (Screens.fill). + if (!drawing) pane.terminal.?.read = true; + return c.rows; + } + // Another pane's rows, or a dropped pane's still waiting for the step's + // sweep (dropPane keeps them, as they may still be borrowed): this + // step's rows are scratch, and nothing in the slot is written over. + if (c.pane != null or c.text_alloc.len > 0 or c.rows.len > 0) { + c.stale = true; + return (try buildRows(p.scratch.allocator(), p, pane)).rows; + } + const built = try buildRows(p.gpa, p, pane); + c.* = .{ + .pane = pane, + .text = built.text, + .rows = built.rows, + .text_alloc = built.text_alloc, + }; + return c.rows; +} + +/// The full modal motion surface: shell history with the live edit overlay +/// spliced into the rows it covers. +pub fn cursorLines(p: *Pardes, pane: *Pane) ![]const []const u8 { + const rows = try shellRows(p, pane); + if (pane.ovl == null) return rows; + var last = rows.len; + if (pane.ovl) |overlay| + last = @max(last, @as(usize, @intCast(@max(0, overlay.row + overlay.rows)))); + var count = last; + if (pane.ovl) |overlay| { + if (overlay.row >= 0 and @as(usize, @intCast(overlay.row)) < last) + count = count - @min( + @as(usize, @intCast(overlay.rows)), + last - @as(usize, @intCast(overlay.row)), + ) + @max(1, modal.lineCount(overlay.text)); + } + const lines = try p.scratch.allocator().alloc([]const u8, count); + var n: usize = 0; + var grid_row: usize = 0; + while (grid_row < last) : (grid_row += 1) { + if (pane.ovl) |overlay| if (overlay.row >= 0 and grid_row == @as(usize, @intCast(overlay.row))) { + var overlay_lines = std.mem.splitScalar(u8, overlay.text, '\n'); + while (overlay_lines.next()) |line| : (n += 1) lines[n] = line; + grid_row += @intCast(overlay.rows - 1); + continue; + }; + lines[n] = if (grid_row < rows.len) rows[grid_row] else ""; + n += 1; + } + return lines[0..n]; +} + +/// Flatten `cursorLines` without rebuilding the common cached/no-overlay +/// case. Scratch-owned when a join is required. +pub fn flatSurface(p: *Pardes, pane: *Pane, lines: []const []const u8) ![]const u8 { + const cache = &p.shell_rows; + if (!cache.stale and cache.pane == pane and + lines.ptr == cache.rows.ptr and lines.len == cache.rows.len) return cache.text; + var total: usize = if (lines.len > 0) lines.len - 1 else 0; + for (lines) |line| total += line.len; + const text = try p.scratch.allocator().alloc(u8, total); + var at: usize = 0; + for (lines, 0..) |line, i| { + if (i > 0) { + text[at] = '\n'; + at += 1; + } + @memcpy(text[at..][0..line.len], line); + at += line.len; + } + return text; +} + +/// Materialize or extend the terminal edit overlay with one exact allocation. +/// Row slices are scratch-owned/borrowed; only the joined text is installed. +pub fn editText(p: *Pardes, pane: *Pane, lo: i32, hi: i32, col: i32) error{ OutOfMemory, WriteFailed, UnknownPoint }!?EditText { + const want_lo = @max(0, @min(lo, hi)); + const want_hi = @max(want_lo, @max(lo, hi)); + const fresh = pane.ovl == null; + const old: EditBuffer = pane.ovl orelse .{ .row = want_lo, .rows = 1, .text = &.{} }; + const lines: i32 = if (fresh) 1 else @intCast(modal.lineCount(old.text)); + const up = old.row - want_lo; + const down = want_hi - (old.row + lines - 1); + const extending = fresh or up > 0 or down > 0; + var row0 = old.row; + var covered = old.rows; + var text = old.text; + var owned = false; + if (extending) { + const rows = try shellRows(p, pane); + row0 = old.row - @max(0, up); + covered = old.rows + @max(0, up) + @max(0, down); + const up_len: usize = @intCast(@max(0, up)); + const down_len: usize = @intCast(@max(0, down)); + const parts = try p.scratch.allocator().alloc([]const u8, up_len + 1 + down_len); + for (parts[0..up_len], 0..) |*part, i| { + const src = @as(usize, @intCast(row0)) + i; + part.* = if (src < rows.len) rows[src] else ""; + } + const middle: usize = @intCast(old.row); + parts[up_len] = if (fresh) + (if (middle < rows.len) rows[middle] else "") + else + old.text; + for (parts[up_len + 1 ..], 0..) |*part, i| { + const src = @as(usize, @intCast(old.row + old.rows)) + i; + part.* = if (src < rows.len) rows[src] else ""; + } + text = try std.mem.join(p.gpa, "\n", parts); + owned = true; + } + + const row: usize = @intCast(@max(0, want_lo - row0)); + const line_len: i32 = @intCast(modal.lineSlice(text, row).len); + if (col > line_len) { + const spaces = p.scratch.allocator().alloc(u8, @intCast(col - line_len)) catch { + if (owned) p.gpa.free(text); + return null; + }; + @memset(spaces, ' '); + const padded = modal.insertAt(p.gpa, text, .{ .row = row, .col = @intCast(line_len) }, spaces) catch { + if (owned) p.gpa.free(text); + return null; + }; + if (owned) p.gpa.free(text); + text = padded; + owned = true; + } + if (owned) { + if (pane.ovl) |overlay| p.gpa.free(overlay.text); + pane.ovl = .{ .row = row0, .rows = covered, .text = text }; + } + // A terminal's text is its program's: edited here it is a copy, to run + // with Tab or B2, gone when the program draws again. Said at its first + // edit, so it never looks like an edit that was lost. + if (fresh) for (p.panes, 0..) |slot, id| if (slot == pane) + p.setMessage(id, "a copy of the terminal's text: Tab or B2 runs it; what the program draws next replaces it (Ctrl-b types to the program)"); + return .{ .text = pane.ovl.?.text, .row0 = pane.ovl.?.row }; +} + +/// Consume a rewritten overlay, freeing the terminal edit text it replaces. +pub fn setEditText(p: *Pardes, pane: *Pane, new: []u8) void { + const overlay = if (pane.ovl) |*value| value else return pardes.edit.retireEditText(p, new); + for (0..pane.sel.len) |button| { + pane.clearPointerSelection(button); + pane.sel[button].state = .none; + } + p.gpa.free(overlay.text); + overlay.text = new; +} + +/// Set while a frame is drawn: what it reads is no step's. +pub var drawing = false; + +fn buildRows(alloc: std.mem.Allocator, p: *Pardes, pane: *Pane) !Rows { + if (!drawing) pane.terminal.?.read = true; + const full = try pane.terminal.?.vt.screens.active.dumpStringAlloc(p.scratch.allocator(), .{ .screen = .{} }); + var pit = pane.terminal.?.vt.screens.active.pages.rowIterator(.right_down, .{ .screen = .{} }, null); + // split yields one more item than delimiters; the extra final slot is the + // cursor row retained below. + const n_rows = std.mem.count(u8, full, "\n") + 2; + const rows = try alloc.alloc([]const u8, n_rows); + errdefer alloc.free(rows); + // Blanking a prompt row only ever SHORTENS it and the retained cursor row + // adds one separator, so the dump's length plus one bounds the join. + const text = try alloc.alloc(u8, full.len + 1); + errdefer alloc.free(text); + var at: usize = 0; + var n: usize = 0; + var it = std.mem.splitScalar(u8, full, '\n'); + while (it.next()) |raw| { + const shown = if (pit.next()) |pin| + if (pin.rowAndCell().row.semantic_prompt != .none) promptRow(pin, raw) else raw + else + raw; + if (n > 0) { + text[at] = '\n'; + at += 1; + } + @memcpy(text[at..][0..shown.len], shown); + rows[n] = text[at..][0..shown.len]; + at += shown.len; + n += 1; + } + text[at] = '\n'; + at += 1; + rows[n] = text[at..][0..0]; + n += 1; + std.debug.assert(n == n_rows); + return .{ .text_alloc = text, .text = text[0..at], .rows = rows }; +} + +pub fn bodyText(arena: std.mem.Allocator, pane: *Pane) ![]const u8 { + const vp: []const []const u8 = vp: { + const state = pane.terminal orelse break :vp &.{}; + const screen = state.vt.screens.active; + var tl = screen.pages.getTopLeft(.viewport); + tl.x = 0; + const br = screen.pages.getBottomRight(.viewport) orelse return error.UnknownPoint; + var rows_out: std.Io.Writer.Allocating = .init(arena); + try screen.dumpString(&rows_out.writer, .{ .tl = tl, .br = br, .unwrap = false }); + const raw = try rows_out.toOwnedSlice(); + var prompts = screen.pages.rowIterator(.right_down, .{ .viewport = .{} }, null); + const vp = try arena.alloc([]const u8, std.mem.count(u8, raw, "\n") + 1); + var lines = std.mem.splitScalar(u8, raw, '\n'); + var n: usize = 0; + while (lines.next()) |ln| { + vp[n] = if (pane.body.mode != .tty) + if (prompts.next()) |pin| + if (pin.rowAndCell().row.semantic_prompt != .none) promptRow(pin, ln) else ln + else + ln + else + ln; + n += 1; + } + std.debug.assert(n == vp.len); + break :vp vp; + }; + + const len = fillBody(null, pane, vp); + const out = try arena.alloc(u8, len); + const filled = fillBody(out, pane, vp); + std.debug.assert(filled == out.len); + return out; +} + +pub const BodyRow = union(enum) { + grid: i32, + edit: struct { line: []const u8, idx: usize }, +}; + +const BodyWalk = struct { + pane: *Pane, + goff: i32, + g: i32, + /// the buffer can start above the viewport: drop the lines scrolled past + skip: usize, + n: usize = 0, + lines: ?std.mem.SplitIterator(u8, .scalar) = null, + covered: i32 = 0, + line_idx: usize = 0, + + fn init(pane: *Pane) BodyWalk { + const off = pane.scroll(); + const goff = gridOffset(pane); + return .{ + .pane = pane, + .goff = goff, + .g = if (pane.body.mode == .tty) goff + pane.pan else pane.gridRow(off), + .skip = if (pane.ovl) |o| @intCast(@max(0, off - pane.surfRow(o.row))) else 0, + }; + } + + fn next(w: *BodyWalk) ?BodyRow { + while (w.n < w.pane.rows) { + if (w.lines) |*it| { + if (it.next()) |line| { + const idx = w.line_idx; + w.line_idx += 1; + // Lines scrolled off the top still count: the index names a + // line of the BUFFER, not of the visible body. + if (w.skip > 0) { + w.skip -= 1; + continue; + } + w.n += 1; + return .{ .edit = .{ .line = line, .idx = idx } }; + } + // The buffer stands in for `rows` shell rows however many lines + // it actually spelled, which is the whole slide. + w.g += w.covered; + w.skip = 0; + w.lines = null; + continue; + } + if (w.pane.body.mode != .tty) if (w.pane.ovl) |o| if (w.g == o.row) { + w.lines = std.mem.splitScalar(u8, o.text, '\n'); + w.covered = o.rows; + w.line_idx = 0; + continue; + }; + const vi = w.g - w.goff; + w.g += 1; + w.n += 1; + return .{ .grid = vi }; + } + return null; + } +}; + +/// Run the terminal body row walk. A null destination counts bytes; a slice +/// fills the exact allocation made from that count. +fn fillBody(dst: ?[]u8, pane: *Pane, viewport: []const []const u8) usize { + var walk: BodyWalk = .init(pane); + var written: usize = 0; + var first = true; + while (walk.next()) |row| { + if (!first) { + if (dst) |out| out[written] = '\n'; + written += 1; + } + first = false; + const bytes = switch (row) { + .edit => |e| e.line, + .grid => |vi| if (vi >= 0 and @as(usize, @intCast(vi)) < viewport.len) + viewport[@intCast(vi)] + else + "", + }; + if (dst) |out| @memcpy(out[written..][0..bytes.len], bytes); + written += bytes.len; + } + return written; +} + +// Match surviving edit-buffer rows to their original terminal styles. +const EditAnchors = struct { + /// the buffer's own text, walked in order: a line the VIEWPORT skipped still + /// consumes the row it came from, so the lines below it stay aligned + text: []const u8 = &.{}, + at: usize = 0, + shell: []const []const u8 = &.{}, + /// the covered span, absolute grid rows, as `[first, end)` + first: usize = 0, + end: usize = 0, + lines: usize = 0, + /// the line `at` names, and the first row still unclaimed + idx: usize = 0, + cursor: usize = 0, + budget: usize = 0, + active: bool = false, + + fn init(p: *Pardes, pane: *Pane, o: EditBuffer) EditAnchors { + if (o.rows <= 0 or o.row < 0) return .{}; + const first: usize = @intCast(o.row); + const screen = pane.terminal.?.vt.screens.active; + const total = screen.pages.scrollbar().total; + if (first >= total) return .{}; + const covered: usize = @intCast(o.rows); + const count = @min(covered, total - first); + const tl = screen.pages.pin(.{ .screen = .{ .y = @intCast(first) } }) orelse return .{}; + const br = screen.pages.pin(.{ .screen = .{ + .x = screen.pages.cols - 1, + .y = @intCast(first + count - 1), + } }) orelse return .{}; + const arena = p.scratch.allocator(); + var output: std.Io.Writer.Allocating = .init(arena); + screen.dumpString(&output.writer, .{ .tl = tl, .br = br, .unwrap = false }) catch return .{}; + const shell = arena.alloc([]const u8, count) catch return .{}; + var raw = std.mem.splitScalar(u8, output.written(), '\n'); + var pins = tl.rowIterator(.right_down, br); + for (shell) |*row| { + const text = raw.next() orelse ""; + row.* = if (pins.next()) |pin| + if (pin.rowAndCell().row.semantic_prompt != .none) promptRow(pin, text) else text + else + text; + } + const lines = std.mem.count(u8, o.text, "\n") + 1; + return .{ + .text = o.text, + .shell = shell, + .first = first, + .end = first + count, + .lines = lines, + .cursor = first, + .budget = covered + 4 * lines, + .active = true, + }; + } + + fn shellRow(a: *EditAnchors, idx: usize) ?Anchor { + if (!a.active or idx >= a.lines) return null; + var found: ?Anchor = null; + while (a.idx <= idx) : (a.idx += 1) found = a.claim(a.nextLine() orelse return null); + return found; + } + + fn nextLine(a: *EditAnchors) ?[]const u8 { + if (a.at > a.text.len) return null; + const rest = a.text[a.at..]; + if (std.mem.indexOfScalar(u8, rest, '\n')) |n| { + a.at += n + 1; + return rest[0..n]; + } + // The last line has no terminator; one past the end ends the walk. + a.at = a.text.len + 1; + return rest; + } + + /// Where this line still stands over the grid, if anywhere. + fn claim(a: *EditAnchors, line: []const u8) ?Anchor { + const end = if (line.len == 0) @min(a.cursor + 1, a.end) else a.end; + var k = a.cursor; + while (k < end) : (k += 1) { + if (a.budget == 0) return null; + a.budget -= 1; + if (!std.mem.eql(u8, line, a.shell[k - a.first])) continue; + a.cursor = k + 1; + return .{ .row = @intCast(k) }; + } + if (a.cursor >= a.end) return null; + const shell = a.shell[a.cursor - a.first]; + var p: usize = 0; + while (p < line.len and p < shell.len and line[p] == shell[p]) p += 1; + var s: usize = 0; + const room = @min(line.len, shell.len) - p; + while (s < room and line[line.len - 1 - s] == shell[shell.len - 1 - s]) s += 1; + if (p + s == 0) return null; + if (line.len != p + s and shell.len - (p + s) > shell.len / 2) return null; + const row = a.cursor; + if (s > 0 or p >= shell.len) a.cursor += 1; + return .{ .row = @intCast(row), .prefix = p, .suffix = s, .shell_len = shell.len }; + } +}; + +const Anchor = struct { + /// the row, absolute while it comes from `EditAnchors`, viewport once + /// `recolorAnsi` has subtracted the walk's offset + row: i32, + prefix: usize = std.math.maxInt(usize), + suffix: usize = 0, + /// the row's own dumped length — what the suffix is measured from on the + /// GRID side, where the edit may have changed the byte count + shell_len: usize = 0, + + fn whole(an: Anchor) bool { + return an.prefix == std.math.maxInt(usize); + } +}; + +pub fn recolorAnsi(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, tx: u16, tw: u16, body_h: u16, body: []const u8) void { + // No emulator, no ANSI cells: the whole pass — and the 256-colour theme + // projection behind it — is compiled out. + if (pane.terminal == null) return; + const body_y = p.bodyTop(pane, r); + var filtered_storage: FilteredColors = undefined; + const filtered: ?*FilteredColors = if (pane.tty_filter) blk: { + const tz_filter = tracy.zone(@src(), "filterInit"); + defer tz_filter.end(); + filtered_storage = FilteredColors.init(p, &pane.terminal.?.vt); + break :blk &filtered_storage; + } else null; + // DECSCNM, read once: the filtered palette folds it in itself, the raw + // path needs it per cell. + const scnm = pane.terminal.?.vt.modes.get(.reverse_colors); + const pages = &pane.terminal.?.vt.screens.active.pages; + const vp_rows: i32 = @intCast(scrollbar(pane).len); + // Which buffer lines the user has not actually changed, so a row swallowed + // by a growing buffer keeps the colour it still stands over. + var anchors: ?EditAnchors = null; + var walk: BodyWalk = .init(pane); + var lines = std.mem.splitScalar(u8, body, '\n'); + var vr: u16 = 0; + while (walk.next()) |row| : (vr += 1) { + if (vr >= body_h) break; + // Before any early exit below, or the lines fall out of step with rows. + const text = lines.next() orelse ""; + const anchor: Anchor = switch (row) { + .edit => |e| blk: { + if (anchors == null) anchors = .init(p, pane, pane.ovl.?); + var an = anchors.?.shellRow(e.idx) orelse continue; + an.row -= walk.goff; + break :blk an; + }, + .grid => |v| .{ .row = v }, + }; + const vi = anchor.row; + if (vi < 0 or vi >= vp_rows) continue; + const row_pin = pages.pin(.{ .viewport = .{ .y = @intCast(vi) } }) orelse continue; + const cut: u16 = if (pane.body.mode == .tty) 0 else cut: { + if (row_pin.rowAndCell().row.semantic_prompt == .none) break :cut 0; + break :cut switch (promptCut(row_pin)) { + .keep => 0, + // Blanked end to end: the row shows nothing of the grid, so + // projecting the prompt's own colours onto it would be a lie. + .blank => continue, + .cut => |n| std.math.cast(u16, n) orelse continue, + }; + }; + if (cut > 0 and text.len == 0) continue; + var at: usize = 0; + var sc: u16 = 0; + var gc: u16 = cut; + var sb: usize = 0; + const mine_from = @min(anchor.prefix, text.len); + const mine_to = text.len - @min(anchor.suffix, text.len); + var crossed = false; + while (at < text.len and sc < tw) { + const stop = modal.nextGrapheme(text, at); + if (stop <= at) break; + const span: u16 = if (sc + 1 < tw and s.at(tx + sc + 1, body_y + vr).len == 0) 2 else 1; + if (at >= mine_from and at < mine_to) { + sc += span; + at = stop; + continue; + } + if (at >= mine_to and !crossed) { + crossed = true; + const upto = anchor.shell_len - @min(anchor.suffix, anchor.shell_len); + while (sb < upto) { + const ci = cellAt(pages, row_pin, gc) orelse break; + sb += dumpedBytes(row_pin, ci.cell); + gc = std.math.add(u16, gc, 1) catch break; + } + } + const want = stop - at; + // Consume every cell that contributed to this grapheme. A cluster + // ghostty split across several cells is still ONE printed glyph. + var covered: usize = 0; + var style: ?pardes.CellStyle = null; + while (covered < want) { + const ci = cellAt(pages, row_pin, gc) orelse break; + if (style == null and ci.cell.wide != .spacer_tail and ci.cell.wide != .spacer_head) + style = cellStyle(p, ci, filtered, scnm); + covered += dumpedBytes(row_pin, ci.cell); + gc = std.math.add(u16, gc, 1) catch break; + // A spacer contributes no bytes; without this the loop would + // spin on a row that ends in one. + if (covered == 0 and gc >= pane.cols) break; + } + while (cellAt(pages, row_pin, gc)) |t| { + if (t.cell.wide != .spacer_tail) break; + gc = std.math.add(u16, gc, 1) catch break; + } + if (style) |st| for (0..span) |k| { + const cell = s.at(tx + sc + @as(u16, @intCast(k)), body_y + vr); + if (cell.default and filtered == null) continue; + cell.default = false; + cell.style = st; + }; + sb += covered; + sc += span; + at = stop; + } + var tail: ?pardes.CellStyle = null; + if (anchor.whole() or anchor.suffix > 0) { + while (sc < tw) { + const ci = cellAt(pages, row_pin, gc) orelse break; + gc = std.math.add(u16, gc, 1) catch break; + if (ci.cell.wide == .spacer_tail) continue; + const cell = s.at(tx + sc, body_y + vr); + sc += 1; + const style = cellStyle(p, ci, filtered, scnm); + tail = style; + if (cell.default and filtered == null) continue; + cell.default = false; + cell.style = style; + } + if (tail) |style| while (sc < tw) : (sc += 1) { + const cell = s.at(tx + sc, body_y + vr); + if (cell.default and filtered == null) continue; + cell.default = false; + cell.style = style; + }; + } + } +} + +/// The cell at column `x` of the row `row` pins, as `pages.getCell` reads +/// it, without walking from the viewport's top down to the row again: that +/// walk, once a cell, was most of a terminal's paint (once getCell was no +/// longer inlined, a frame of a 120x40 shell took 75 µs more). +fn cellAt(pages: *const ghostty_vt.PageList, row: ghostty_vt.Pin, x: u16) ?ghostty_vt.PageList.Cell { + if (x >= pages.cols or x >= row.node.cols()) return null; + const rac = row.node.page().getRowAndCell(x, row.y); + return .{ .node = row.node, .row = rac.row, .cell = rac.cell, .row_idx = row.y, .col_idx = x }; +} + +// A cell's underline styles are SGR's, in SGR's order (`off` its `none`): a +// cast takes one to the other (cellStyle, pager.zig). +comptime { + for (@typeInfo(@FieldType(pardes.CellStyle, "ul")).@"enum".field_names, 0..) |name, i| + if (@backingInt(@field(ghostty_vt.sgr.Attribute.Underline, if (std.mem.eql(u8, name, "off")) "none" else name)) != i) @compileError("a cell's underline styles are not SGR's"); +} + +fn cellStyle(p: *Pardes, ci: ghostty_vt.PageList.Cell, filtered: ?*FilteredColors, scnm: bool) pardes.CellStyle { + const style = ci.style(); + var cs: pardes.CellStyle = .{ + .fg = if (filtered) |colors| colors.fg(style) else ghostColor(p, style.fg_color, scnm), + .bg = if (filtered) |colors| colors.bg(style, ci.cell) else ghostColor(p, style.bg_color, !scnm), + .bold = style.flags.bold, + .dim = style.flags.faint, + .italic = style.flags.italic, + .blink = style.flags.blink, + .reverse = style.flags.inverse, + .invisible = style.flags.invisible, + .strikethrough = style.flags.strikethrough, + .ul = @fromBackingInt(@intCast(@backingInt(style.flags.underline))), + }; + if (filtered == null) switch (ci.cell.content_tag) { + .bg_color_palette => cs.bg = palColor(p, ci.cell.content.color_palette.data), + .bg_color_rgb => { + const rgb = ci.cell.content.color_rgb; + cs.bg = .{ .rgb = .{ rgb.r, rgb.g, rgb.b } }; + }, + else => {}, + }; + return cs; +} + +fn asPardesColor(rgb: GColor.RGB) pardes.Color { + return .{ .rgb = .{ rgb.r, rgb.g, rgb.b } }; +} + +/// A program's colours mapped to the theme (Filter): a terminal's cells, or +/// a +Pager's spans (`vt` null, the program's palette xterm's own). +pub const FilteredColors = struct { + source: GColor.Palette, + target: *const GColor.Palette, + theme_bg: GColor.RGB, + theme_fg: GColor.RGB, + dynamic_bg: ?GColor.RGB, + dynamic_fg: ?GColor.RGB, + default_bg: GColor.RGB, + /// What a foreground too near `default_bg` becomes instead. + fallback_fg: GColor.RGB, + default_bg_luminance: f64, + fg_for_palette: [256]pardes.Color = undefined, + bg_for_palette: [256]pardes.Color = undefined, + /// The two answers for a cell that names no colour of its own. + fg_default: pardes.Color = undefined, + bg_default: pardes.Color = undefined, + cache_rgb: [256]GColor.RGB = undefined, + cache_key: [256]u8 = undefined, + cache_valid: [256]bool = @splat(false), + + pub fn init(p: *Pardes, vt: ?*const ghostty_vt.Terminal) FilteredColors { + var source = GColor.default; + if (vt) |t| { + for (&source, 0..) |*rgb, i| + rgb.* = t.colorForXterm(.{ .palette = @intCast(i) }) orelse rgb.*; + } + const theme = p.theme(); + var theme_bg = asGhostRgb(theme.bg orelse theme.tag_bg); + var theme_fg = asGhostRgb(theme.fg orelse theme.tag_fg); + var dynamic_bg = if (vt) |t| t.colorForXterm(.{ .dynamic = .background }) else null; + var dynamic_fg = if (vt) |t| t.colorForXterm(.{ .dynamic = .foreground }) else null; + if (vt != null and vt.?.modes.get(.reverse_colors)) { + std.mem.swap(GColor.RGB, &theme_bg, &theme_fg); + std.mem.swap(?GColor.RGB, &dynamic_bg, &dynamic_fg); + } + var self: FilteredColors = .{ + .source = source, + .target = p.tty_filter_palette.get(theme), + .theme_bg = theme_bg, + .theme_fg = theme_fg, + .dynamic_bg = dynamic_bg, + .dynamic_fg = dynamic_fg, + .default_bg = theme_bg, + .fallback_fg = theme_fg, + .default_bg_luminance = luminanceOf(theme_bg), + }; + if (dynamic_bg) |rgb| self.default_bg = self.keyedRgb(rgb); + self.default_bg_luminance = luminanceOf(self.default_bg); + if (self.theme_bg.contrast(self.default_bg) > self.theme_fg.contrast(self.default_bg)) + self.fallback_fg = self.theme_bg; + + self.fg_default = asPardesColor(self.legible( + if (self.dynamic_fg) |rgb| self.keyedRgb(rgb) else self.theme_fg, + )); + self.bg_default = asPardesColor(self.default_bg); + for (&self.source, 0..) |current, i| { + const idx: u8 = @intCast(i); + const mapped = self.paletteRgb(idx, current); + self.fg_for_palette[idx] = asPardesColor(self.legible(mapped)); + self.bg_for_palette[idx] = asPardesColor(mapped); + } + return self; + } + + fn fg(self: *FilteredColors, style: ghostty_vt.Style) pardes.Color { + return switch (style.fg_color) { + .none => self.fg_default, + .palette => |idx| self.fg_for_palette[idx], + .rgb => asPardesColor(self.legible(self.keyedRgb(style.fg(.{ + .default = self.dynamic_fg orelse self.theme_fg, + .palette = &self.source, + .bold = null, + })))), + }; + } + + fn bg(self: *FilteredColors, style: ghostty_vt.Style, cell: *const ghostty_vt.Cell) pardes.Color { + switch (cell.content_tag) { + .bg_color_palette => return self.bg_for_palette[cell.content.color_palette.data], + .bg_color_rgb => {}, + else => switch (style.bg_color) { + .none => return self.bg_default, + .palette => |idx| return self.bg_for_palette[idx], + .rgb => {}, + }, + } + // Truecolour, from either the cell or its style. + return asPardesColor(self.keyedRgb(style.bg(cell, &self.source).?)); + } + + /// A +Pager span's colour (pager.zig), as `fg` and `bg` map a cell's. + pub fn spanColor(self: *FilteredColors, color: pardes.Color, is_bg: bool) pardes.Color { + return switch (color) { + .default => color, + .index => |idx| if (is_bg) self.bg_for_palette[idx] else self.fg_for_palette[idx], + .rgb => |rgb| blk: { + const keyed = self.keyedRgb(asGhostRgb(rgb)); + break :blk asPardesColor(if (is_bg) keyed else self.legible(keyed)); + }, + }; + } + + fn legible(self: *const FilteredColors, rgb: GColor.RGB) GColor.RGB { + if (contrastOf(luminanceOf(rgb), self.default_bg_luminance) >= + config.tty_filter_min_contrast) return rgb; + return self.fallback_fg; + } + + fn paletteRgb(self: *FilteredColors, idx: u8, current: GColor.RGB) GColor.RGB { + if (current.eql(GColor.default[idx])) return self.target[idx]; + return self.keyedRgb(current); + } + + fn keyedRgb(self: *FilteredColors, rgb: GColor.RGB) GColor.RGB { + return self.target[self.nearestKey(rgb)]; + } + + fn nearestKey(self: *FilteredColors, rgb: GColor.RGB) u8 { + const rgb24 = (@as(u32, rgb.r) << 16) | (@as(u32, rgb.g) << 8) | rgb.b; + const slot: u8 = @truncate((rgb24 *% 0x9e3779b1) >> 24); + if (self.cache_valid[slot] and self.cache_rgb[slot].eql(rgb)) + return self.cache_key[slot]; + + var best: u8 = 0; + var best_distance: u32 = std.math.maxInt(u32); + for (GColor.default, 0..) |candidate, i| { + const distance = colorDistance(rgb, candidate); + // Strict comparison makes duplicate-colour ties stable at the + // lowest canonical xterm key. + if (distance < best_distance) { + best_distance = distance; + best = @intCast(i); + } + } + self.cache_rgb[slot] = rgb; + self.cache_key[slot] = best; + self.cache_valid[slot] = true; + return best; + } +}; + +const channel_luminance: [256]f64 = blk: { + @setEvalBranchQuota(20000); + var table: [256]f64 = undefined; + for (&table, 0..) |*slot, c| { + const normalized: f64 = @as(f64, @floatFromInt(c)) / 255; + slot.* = if (normalized <= 0.03928) + normalized / 12.92 + else + std.math.pow(f64, (normalized + 0.055) / 1.055, 2.4); + } + break :blk table; +}; + +fn luminanceOf(rgb: GColor.RGB) f64 { + return 0.2126 * channel_luminance[rgb.r] + + 0.7152 * channel_luminance[rgb.g] + + 0.0722 * channel_luminance[rgb.b]; +} + +/// ghostty's `RGB.contrast` with both luminances already in hand. +fn contrastOf(a_luminance: f64, b_luminance: f64) f64 { + const lighter = @max(a_luminance, b_luminance); + const darker = @min(a_luminance, b_luminance); + return (lighter + 0.05) / (darker + 0.05); +} + +fn colorDistance(a: GColor.RGB, b: GColor.RGB) u32 { + const dr = @as(i32, a.r) - @as(i32, b.r); + const dg = @as(i32, a.g) - @as(i32, b.g); + const db = @as(i32, a.b) - @as(i32, b.b); + return @intCast(dr * dr + dg * dg + db * db); +} + +pub fn palColor(p: *Pardes, idx: u8) pardes.Color { + if (p.theme().palette) |pal| if (idx < 16) return .{ .rgb = pal[idx] }; + return .{ .index = idx }; +} + +fn ghostColor(p: *Pardes, color: ghostty_vt.Style.Color, is_bg: bool) pardes.Color { + return switch (color) { + .none => blk: { + const t = if (is_bg) p.theme().bg else p.theme().fg; + break :blk if (t) |c| .{ .rgb = c } else .default; + }, + .palette => |idx| palColor(p, idx), + .rgb => |rgb| .{ .rgb = .{ rgb.r, rgb.g, rgb.b } }, + }; +} + +/// executing at a prompt with typed text below it: pad the output area +/// with newlines so the command's output doesn't overwrite the buffer +pub fn padOutputBelowEdits(p: *Pardes, id: usize) void { + // Nothing to pad away from: with no emulator there is no prompt and no + // child whose output could land on top of the edit buffer. + const pane = p.panes[id] orelse return; + const o = pane.ovl orelse return; + if (!pane.isTerminal()) return; + const state = pane.terminal orelse return; + if (!state.vt.cursorIsAtPrompt()) return; + // the buffer's LAST surface row: its lines may outnumber the shell + // rows it covers, and it is the bottom one output must clear + const max_row = o.row + @as(i32, @intCast(modal.lineCount(o.text))) - 1; + const goff: i32 = @intCast(state.vt.screens.active.pages.scrollbar().offset); + const cursor_abs = pane.surfRow(goff + @as(i32, @intCast(state.vt.screens.active.cursor.y))); + const pad = std.math.clamp(max_row - cursor_abs, 0, @as(i32, pane.rows)); + var i: i32 = 0; + while (i < pad) : (i += 1) p.emitWrite(id, "\r"); +} + +pub fn pushUndo(p: *Pardes, pane: *Pane) error{OutOfMemory}!void { + return pane.body.remember(p.gpa, pane.ovl); +} + +/// Undo (back) or redo one step of the edit overlay. +pub fn undoStep(p: *Pardes, pane: *Pane, back: bool) error{OutOfMemory}!void { + const to = (try pane.body.step(p.gpa, pane.ovl, back)) orelse return; + if (pane.ovl) |o| p.gpa.free(o.text); + pane.ovl = to.ovl; + pane.ensureCursorVisible(); +} + +/// The emulator's size in pixels follows the shell's cells: kitty graphics +/// placements are sized by it, and CSI 14/16 t report it. +fn syncPixels(p: *const Pardes, pane: *Pane) void { + if (comptime !images_enabled) return; + const state = pane.terminal orelse return; + const vt = &state.vt; + vt.width_px = @as(u32, vt.cols) * p.view().cell_pixels.w; + vt.height_px = @as(u32, vt.rows) * p.view().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 { + if (comptime !images_enabled) return; + const state = pane.terminal orelse return; + state.images.petscii = !state.images.petscii; +} + +/// Whether the pane draws its kitty graphics as glyph art by choice; null +/// for a pane with no emulator. +pub fn petscii(pane: *const Pane) ?bool { + if (comptime !images_enabled) return null; + const state = pane.terminal orelse return null; + return state.images.petscii; +} + +/// Whether the screen showing holds any kitty graphics image. +pub fn hasImages(pane: *const Pane) bool { + if (comptime !images_enabled) return false; + const state = pane.terminal orelse return false; + return state.vt.screens.active.kitty_images.images.count() > 0; +} + +/// Draw the pane's kitty graphics images over its body rows (after the +/// rows and their colours: placeholder cells are blanked here). +pub fn drawImages(p: *Pardes, s: *Surface, pane: *Pane, id: usize, tx: u16, tw: u16, body_y: u16, body_h: u16) void { + if (comptime !images_enabled) return; + const state = pane.terminal orelse return; + const screen = state.vt.screens.active; + syncPixels(p, pane); + // A body taller than this draws its images on the rows that fit. + var rows_of: [1024]?u16 = undefined; + const vp_rows: usize = @min(scrollbar(pane).len, rows_of.len); + @memset(rows_of[0..vp_rows], null); + var any_placeholder = false; + var walk: BodyWalk = .init(pane); + var vr: u16 = 0; + while (walk.next()) |row| : (vr += 1) { + if (vr >= body_h) break; + const vi = switch (row) { + .grid => |v| v, + .edit => continue, + }; + if (vi < 0 or vi >= vp_rows) continue; + const row_pin = screen.pages.pin(.{ .viewport = .{ .y = @intCast(vi) } }) orelse continue; + const flags = row_pin.rowAndCell().row; + // A prompt row outside tty mode is cut or hidden: its columns are + // not the grid's. + if (pane.body.mode != .tty and flags.semantic_prompt != .none) continue; + any_placeholder = any_placeholder or flags.kitty_virtual_placeholder; + rows_of[@intCast(vi)] = vr; + } + // Nothing to draw, no placeholder to blank and nothing cached to let + // go: the common case costs the rows' flags. + if (!any_placeholder and screen.kitty_images.placements.count() == 0 and state.images.converted.count() == 0) return; + state.images.draw(p, s, &state.vt, @intCast(id), pane.serial, tx, body_y, @min(tw, pane.cols), rows_of[0..vp_rows]); +} + +/// Every byte the core wrote to each pane's pty since the last call, `\n` +/// between panes' shares: "0:\n1:\n" for panes that got any. +fn ptyWritesByPane(p: *Pardes, out: *std.ArrayList(u8)) ![]const u8 { + out.clearRetainingCapacity(); + var per: [pardes.MAX_PANES]std.ArrayList(u8) = @splat(.empty); + defer for (&per) |*b| b.deinit(p.gpa); + while (p.nextEffect()) |effect| { + if (effect == .write) try per[effect.write.pane].appendSlice(p.gpa, effect.write.bytes.slice()); + } + for (per, 0..) |b, id| if (b.items.len > 0) try out.print(p.gpa, "{d}:{s}\n", .{ id, b.items }); + return out.items; +} + +/// `a=T` of a 4x2 RGB image, `i=7`, red on the left half, blue on the +/// right, with the rest of the control data `extra` (`c=2,r=1` places it +/// over two cells). +inline fn kittyTransmit(comptime extra: []const u8) []const u8 { + return comptime blk: { + const row = [_]u8{ 255, 0, 0, 255, 0, 0, 0, 0, 255, 0, 0, 255 }; + const pixels = row ++ row; + var buf: [std.base64.standard.Encoder.calcSize(pixels.len)]u8 = undefined; + const b64 = std.base64.standard.Encoder.encode(&buf, &pixels)[0..buf.len].*; + break :blk "\x1b_Ga=T,f=24,s=4,v=2,i=7," ++ extra ++ ";" ++ b64 ++ "\x1b\\"; + }; +} + +fn imagePlaces(s: *const Surface) []const pardes.Placement { + return s.placements.items; +} + +/// `n` line breaks: enough of them push the screen's rows into history. +fn lineBreaks(comptime n: usize) [2 * n]u8 { + var out: [2 * n]u8 = undefined; + for (0..n) |i| out[2 * i ..][0..2].* = "\r\n".*; + return out; +} + +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 { + const pane = if (terminal) try create(gpa, 40, 12) else try createDoc(gpa, 40, 12); + defer gpa.destroy(pane); + defer deinitEmulator(pane, gpa); + try std.testing.expectEqual(terminal, pane.terminal != null); + } + }; + try std.testing.expect(@sizeOf(Pane) < 128 * 1024); + try std.testing.checkAllAllocationFailures(std.testing.allocator, Case.run, .{false}); + try std.testing.checkAllAllocationFailures(std.testing.allocator, Case.run, .{true}); +} + +test "a terminal made fresh has no size waiting: one never resized keeps its grid when a unit is cut" { + // Fresh memory as a Debug build's allocator hands it out: 0xAA. A field + // `create` left unset read a size of 43690x43690 (fixer 2's, 2026-10-30). + const Filling = struct { + child: std.mem.Allocator, + fn alloc(ctx: *anyopaque, len: usize, a: std.mem.Alignment, ra: usize) ?[*]u8 { + const self: *@This() = @ptrCast(@alignCast(ctx)); + const ptr = self.child.rawAlloc(len, a, ra) orelse return null; + @memset(ptr[0..len], 0xAA); + return ptr; + } + fn resize(ctx: *anyopaque, buf: []u8, a: std.mem.Alignment, n: usize, ra: usize) bool { + const self: *@This() = @ptrCast(@alignCast(ctx)); + return self.child.rawResize(buf, a, n, ra); + } + fn remap(ctx: *anyopaque, buf: []u8, a: std.mem.Alignment, n: usize, ra: usize) ?[*]u8 { + const self: *@This() = @ptrCast(@alignCast(ctx)); + return self.child.rawRemap(buf, a, n, ra); + } + fn free(ctx: *anyopaque, buf: []u8, a: std.mem.Alignment, ra: usize) void { + const self: *@This() = @ptrCast(@alignCast(ctx)); + self.child.rawFree(buf, a, ra); + } + const vtable: std.mem.Allocator.VTable = .{ .alloc = alloc, .resize = resize, .remap = remap, .free = free }; + }; + var filling: Filling = .{ .child = std.testing.allocator }; + const gpa: std.mem.Allocator = .{ .ptr = &filling, .vtable = &Filling.vtable }; + const pane = try create(gpa, 40, 12); + defer gpa.destroy(pane); + defer deinitEmulator(pane, gpa); + try std.testing.expect(pane.terminal.?.resize_to == null); +} + +test "document construction allocates only the pane and has inert terminal defaults" { + var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{ .fail_index = 1 }); + const allocator = failing.allocator(); + const pane = try createDoc(allocator, 40, 12); + defer allocator.destroy(pane); + defer deinitEmulator(pane, allocator); + try std.testing.expect(pane.terminal == null); + try std.testing.expectEqual(@as(usize, 1), failing.alloc_index); + try std.testing.expectEqual(@as(usize, 0), failing.resize_index); + try std.testing.expect(!failing.has_induced_failure); + try std.testing.expectEqual(@as(usize, @sizeOf(Pane)), failing.allocated_bytes); + try std.testing.expectEqual(GridCursor{}, gridCursor(pane)); + try std.testing.expectEqual(Scrollbar{}, scrollbar(pane)); + try std.testing.expect(!bracketedPaste(pane)); + try std.testing.expect(!reportsMouse(pane)); + try std.testing.expect(!promptInputReady(pane)); + scrollGrid(pane, 100); + followOutput(pane); + resizeGrid(pane, 80, 24); + try std.testing.expectEqual(GridCursor{}, gridCursor(pane)); + try std.testing.expectEqual(Scrollbar{}, scrollbar(pane)); + try std.testing.expectEqualStrings("", try screenTextAlloc(pane, allocator)); + try std.testing.expect(!failing.has_induced_failure); +} + +test "terminal state keeps parser fragments and sends emulator replies through its owner" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 40, .rows = 12 }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + const pane = p.panes[0].?; + const state = pane.terminal.?; + try std.testing.expect(state.stream.handler.inner.terminal == &state.vt); + p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[" } }); + while (p.nextEffect()) |effect| { + if (effect == .write) return error.PrematureTerminalReply; + } + p.update(.{ .output = .{ .pane = 0, .bytes = "6n" } }); + var replies: usize = 0; + while (p.nextEffect()) |effect| { + if (effect != .write) continue; + try std.testing.expectEqual(@as(u8, 0), effect.write.pane); + try std.testing.expectEqualStrings("\x1b[1;1R", effect.write.bytes.slice()); + replies += 1; + } + try std.testing.expectEqual(@as(usize, 1), replies); + try std.testing.expectEqual(@as(u16, 0), state.reply_len); +} + +test "a terminal's dump restores the cells it drew, its history and where its view was" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + const pane = p.panes[0].?; + feedOutput(p, pane, "\x1b[31mred\x1b[0m\r\ncafé\r\n"); + for (0..30) |i| { + var line: [16]u8 = undefined; + feedOutput(p, pane, std.fmt.bufPrint(&line, "line {d}\r\n", .{i}) catch unreachable); + } + scrollGrid(pane, -5); + const bytes = try pardes.walk.save(State, gpa, pane.terminal.?); + defer gpa.free(bytes); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const state = try gpa.create(State); + defer gpa.destroy(state); + state.* = try pardes.walk.load(State, gpa, bytes, null); + defer { + state.stream.deinit(); + state.vt.deinit(gpa); + } + try std.testing.expectEqual(pardes.walk.digest(State, pane.terminal.?), pardes.walk.digest(State, state)); + try std.testing.expectEqual(pane.terminal.?.vt.screens.active.pages.scrollbar(), state.vt.screens.active.pages.scrollbar()); +} + +test "a restored command pane comes back finished, its command not run again" { + if (comptime pardes.isolated) return error.SkipZigTest; + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 16 }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + const cmd = p.freeSlot().?; + _ = try panes.newCommand(p, cmd, "/tmp", "make test"); + feedOutput(p, p.panes[cmd].?, "ok\r\n"); + p.update(.{ .exited = .{ .pane = @intCast(cmd), .status = 2 } }); + try pardes.dump.dumpState(p); + const restored = try pardes.dump.initFromDump(gpa, .{ .tty_only = true, .cols = 60, .rows = 16 }, p.dump_out.?); + defer restored.deinit(); + var spawns: usize = 0; + while (restored.nextEffect()) |effect| switch (effect) { + .spawn => spawns += 1, + else => {}, + }; + try std.testing.expectEqual(@as(usize, 1), spawns); // the shell, not the command + const again = for (restored.panes) |slot| { + if (slot) |pane| if (pane.command != null) break pane; + } else return error.CommandPaneLost; + try std.testing.expectEqualStrings("make test", again.command.?); + try std.testing.expect(again.command_done); + try std.testing.expectEqual(@as(?u8, 2), again.command_status); + const text = try screenTextAlloc(again, gpa); + defer gpa.free(text); + try std.testing.expect(std.mem.indexOf(u8, text, "ok") != null); +} + +test "a restored terminal keeps its history above a marker and gets a shell where it was" { + if (comptime pardes.isolated) return error.SkipZigTest; + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + const pane = p.panes[0].?; + feedOutput(p, pane, "old output\r\n$ ls\r\n"); + try pane.setOwnedCwd("/tmp"); + pane.shell = try gpa.dupe(u8, "bash"); // `Term bash`: it comes back as bash + try pardes.dump.dumpState(p); + const restored = try pardes.dump.initFromDump(gpa, .{ .tty_only = true, .cols = 40, .rows = 12 }, p.dump_out.?); + defer restored.deinit(); + var spawned = false; + while (restored.nextEffect()) |effect| switch (effect) { + .spawn => |sp| spawned = sp.pane == 0 and std.mem.eql(u8, sp.cwd.slice(), "/tmp"), + else => {}, + }; + try std.testing.expect(spawned); + const again = restored.panes[0].?; + try std.testing.expectEqualStrings("bash", again.shell.?); + try std.testing.expect(again.command == null); + try std.testing.expectEqual(.spawn, again.pending_command.wait); + const text = try screenTextAlloc(again, gpa); + defer gpa.free(text); + const old_at = std.mem.indexOf(u8, text, "old output") orelse return error.HistoryLost; + const mark_at = std.mem.indexOf(u8, text, "restored history") orelse return error.NoMarker; + try std.testing.expect(old_at < mark_at); +} + +test "a position that cannot follow the output for want of memory is said on the message row, and logged" { + var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{}); + const p = try Pardes.init(failing.allocator(), .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + const pane = p.panes[0].?; + feedOutput(p, pane, "one\r\ntwo\r\n"); + // More positions than the emulator's pins have room for: following them + // through the output needs memory, and there is none. + try std.testing.expect(pane.body.reserveSels()); + for (pane.body.sels[0..8], 0..) |*range, i| range.* = .{ .row = @intCast(i), .col = 0, .arow = @intCast(i), .acol = 1 }; + pane.body.nsel = 8; + failing.fail_index = failing.alloc_index; + feedOutput(p, pane, "three\r\n"); + failing.fail_index = std.math.maxInt(usize); + try std.testing.expect(failing.has_induced_failure); + try std.testing.expectEqualStrings(positions_lost, pane.msg[0..pane.msg_len]); + const logged = p.messages.at(p.messages.len -| 1) orelse return error.NothingLogged; + try std.testing.expectEqualStrings(positions_lost, logged.slice()); +} + +test "raw terminal output needs no position tracking allocations" { + var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{}); + const p = try Pardes.init(failing.allocator(), .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + const pane = p.panes[0].?; + pane.body.mode = .tty; + feedOutput(p, pane, "warm"); + const allocations = failing.alloc_index; + failing.fail_index = allocations; + const pins = pane.terminal.?.vt.screens.active.pages.countTrackedPins(); + feedOutput(p, pane, " output"); + try std.testing.expectEqual(allocations, failing.alloc_index); + try std.testing.expect(!failing.has_induced_failure); + try std.testing.expectEqual(pins, pane.terminal.?.vt.screens.active.pages.countTrackedPins()); +} + +test "rows asked for after a pane's cached rows were dropped, before the sweep, leak nothing" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true }); + defer p.deinit(); + const pane = p.panes[0].?; + try std.testing.expect(pane.terminal != null); + _ = try shellRows(p, pane); + // A pane's teardown drops its entry; the rows stay until the sweep. + p.shell_rows.dropPane(pane); + _ = try shellRows(p, pane); +} + +test "the luminance table answers exactly what ghostty computes" { + for (0..256) |i| { + const c: u8 = @intCast(i); + const grey: GColor.RGB = .{ .r = c, .g = c, .b = c }; + try std.testing.expectEqual(grey.luminance(), luminanceOf(grey)); + } + // Channel weights are asymmetric, so a grey ramp alone would not catch a + // transposed coefficient. The palette is what the tables enumerate. + for (GColor.default) |candidate| { + try std.testing.expectEqual(candidate.luminance(), luminanceOf(candidate)); + for (GColor.default) |page| { + try std.testing.expectEqual( + candidate.contrast(page), + contrastOf(luminanceOf(candidate), luminanceOf(page)), + ); + } + } +} + +test "terminal Filter keys indexed truecolor OSC and background-only cells through the theme" { + const testing = std.testing; + // Raw index assertions require a theme that inherits the ANSI palette. + const p = try Pardes.init(testing.allocator, .{ .startup_config = "Theme helix\n", .tty_only = true, .cols = 18, .rows = 6 }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + const pane = p.panes[0].?; + pane.tag_expanded = false; // a one-row tag keeps this small screen's rows + p.sync(); + try testing.expect(pane.tty_filter); + pane.tty_filter = false; + + p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[31mA" ++ + "\x1b[38;5;196mB" ++ + "\x1b[38;2;255;0;0mC" ++ + "\x1b[0;48;5;25mD" ++ + "\x1b[0;48;2;0;95;175mE" ++ + "\x1b[0;1;2;3;4;5;7;8;9mF" ++ + "\x1b[0;48;5;25m\x1b[K" ++ + "\r\n\x1b[0;38;5;2m界" } }); + + var frame = std.heap.ArenaAllocator.init(testing.allocator); + defer frame.deinit(); + const r = p.view().rects[0]; + const tx = r.x + config.GUTTER; + const body_y = p.bodyTop(pane, r); + + const raw = try p.render(frame.allocator()); + try testing.expectEqual(pardes.Color{ .index = 1 }, raw.at(tx, body_y).style.fg); + try testing.expectEqual(pardes.Color{ .index = 196 }, raw.at(tx + 1, body_y).style.fg); + try testing.expectEqual(pardes.Color{ .rgb = .{ 255, 0, 0 } }, raw.at(tx + 2, body_y).style.fg); + + pane.tty_filter = true; + _ = frame.reset(.retain_capacity); + const filtered = try p.render(frame.allocator()); + var expected_cache: FilterPalette = .{}; + const expected = expected_cache.get(p.theme()); + try testing.expectEqual(asPardesColor(expected[1]), filtered.at(tx, body_y).style.fg); + try testing.expectEqual(asPardesColor(expected[196]), filtered.at(tx + 1, body_y).style.fg); + try testing.expectEqual(filtered.at(tx + 1, body_y).style.fg, filtered.at(tx + 2, body_y).style.fg); + try testing.expectEqual(asPardesColor(expected[25]), filtered.at(tx + 3, body_y).style.bg); + try testing.expectEqual(filtered.at(tx + 3, body_y).style.bg, filtered.at(tx + 4, body_y).style.bg); + + const attrs = filtered.at(tx + 5, body_y).style; + try testing.expect(attrs.bold); + try testing.expect(attrs.dim); + try testing.expect(attrs.italic); + try testing.expect(attrs.blink); + try testing.expect(attrs.reverse); + try testing.expect(attrs.invisible); + try testing.expect(attrs.strikethrough); + try testing.expectEqual(.single, attrs.ul); + + const erased = pane.terminal.?.vt.screens.active.pages.getCell(.{ .viewport = .{ .x = 6, .y = 0 } }).?; + try testing.expectEqual(.bg_color_palette, erased.cell.content_tag); + try testing.expectEqual(asPardesColor(expected[25]), filtered.at(tx + 6, body_y).style.bg); + try testing.expectEqual(asPardesColor(expected[2]), filtered.at(tx, body_y + 1).style.fg); + try testing.expectEqual(filtered.at(tx, body_y + 1).style, filtered.at(tx + 1, body_y + 1).style); + // A filtered terminal never delegates either colour to a backend palette, + // including cells which were empty/default before the pass. + for (0..r.w - config.GUTTER) |col| { + const cell = filtered.at(tx + @as(u16, @intCast(col)), body_y); + try testing.expect(!cell.default); + switch (cell.style.fg) { + .rgb => {}, + else => return error.FilteredForegroundWasNotRgb, + } + switch (cell.style.bg) { + .rgb => {}, + else => return error.FilteredBackgroundWasNotRgb, + } + } + + p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b]4;1;#ff0000\x1b\\" } }); + const osc_red = pane.terminal.?.vt.colorForXterm(.{ .palette = 1 }).?; + try testing.expect(osc_red.eql(.{ .r = 255, .g = 0, .b = 0 })); + pane.tty_filter = false; + pane.tty_filter = true; + try testing.expect(osc_red.eql(pane.terminal.?.vt.colorForXterm(.{ .palette = 1 }).?)); + _ = frame.reset(.retain_capacity); + const osc_palette = try p.render(frame.allocator()); + try testing.expectEqual(asPardesColor(expected[196]), osc_palette.at(tx, body_y).style.fg); + + // Dynamic default foreground/background colours key every default cell, + // including the otherwise blank end of the row. + p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b]10;#ff0000\x1b\\" ++ + "\x1b]11;#5f5f5f\x1b\\" } }); + const dyn_fg = pane.terminal.?.vt.colorForXterm(.{ .dynamic = .foreground }).?; + const dyn_bg = pane.terminal.?.vt.colorForXterm(.{ .dynamic = .background }).?; + try testing.expect(dyn_fg.eql(.{ .r = 255, .g = 0, .b = 0 })); + try testing.expect(dyn_bg.eql(.{ .r = 95, .g = 95, .b = 95 })); + _ = frame.reset(.retain_capacity); + const dynamic = try p.render(frame.allocator()); + const blank = dynamic.at(tx + r.w - config.GUTTER - 1, body_y + 2).style; + try testing.expectEqual(asPardesColor(expected[196]), blank.fg); + try testing.expectEqual(asPardesColor(expected[59]), blank.bg); + + const explicit_before_reverse = dynamic.at(tx, body_y).style.fg; + try testing.expectEqual(asPardesColor(expected[196]), explicit_before_reverse); + p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?5h" } }); + _ = frame.reset(.retain_capacity); + const reversed = try p.render(frame.allocator()); + const reversed_blank = reversed.at(tx + r.w - config.GUTTER - 1, body_y + 2).style; + try testing.expectEqual(asPardesColor(expected[59]), reversed_blank.fg); + try testing.expectEqual(asPardesColor(expected[196]), reversed_blank.bg); + const reversed_explicit = reversed.at(tx, body_y).style; + try testing.expectEqual(asPardesColor(expected[196]), reversed_explicit.bg); + try testing.expectEqual(pardes.Color{ .rgb = p.theme().bg.? }, reversed_explicit.fg); + try testing.expect(!std.meta.eql(reversed_explicit.fg, reversed_explicit.bg)); + + p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?5l" } }); + _ = frame.reset(.retain_capacity); + const unreversed = try p.render(frame.allocator()); + const unreversed_blank = unreversed.at(tx + r.w - config.GUTTER - 1, body_y + 2).style; + try testing.expectEqual(asPardesColor(expected[196]), unreversed_blank.fg); + try testing.expectEqual(asPardesColor(expected[59]), unreversed_blank.bg); + + // The cache is keyed by values, not a theme name. Replacing a custom + // theme in place immediately recolours already-rendered indexed cells. + p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b]104;1\x1b\\" } }); + var custom = p.theme().*; + custom.name = try p.gpa.dupe(u8, "same-name"); + custom.palette = null; + custom.kw = .{ 1, 2, 3 }; + p.custom_theme = custom; + _ = frame.reset(.retain_capacity); + const custom_first = try p.render(frame.allocator()); + try testing.expectEqual(pardes.Color{ .rgb = .{ 1, 2, 3 } }, custom_first.at(tx, body_y).style.fg); + if (p.custom_theme) |*theme| theme.kw = .{ 4, 5, 6 }; + _ = frame.reset(.retain_capacity); + const custom_second = try p.render(frame.allocator()); + try testing.expectEqual(pardes.Color{ .rgb = .{ 4, 5, 6 } }, custom_second.at(tx, body_y).style.fg); +} + +test "terminal Filter keeps extended keys dark-to-light on a light theme" { + const p = try Pardes.init(std.testing.allocator, .{ .startup_config = "Theme acme\n", .tty_only = true }); + defer p.deinit(); + try std.testing.expectEqualStrings("acme", p.theme().name); + var cache: FilterPalette = .{}; + const palette = cache.get(p.theme()); + try std.testing.expect(palette[16].eql(asGhostRgb(p.theme().fg.?))); + try std.testing.expect(palette[231].eql(asGhostRgb(p.theme().bg.?))); +} + +test "terminal Filter preserves exact palette-null light theme default roles" { + const testing = std.testing; + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 12, .rows = 5 }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + + var light = p.theme().*; + light.name = try p.gpa.dupe(u8, "filter-light-defaults"); + light.bg = .{ 0xf8, 0xf8, 0xf8 }; + light.fg = .{ 0x38, 0x38, 0x38 }; + light.palette = null; + p.custom_theme = light; + + const pane = p.panes[0].?; + try testing.expectEqual(@as(?GColor.RGB, null), pane.terminal.?.vt.colorForXterm(.{ .dynamic = .foreground })); + try testing.expectEqual(@as(?GColor.RGB, null), pane.terminal.?.vt.colorForXterm(.{ .dynamic = .background })); + pane.tty_filter = true; + + var frame = std.heap.ArenaAllocator.init(testing.allocator); + defer frame.deinit(); + const r = p.view().rects[0]; + const tx = r.x + config.GUTTER; + const body_y = p.bodyTop(pane, r); + const ordinary = try p.render(frame.allocator()); + try testing.expectEqual(pardes.Color{ .rgb = light.fg.? }, ordinary.at(tx, body_y).style.fg); + try testing.expectEqual(pardes.Color{ .rgb = light.bg.? }, ordinary.at(tx, body_y).style.bg); + + p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?5h" } }); + _ = frame.reset(.retain_capacity); + const reversed = try p.render(frame.allocator()); + try testing.expectEqual(pardes.Color{ .rgb = light.bg.? }, reversed.at(tx, body_y).style.fg); + try testing.expectEqual(pardes.Color{ .rgb = light.fg.? }, reversed.at(tx, body_y).style.bg); +} + +test "terminal Filter maps the default roles before it maps anything else" { + const testing = std.testing; + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 18, .rows = 6 }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + const pane = p.panes[0].?; + try testing.expect(pane.tty_filter); + + // Native dark/light palettes plus legacy helix/dark/acme, including + // the null-page fallback that only the legacy dark theme exercises. + for (0..9) |t| { + p.settings.theme = @intCast(t); + const stage_one = FilteredColors.init(p, &pane.terminal.?.vt); + // `dark` declares no background of its own, which is exactly why the + // resolver reads the tag colours as the fallback rather than `.?`. + const theme = p.theme(); + try testing.expect(stage_one.theme_bg.eql(asGhostRgb(theme.bg orelse theme.tag_bg))); + try testing.expect(stage_one.theme_fg.eql(asGhostRgb(theme.fg orelse theme.tag_fg))); + // With no OSC 11 in play the mapped page IS that anchor, and the + // fallback is the other one: a background never contrasts with itself. + try testing.expect(stage_one.default_bg.eql(stage_one.theme_bg)); + try testing.expect(stage_one.fallback_fg.eql(stage_one.theme_fg)); + } + + p.settings.theme = 0; + p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b]11;#5f5f5f\x1b\\" } }); + var moved = FilteredColors.init(p, &pane.terminal.?.vt); + try testing.expect(!moved.default_bg.eql(moved.theme_bg)); + try testing.expect(moved.default_bg.eql(moved.keyedRgb(.{ .r = 0x5f, .g = 0x5f, .b = 0x5f }))); +} + +test "terminal Filter refuses a foreground that would collapse onto the page" { + const testing = std.testing; + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 18, .rows = 6 }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + const pane = p.panes[0].?; + try testing.expect(pane.tty_filter); + + p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[38;2;255;255;255mW" ++ + "\x1b[0;30mB" ++ + "\x1b[0;31mR" } }); + + var frame = std.heap.ArenaAllocator.init(testing.allocator); + defer frame.deinit(); + const r = p.view().rects[0]; + const tx = r.x + config.GUTTER; + const body_y = p.bodyTop(pane, r); + + for (0..9) |t| { + p.settings.theme = @intCast(t); + var fc = FilteredColors.init(p, &pane.terminal.?.vt); + const page = asPardesColor(fc.default_bg); + const rescued = asPardesColor(fc.fallback_fg); + _ = frame.reset(.retain_capacity); + const g = try p.render(frame.allocator()); + + // The colour each of the three would have been given with no floor. + const raw_white = fc.keyedRgb(.{ .r = 255, .g = 255, .b = 255 }); + const raw_black = fc.paletteRgb(0, GColor.default[0]); + const raw_red = fc.paletteRgb(1, GColor.default[1]); + + for ([_]struct { at: u16, raw: GColor.RGB }{ + .{ .at = 0, .raw = raw_white }, + .{ .at = 1, .raw = raw_black }, + .{ .at = 2, .raw = raw_red }, + }) |case| { + const cell = g.at(tx + case.at, body_y).style; + try testing.expectEqual(page, cell.bg); + if (case.raw.contrast(fc.default_bg) < config.tty_filter_min_contrast) { + // Refused: the projection's answer is not painted, the anchor is. + try testing.expectEqual(rescued, cell.fg); + try testing.expect(!std.meta.eql(cell.fg, cell.bg)); + } else { + // Cleared the floor, so stage two leaves it exactly alone. + try testing.expectEqual(asPardesColor(case.raw), cell.fg); + } + // Either way a filtered cell delegates neither colour to a backend. + switch (cell.fg) { + .rgb => |ink| try testing.expect(asGhostRgb(ink).contrast(fc.default_bg) >= + config.tty_filter_min_contrast), + else => return error.FilteredForegroundWasNotRgb, + } + } + + // At least one of the three has to have been a real collapse, or this + // theme proved nothing: white on the light theme, black on the dark. + try testing.expect(raw_white.contrast(fc.default_bg) < config.tty_filter_min_contrast or + raw_black.contrast(fc.default_bg) < config.tty_filter_min_contrast); + // A saturated red is never the page on any curated theme. + try testing.expect(raw_red.contrast(fc.default_bg) >= config.tty_filter_min_contrast); + } +} + +test "terminal Filter holds every projected foreground off the page" { + const testing = std.testing; + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 18, .rows = 6 }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + const pane = p.panes[0].?; + + for (0..9) |t| { + p.settings.theme = @intCast(t); + var fc = FilteredColors.init(p, &pane.terminal.?.vt); + var refused: usize = 0; + for (fc.target, 0..) |projected, key| { + const ink = fc.legible(projected); + try testing.expect(ink.contrast(fc.default_bg) >= config.tty_filter_min_contrast); + if (!ink.eql(projected)) { + refused += 1; + try testing.expect(ink.eql(fc.fallback_fg)); + // Only ever refused for being too near the page. + try testing.expect(projected.contrast(fc.default_bg) < config.tty_filter_min_contrast); + } + // The key a background asks for is handed back untouched, including + // the one whose value is the page itself. + try testing.expect(fc.keyedRgb(GColor.default[key]).eql(fc.target[fc.nearestKey(GColor.default[key])])); + } + // Every curated theme owns at least one collapsing key — that is why + // the floor exists — and the floor must not be flattening the palette. + try testing.expect(refused > 0); + try testing.expect(refused < fc.target.len / 8); + } +} + +test "tty ansi colors follow the prompt hug into normal mode" { + const testing = std.testing; + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 18, .rows = 6 }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + const pane = p.panes[0].?; + pane.tty_filter = false; + + p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b]133;A\x1b\\\x1b[32mPP\x1b]133;B\x1b\\\x1b[31mR\x1b[34mB\x1b[0m out" } }); + + var frame = std.heap.ArenaAllocator.init(testing.allocator); + defer frame.deinit(); + const r = p.view().rects[0]; + const tx = r.x + config.GUTTER; + const body_y = p.bodyTop(pane, r); + const red: pardes.Color = .{ .rgb = p.theme().palette.?[1] }; + const blue: pardes.Color = .{ .rgb = p.theme().palette.?[4] }; + + // tty mode projects the emulator's ansi colours cell for cell. + pane.body.mode = .tty; + p.shell_rows.stale = true; + const tty = try p.render(frame.allocator()); + try testing.expectEqual(red, tty.at(tx + 2, body_y).style.fg); + try testing.expectEqual(blue, tty.at(tx + 3, body_y).style.fg); + + pane.body.mode = .normal; + p.shell_rows.stale = true; + _ = frame.reset(.retain_capacity); + const norm = try p.render(frame.allocator()); + try testing.expectEqualStrings("R", norm.at(tx, body_y).grapheme()); + try testing.expectEqualStrings("B", norm.at(tx + 1, body_y).grapheme()); + try testing.expectEqual(red, norm.at(tx, body_y).style.fg); + try testing.expectEqual(blue, norm.at(tx + 1, body_y).style.fg); +} + +test "a prompt row hidden end to end paints nothing at all" { + const testing = std.testing; + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 12, .rows = 8 }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + const pane = p.panes[0].?; + pane.tag_expanded = false; // a 12x8 screen: the body, not the tag + p.sync(); + pane.tty_filter = false; + pane.body.mode = .normal; + p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b]133;A\x1b\\\x1b[32maaaaaaaaa\x1b]133;B\x1b\\\x1b[41;36m\u{754C}\x1b[0m\r\n" } }); + + var frame = std.heap.ArenaAllocator.init(testing.allocator); + defer frame.deinit(); + const r = p.view().rects[0]; + const tx = r.x + config.GUTTER; + const body_y = p.bodyTop(pane, r); + p.shell_rows.stale = true; + const s = try p.render(frame.allocator()); + + try testing.expectEqualStrings(" ", s.at(tx, body_y).grapheme()); + try testing.expect(!std.meta.eql(pardes.Color{ .rgb = p.theme().palette.?[1] }, s.at(tx, body_y).style.bg)); +} + +test "a program that asked for focus reports hears CSI I and CSI O as its pane takes and gives up the keyboard, and as the window does" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .cols = 90, .rows = 30, .shells = 3 }); + defer p.deinit(); + var frame = std.heap.ArenaAllocator.init(gpa); + defer frame.deinit(); + _ = try p.render(frame.allocator()); + var out: std.ArrayList(u8) = .empty; + defer out.deinit(gpa); + _ = try ptyWritesByPane(p, &out); + p.view().active = 0; + // Asking while its pane has the keyboard: told at once that it has. + p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[?1004h" } }); + try std.testing.expectEqualStrings("0:\x1b[I\n", try ptyWritesByPane(p, &out)); + p.update(.{ .output = .{ .pane = 1, .bytes = "\x1b[?1004h" } }); + try std.testing.expectEqualStrings("", try ptyWritesByPane(p, &out)); + // A click into the other pane moves the keyboard: out of one, into the + // other. + const r = p.view().rects[1]; + const at_col = r.x + config.GUTTER + 2; + const at_row = p.bodyTop(p.panes[1].?, r) + 1; + p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = at_col, .row = at_row } }); + p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = at_col, .row = at_row } }); + try std.testing.expectEqual(@as(usize, 1), p.view().active); + try std.testing.expectEqualStrings("0:\x1b[O\n1:\x1b[I\n", try ptyWritesByPane(p, &out)); + // The window: away and back. + p.update(.{ .focus = false }); + try std.testing.expectEqualStrings("1:\x1b[O\n", try ptyWritesByPane(p, &out)); + p.update(.{ .focus = false }); + try std.testing.expectEqualStrings("", try ptyWritesByPane(p, &out)); + p.update(.{ .focus = true }); + try std.testing.expectEqualStrings("1:\x1b[I\n", try ptyWritesByPane(p, &out)); + // A program that did not ask hears nothing: pane 2. + p.view().active = 2; + p.update(.tick); + try std.testing.expectEqualStrings("1:\x1b[O\n", try ptyWritesByPane(p, &out)); +} + +test "a terminal's kitty graphics image is drawn where its program placed it, and a delete takes it away" { + if (comptime !images_enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .shells = 1 }); + defer p.deinit(); + p.view().frontend.native_images = true; + p.view().active = 0; + p.update(.{ .resize = .{ .cols = 80, .rows = 24, .cell_pixels = .{ .w = 10, .h = 20 } } }); + var frame = std.heap.ArenaAllocator.init(gpa); + defer frame.deinit(); + const pane = p.panes[0].?; + pane.body.mode = .tty; + p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[3;5H" ++ kittyTransmit("c=2,r=1") } }); + var s = try p.render(frame.allocator()); + const places = imagePlaces(s); + try std.testing.expectEqual(@as(usize, 1), places.len); + const place = places[0]; + const r = p.view().rects[0]; + const img = pane.terminal.?.vt.screens.active.kitty_images.imageById(7).?; + try std.testing.expectEqual(pardes.Placement.keyOf(pane.serial, 7, @truncate(img.generation)), place.key); + try std.testing.expectEqual(pardes.RectCell{ .x = r.x + config.GUTTER + 4, .y = p.bodyTop(pane, r) + 2, .w = 2, .h = 1 }, place.dst); + try std.testing.expectEqual(@as(u32, 4), place.raster.w); + try std.testing.expectEqual(@as(u32, 2), place.raster.h); + try std.testing.expectEqualSlices(u8, &.{ 255, 0, 0, 255 }, place.raster.bytes()[0..4]); + try std.testing.expectEqual(pardes.RectPx{ .x = 0, .y = 0, .w = 4, .h = 2 }, place.src); + try std.testing.expectEqual(pardes.RectPx{ .x = 0, .y = 0, .w = 20, .h = 20 }, place.at.?); + + // Scrolled up by the shell's output, it goes up with the text. + p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[24;1H\n\n" } }); + _ = frame.reset(.retain_capacity); + s = try p.render(frame.allocator()); + try std.testing.expectEqual(p.bodyTop(pane, r), imagePlaces(s)[0].dst.y); + p.update(.{ .output = .{ .pane = 0, .bytes = "\n" } }); + _ = frame.reset(.retain_capacity); + s = try p.render(frame.allocator()); + try std.testing.expectEqual(@as(usize, 0), imagePlaces(s).len); + + p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[1;1H" ++ kittyTransmit("c=2,r=1") } }); + _ = frame.reset(.retain_capacity); + s = try p.render(frame.allocator()); + try std.testing.expectEqual(@as(usize, 1), imagePlaces(s).len); + p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b_Ga=d,d=A\x1b\\" } }); + _ = frame.reset(.retain_capacity); + s = try p.render(frame.allocator()); + try std.testing.expectEqual(@as(usize, 0), imagePlaces(s).len); +} + +test "ED 3 takes away a kitty placement whose row it erased: it never comes back at the top" { + if (comptime !images_enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .shells = 1 }); + defer p.deinit(); + p.view().frontend.native_images = true; + p.update(.{ .resize = .{ .cols = 80, .rows = 24, .cell_pixels = .{ .w = 10, .h = 20 } } }); + var frame = std.heap.ArenaAllocator.init(gpa); + defer frame.deinit(); + const pane = p.panes[0].?; + pane.body.mode = .tty; + p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[1;1H" ++ kittyTransmit("c=2,r=1") } }); + const storage = &pane.terminal.?.vt.screens.active.kitty_images; + try std.testing.expectEqual(@as(usize, 1), storage.placements.count()); + // Scrolled into the history, it is not on screen; `clear` (ED 2 then + // ED 3) erases that history and the placement with it. + p.update(.{ .output = .{ .pane = 0, .bytes = &lineBreaks(40) } }); + var s = try p.render(frame.allocator()); + try std.testing.expectEqual(@as(usize, 0), imagePlaces(s).len); + p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[H\x1b[2J\x1b[3J" } }); + try std.testing.expectEqual(@as(usize, 0), storage.placements.count()); + _ = frame.reset(.retain_capacity); + s = try p.render(frame.allocator()); + try std.testing.expectEqual(@as(usize, 0), imagePlaces(s).len); +} + +test "a finished command's output erased by a later ED 3 is not called whole, and no rows stand in for it" { + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 6 }); + defer p.deinit(); + const pane = p.panes[0].?; + const prompt = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07"; + p.update(.{ .output = .{ .pane = 0, .bytes = prompt ++ "\r\n\x1b]133;C;aid=pardes\x07printed\r\n\x1b]133;D;0;aid=pardes\x07" ++ prompt } }); + const before = try commandOutput(pane, gpa, 100); + defer gpa.free(before.text); + try std.testing.expect(before.whole); + try std.testing.expect(std.mem.startsWith(u8, before.text, "printed")); + // The command's rows go into the history, and ED 3 erases them: its + // pins are `garbage` now (ghostty 1.4), not moved to the screen's top, + // where another program's text would be read as its output. + p.update(.{ .output = .{ .pane = 0, .bytes = &lineBreaks(20) } }); + p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[3J" } }); + const after = try commandOutput(pane, gpa, 100); + defer gpa.free(after.text); + try std.testing.expect(!after.whole); + try std.testing.expectEqualStrings("", after.text); +} + +test "a selection whose row ED 3 erased goes; one on the screen stays" { + const gpa = std.testing.allocator; + for ([_]bool{ true, false }) |in_history| { + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 6 }); + defer p.deinit(); + const pane = p.panes[0].?; + pane.body.mode = .tty; + p.update(.{ .output = .{ .pane = 0, .bytes = &lineBreaks(20) } }); + pane.body.mode = .normal; + const pages = &pane.terminal.?.vt.screens.active.pages; + const top: i32 = @intCast(pages.total_rows - pane.rows); + const row: i32 = if (in_history) top - 3 else top + 2; + pane.body.vsel = .{ .active = true, .row = row, .col = 1 }; + p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[3J" } }); + try std.testing.expectEqual(!in_history, pane.body.vsel.active); + if (!in_history) try std.testing.expectEqual(@as(i32, 2), pane.body.vsel.row); + } +} + +test "a virtual kitty placement shows through its placeholder cells, which draw no text" { + if (comptime !images_enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .shells = 1 }); + defer p.deinit(); + p.view().frontend.native_images = true; + p.update(.{ .resize = .{ .cols = 80, .rows = 24, .cell_pixels = .{ .w = 10, .h = 20 } } }); + var frame = std.heap.ArenaAllocator.init(gpa); + defer frame.deinit(); + const pane = p.panes[0].?; + pane.body.mode = .tty; + // yazi's way: a virtual placement two cells by one, then the two + // placeholders, the image's id in their colour, row and column in + // their diacritics. + p.update(.{ .output = .{ .pane = 0, .bytes = kittyTransmit("U=1,c=2,r=1,q=2") ++ + "\x1b[2;3H\x1b[38;2;0;0;7m\u{10EEEE}\u{0305}\u{0305}\u{10EEEE}\u{0305}\u{030D}\x1b[39mX" } }); + const s = try p.render(frame.allocator()); + const places = imagePlaces(s); + try std.testing.expectEqual(@as(usize, 1), places.len); + const r = p.view().rects[0]; + const tx = r.x + config.GUTTER; + const y = p.bodyTop(pane, r) + 1; + try std.testing.expectEqual(pardes.RectCell{ .x = tx + 2, .y = y, .w = 2, .h = 1 }, places[0].dst); + try std.testing.expectEqualStrings(" ", s.at(tx + 2, y).grapheme()); + try std.testing.expectEqualStrings(" ", s.at(tx + 3, y).grapheme()); + try std.testing.expectEqualStrings("X", s.at(tx + 4, y).grapheme()); +} + +test "a terminal's image is a placement whatever the frontend draws, and the Petscii builtin asks for it as glyph art" { + if (comptime !images_enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .shells = 1 }); + defer p.deinit(); + p.update(.{ .resize = .{ .cols = 80, .rows = 24, .cell_pixels = .{ .w = 10, .h = 20 } } }); + var frame = std.heap.ArenaAllocator.init(gpa); + defer frame.deinit(); + const pane = p.panes[0].?; + pane.body.mode = .tty; + p.update(.{ .output = .{ .pane = 0, .bytes = kittyTransmit("c=2,r=1") } }); + const r = p.view().rects[0]; + const tx = r.x + config.GUTTER; + const y = p.bodyTop(pane, r); + + // A frontend that draws no pixels (a tty under no kitty graphics) is + // handed the picture all the same, and the cells under it are its. + p.view().frontend.native_images = false; + var s = try p.render(frame.allocator()); + try std.testing.expectEqual(@as(usize, 1), imagePlaces(s).len); + try std.testing.expect(!imagePlaces(s)[0].art.glyph); + try std.testing.expectEqualStrings(" ", s.at(tx, y).grapheme()); + try std.testing.expectEqualStrings(" ", s.at(tx + 1, y).grapheme()); + // The tag says what that frontend draws, glyph art, as an image pane's does. + try std.testing.expect(std.mem.indexOf(u8, try tagline.tagPrefix(p, pane), " petscii:on") != null); + + // The builtin flips the pane to glyph art and back. + p.view().frontend.native_images = true; + try std.testing.expect(p.executeBuiltinLine(0, "Petscii")); + _ = frame.reset(.retain_capacity); + s = try p.render(frame.allocator()); + try std.testing.expectEqual(@as(usize, 1), imagePlaces(s).len); + try std.testing.expect(imagePlaces(s)[0].art.glyph); + try std.testing.expect(std.mem.indexOf(u8, try tagline.tagPrefix(p, pane), " petscii:on") != null); + // Dumped and restored, it still asks for glyph art. + try pardes.dump.dumpState(p); + const restored = try pardes.dump.restore(p, p.dump_out.?, "/tmp/pardes.dump.zon"); + defer restored.deinit(); + try std.testing.expectEqual(@as(?bool, true), petscii(restored.panes[0].?)); + try std.testing.expect(p.executeBuiltinLine(0, "Petscii")); + _ = frame.reset(.retain_capacity); + s = try p.render(frame.allocator()); + try std.testing.expect(!imagePlaces(s)[0].art.glyph); +} + +test "a new terminal draws its program's images as pixels, and one without images says nothing of them" { + if (comptime !images_enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .shells = 1 }); + defer p.deinit(); + try std.testing.expectEqual(false, petscii(p.panes[0].?).?); + // A pane without images says nothing of them. + try std.testing.expect(std.mem.indexOf(u8, try tagline.tagPrefix(p, p.panes[0].?), "petscii") == null); + const id = p.freeSlot().?; + const fresh = try p.newShell(id, ""); + try std.testing.expectEqual(false, petscii(fresh).?); +} + +test "a program asking the cell size in pixels (CSI 16 t) is told the shell's" { + if (comptime !images_enabled) return error.SkipZigTest; + const gpa = std.testing.allocator; + const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .shells = 1 }); + defer p.deinit(); + p.update(.{ .resize = .{ .cols = 80, .rows = 24, .cell_pixels = .{ .w = 10, .h = 20 } } }); + var out: std.ArrayList(u8) = .empty; + defer out.deinit(gpa); + _ = try ptyWritesByPane(p, &out); + p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[16t" } }); + try std.testing.expectEqualStrings("0:\x1b[6;20;10t\n", try ptyWritesByPane(p, &out)); +} + +test "colors.xterm is the palette the emulator starts from, all 256" { + for (pardes.colors.xterm, ghostty_vt.color.default) |ours, its| try std.testing.expectEqual(ours, [3]u8{ its.r, its.g, its.b }); +} -- cgit v1.3