diff options
| -rw-r--r-- | abi/term.zig | 3 | ||||
| -rw-r--r-- | core/Term.zig | 2 | ||||
| -rw-r--r-- | core/draw.zig | 3 | ||||
| -rw-r--r-- | core/surface.zig | 4 | ||||
| -rw-r--r-- | core/term/terminal.zig | 1 | ||||
| -rw-r--r-- | dev/tui/tui.zig | 126 |
6 files changed, 135 insertions, 4 deletions
diff --git a/abi/term.zig b/abi/term.zig index 2fb7c260..4dfd523a 100644 --- a/abi/term.zig +++ b/abi/term.zig @@ -59,6 +59,9 @@ pub const Modes = struct { bracketed_paste: bool = false, /// It has the alternate screen (1049). alternate: bool = false, + /// It is drawing a frame (synchronized output, 2026): what its screen + /// shows now is half of one (draw.zig's `Region.held`). + sync: bool = false, }; /// Where a shell is in its command cycle, as pardes's own OSC 133 marks diff --git a/core/Term.zig b/core/Term.zig index 2f0a0f3f..32b98cd9 100644 --- a/core/Term.zig +++ b/core/Term.zig @@ -425,7 +425,7 @@ pub const calls = [_]struct { []const u8, []const u8, []const u8 }{ .{ "replies", "Core.zig:1", "WRITE: a step drains the reply buffer (in the digest), counted when it held any (the feed's own unit, which filled it); only the session writes it to the pty (pty_write is off the tape)" }, // ---- reads by a step: in the digest ---- .{ "grid", "Core.zig:3 ansi.zig:3 body_layer.zig:3 draw.zig:2 dump.zig:4 echo.zig:8 exec.zig:2 layout.zig:2 panes.zig:6 replica.zig:2", "READ: the size, the history's length, the cursor: in the digest (a view's place is its pane's; ansi.zig, body_layer.zig, draw.zig, echo.zig: a draw's)" }, - .{ "modes", "Core.zig:5 draw.zig:1 dump.zig:2 echo.zig:3 exec.zig:3 mouse.zig:4 ninep/pty.zig:3 panes.zig:2 replica.zig:1", "READ: the program's modes (mouse, focus, bracketed paste, the alternate screen): in the digest (draw.zig, echo.zig: a draw's; panes.zig: a view scrolls no history on the alternate screen)" }, + .{ "modes", "Core.zig:5 draw.zig:2 dump.zig:2 echo.zig:3 exec.zig:3 mouse.zig:4 ninep/pty.zig:3 panes.zig:2 replica.zig:1", "READ: the program's modes (mouse, focus, bracketed paste, the alternate screen, a frame under way): in the digest (draw.zig, echo.zig: a draw's; panes.zig: a view scrolls no history on the alternate screen)" }, .{ "prompt", "Core.zig:1 builtins.zig:1 echo.zig:1 exec.zig:3 ninep/pty.zig:1 panes.zig:1", "READ: the OSC 133 cycle: in the digest (`marksOf`); Core.zig's and ninep/pty.zig's also taped (`.prompt`, each side's compared)" }, .{ "promptPrefix", "body_layer.zig:1 edit.zig:1 exec.zig:1", "READ: a row's prompt cells (`promptCut`): the active area's in the digest; a history row's text is not (below)" }, .{ "hasImages", "replica.zig:3 tagline.zig:1", "READ: whether it holds a kitty image (the tag's prefix): in the digest" }, diff --git a/core/draw.zig b/core/draw.zig index 648808aa..3146df3f 100644 --- a/core/draw.zig +++ b/core/draw.zig @@ -114,7 +114,8 @@ pub fn place(p: *Pardes, s: *Surface) void { // view moved are another text's); none for a terminal or a picture. const doc: u64 = if (pane.file) |f| std.hash.Wyhash.hash(if (f.output != null) f.revision else 0, f.path) else 0; const row: u32 = if (pane.file != null) panes.File.top(p.settings.tab, pane).row else 0; - s.addRegion(.{ .kind = .body, .owner = owner, .serial = pane.serial, .active = active, .line = line, .row = row, .doc = doc, .rect = .{ .x = r.x + config.GUTTER, .y = body_y, .w = r.w - config.GUTTER, .h = body_h } }); + const held = pane.isTerminal() and panes.Term.modes(pane.terminal).sync; + s.addRegion(.{ .kind = .body, .owner = owner, .serial = pane.serial, .active = active, .line = line, .row = row, .doc = doc, .held = held, .rect = .{ .x = r.x + config.GUTTER, .y = body_y, .w = r.w - config.GUTTER, .h = body_h } }); var rail: Region = .{ .kind = .rail, .owner = owner, .serial = pane.serial, .active = active, .rect = .{ .x = r.x, .y = body_y, .w = config.GUTTER, .h = body_h } }; // A kind's rail is its paint's (drawPane); every other pane's is // here. diff --git a/core/surface.zig b/core/surface.zig index 7928c89e..69473bc8 100644 --- a/core/surface.zig +++ b/core/surface.zig @@ -142,6 +142,10 @@ pub const Region = struct { line: i32 = 0, row: u32 = 0, doc: u64 = 0, + /// A terminal's body whose program is mid-frame (synchronized output, + /// 2026): a frontend keeps showing what it showed there until the + /// frame is done, as a terminal holds its picture (tui.zig `Held`). + held: bool = false, /// A body drawn as a strip of pictures (a PDF): how far down the strip /// it shows, in pixels. Its placements are laid at the whole pixel; a /// pixel frontend draws them the fraction higher. diff --git a/core/term/terminal.zig b/core/term/terminal.zig index 1c2310b1..12306957 100644 --- a/core/term/terminal.zig +++ b/core/term/terminal.zig @@ -630,6 +630,7 @@ pub fn modes(state: ?*const State) Term.Modes { .focus = s.vt.modes.get(.focus_event), .bracketed_paste = s.vt.modes.get(.bracketed_paste), .alternate = alternate, + .sync = s.vt.modes.get(.synchronized_output), }; } diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig index 8cb06b05..35fac823 100644 --- a/dev/tui/tui.zig +++ b/dev/tui/tui.zig @@ -831,6 +831,7 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8, defer start.close(); if (opts.pair == null) try screen.enter(try vaxis.Tty.init(io, &tty_buf)); defer screen.glide.deinit(gpa); + defer screen.held.deinit(gpa); defer screen.art.deinit(gpa); var paste_buf: std.Io.Writer.Allocating = .init(gpa); defer paste_buf.deinit(); @@ -942,6 +943,8 @@ const Screen = struct { glide: pardes.glide.Glide = .{}, /// The cursor's jump trail (JumpTrail), on the tty's own clock. trail: JumpTrail = .{}, + /// What it last showed of a terminal mid-frame (synchronized output). + held: Held = .{}, /// `fd` made raw, as vaxis.Tty.init makes /dev/tty: the terminal of a /// forked session is no controlling terminal of its, which /dev/tty is. @@ -1123,6 +1126,7 @@ const Own = struct { }; o.screen.art.deinit(gpa); o.screen.glide.deinit(gpa); + o.screen.held.deinit(gpa); o.frame_arena.deinit(); if (o.kept) |k| _ = libc.close(k.fd); } @@ -1786,7 +1790,7 @@ const Shell = struct { /// The tty's own motions (its glide, a jump's trail) are under way. fn moving(s: *const Shell) bool { const sc = s.term() orelse return false; - return sc.glide.moving() or sc.trail.active; + return sc.glide.moving() or sc.trail.active or sc.held.holding; } /// The tty's own motions draw their frames themselves: the last frame @@ -1842,7 +1846,8 @@ const Shell = struct { // The pictures it shows as glyph art, in a copy of the frame: all // of them where the terminal has no kitty graphics. const cell_h = terminalCellPixels(vx.screen.height_pix, vx.screen.height, 16); - const arted = sc.art.frame(s.gpa, s.frame.allocator(), canonical, vx.caps.kitty_graphics, cell_h) catch canonical; + const kept = sc.held.frame(s.gpa, s.frame.allocator(), canonical, now) catch canonical; + const arted = sc.art.frame(s.gpa, s.frame.allocator(), kept, vx.caps.kitty_graphics, cell_h) catch kept; sc.glide.frame(s.gpa, arted, now, null, null); s.frames.scroll = @round(sc.glide.follow.offset); const glided = glideRows(s.frame.allocator(), arted, &sc.glide) catch arted; @@ -2272,6 +2277,88 @@ fn colortermTruecolor() bool { return std.mem.eql(u8, value, "truecolor") or std.mem.eql(u8, value, "24bit"); } +/// Synchronized output (2026, `Region.held`): a terminal's body whose +/// program is mid-frame keeps the cells this tty last showed there, as a +/// terminal's renderer pauses (ghostty's), a pane at a time (tmux's), and +/// for a second at most (ghostty's sync_reset_ms): a program that never +/// ends its frame is drawn as it is. Drawn as it came, an agent's redraw of +/// a long transcript (many reads of the pty) showed the transcript's start +/// again and then jumped to its end. +const Held = struct { + /// The cells it last showed, its size then, and each body's place. + last: []pardes.Cell = &.{}, + cols: u16 = 0, + rows: u16 = 0, + cursor: @FieldType(pardes.Surface, "cursor") = null, + rects: [pardes.MAX_PANES]?pardes.Rect = @splat(null), + /// When each pane's hold began. + since: [pardes.MAX_PANES]?u64 = @splat(null), + /// One is held: the tty draws a frame a tick until it lets go. + holding: bool = false, + + const max_ns = std.time.ns_per_s; + + fn deinit(h: *Held, gpa: std.mem.Allocator) void { + gpa.free(h.last); + h.* = .{}; + } + + /// `s` with each held body's rect as it was last shown (a copy in + /// `arena`), or `s` itself when none is. + fn frame(h: *Held, gpa: std.mem.Allocator, arena: std.mem.Allocator, s: *const pardes.Surface, now: u64) !*const pardes.Surface { + var copy: ?*pardes.Surface = null; + var rects: [pardes.MAX_PANES]?pardes.Rect = @splat(null); + h.holding = false; + for (s.regionList()) |region| { + if (region.kind != .body or region.owner >= rects.len) continue; + const id = region.owner; + const r = region.rect; + rects[id] = r; + if (!region.held) { + h.since[id] = null; + continue; + } + const began = h.since[id] orelse now; + h.since[id] = began; + // No picture of it to keep: a new size or place shows it as it is. + if (now -| began >= max_ns or h.cols != s.cols or h.rows != s.rows or !std.meta.eql(h.rects[id], r) or + r.x + r.w > s.cols or r.y + r.h > s.rows) continue; + h.holding = true; + const out = copy orelse made: { + const c = try arena.create(pardes.Surface); + c.* = s.*; + c.cells = try arena.dupe(pardes.Cell, s.cells); + copy = c; + break :made c; + }; + for (r.y..r.y + r.h) |y| @memcpy(out.cells[y * s.cols + r.x ..][0..r.w], h.last[y * s.cols + r.x ..][0..r.w]); + if (inside(out.cursor, r) or inside(h.cursor, r)) out.cursor = h.cursor; + } + for (&h.since, rects) |*began, r| if (r == null) { + began.* = null; + }; + const out: *const pardes.Surface = copy orelse s; + if (h.last.len != out.cells.len) { + gpa.free(h.last); + h.last = &.{}; + h.cols = 0; + h.rows = 0; + h.last = try gpa.alloc(pardes.Cell, out.cells.len); + } + @memcpy(h.last, out.cells); + h.cols = out.cols; + h.rows = out.rows; + h.cursor = out.cursor; + h.rects = rects; + return out; + } + + fn inside(cursor: @FieldType(pardes.Surface, "cursor"), r: pardes.Rect) bool { + const c = cursor orelse return false; + return c.x >= r.x and c.x < r.x + r.w and c.y >= r.y and c.y < r.y + r.h; + } +}; + /// The tty's picture of the glide (glide.zig, anim 12): the body's rows /// moved by the offset rounded to whole rows (the position rounded, never /// the step, so a slowing glide's steps come further apart), the rows that @@ -3252,6 +3339,41 @@ test "a fade sends the text it dims, not the blanks beside it" { try std.testing.expect(std.mem.indexOfScalar(u8, sent[1], 'a') != null); } +test "a terminal mid-frame (synchronized output) shows what the tty last showed of it, until the frame ends or a second has gone" { + if (pardes.Term.ops == null) return error.SkipZigTest; // no emulator + const gpa = std.testing.allocator; + const core = try pardes.Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 8 }); + defer core.deinit(); + while (core.nextEffect()) |_| {} + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + var held: Held = .{}; + defer held.deinit(gpa); + // The tty's frame after `bytes` came, at `t`: whether it shows `word`. + const shows = struct { + fn f(c: *pardes.Pardes, h: *Held, a: *std.heap.ArenaAllocator, bytes: []const u8, t: u64, word: []const u8) !bool { + if (bytes.len > 0) c.update(.{ .output = .{ .pane = .at(0), .bytes = bytes } }); + _ = a.reset(.retain_capacity); + const s = try h.frame(std.testing.allocator, a.allocator(), try c.render(a.allocator()), t); + var text: std.ArrayList(u8) = .empty; + for (s.cells) |*cell| try text.appendSlice(a.allocator(), cell.grapheme()); + return std.mem.indexOf(u8, text.items, word) != null; + } + }.f; + const s = std.time.ns_per_s; + try std.testing.expect(try shows(core, &held, &arena, "first frame", s, "first frame")); + // A redraw that came in two reads: the first half is not shown. + try std.testing.expect(try shows(core, &held, &arena, "\x1b[?2026h\x1b[2J\x1b[Hsecond", s + s / 10, "first frame")); + try std.testing.expect(held.holding); + try std.testing.expect(try shows(core, &held, &arena, " frame\x1b[?2026l", s + s / 5, "second frame")); + try std.testing.expect(!held.holding); + // One never ended: drawn as it is after a second. + try std.testing.expect(try shows(core, &held, &arena, "\x1b[?2026h\x1b[2J\x1b[Hthird", 2 * s, "second frame")); + try std.testing.expect(try shows(core, &held, &arena, "", 3 * s - 1, "second frame")); + try std.testing.expect(try shows(core, &held, &arena, "", 3 * s, "third")); + try std.testing.expect(!held.holding); +} + test "the tty draws a glide at whole rows: once a flick or a spin slows, its steps a tick never grow and its one-row steps come further apart, to where the core is" { const gpa = std.testing.allocator; const core = try pardes.Pardes.init(gpa, .{ .cols = 80, .rows = 30, .tty_only = true }); |
