diff options
Diffstat (limited to 'dev/tui/tui.zig')
| -rw-r--r-- | dev/tui/tui.zig | 126 |
1 files changed, 124 insertions, 2 deletions
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 }); |
