diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-19 22:15:16 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-19 22:26:54 -0300 |
| commit | ba10cc4ff30d08d27ad35576df04a03282b6521b (patch) | |
| tree | e367489aa56f36d71485ce07b4022b512139a94c | |
| parent | 98b2bc05cd7b6c78952600029939bbc44c5fc56b (diff) | |
| download | pardes-ba10cc4ff30d08d27ad35576df04a03282b6521b.tar.gz pardes-ba10cc4ff30d08d27ad35576df04a03282b6521b.zip | |
Term: the two things a speculation writes into a terminal and no rollback puts back move into the pane, which the copy holds and the digest covers: the cell its program last heard the mouse at (Pane.term_mouse, handed to Term.mouse and Term.wheel, which ghostty's encoder reads and writes) and its kitty graphics as glyph art (Pane.term_petscii, which the Petscii builtin flips and the images' draw takes in ImagesAt): the sizing's table (reports/pardes-term-bytes.md, section 1; the brief's step 1c); Parts loses both (a dump of an older build carries them and they are passed over by name: a terminal's Petscii chosen before an Upgrade is off after it), Ops loses petscii and togglePetscii; term-test 17/17, core-test 848/991 (143 skipped), shell-test 1027/1030
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
| -rw-r--r-- | abi/term.zig | 11 | ||||
| -rw-r--r-- | core/Term.zig | 21 | ||||
| -rw-r--r-- | core/ansi.zig | 1 | ||||
| -rw-r--r-- | core/builtins.zig | 4 | ||||
| -rw-r--r-- | core/mouse.zig | 10 | ||||
| -rw-r--r-- | core/panes.zig | 6 | ||||
| -rw-r--r-- | core/tagline.zig | 3 | ||||
| -rw-r--r-- | core/term/TerminalImages.zig | 9 | ||||
| -rw-r--r-- | core/term/term.zig | 2 | ||||
| -rw-r--r-- | core/term/terminal.zig | 35 | ||||
| -rw-r--r-- | test/terminal.zig | 7 |
11 files changed, 42 insertions, 67 deletions
diff --git a/abi/term.zig b/abi/term.zig index 68434e8e..40f9e4f3 100644 --- a/abi/term.zig +++ b/abi/term.zig @@ -231,6 +231,9 @@ pub const ImagesAt = struct { now_ms: u64, cell_w: u32, cell_h: u32, + /// Its images as glyph art even where pixels could be drawn (the + /// pane's Petscii). + petscii: bool = false, }; /// What a predicted echo reads of a cell: its codepoint, whether the shell @@ -286,8 +289,6 @@ pub const Applied = struct { seq: u32, extra: []const u8 }; pub const Parts = struct { reply: []const u8 = "", reply_dropped: u32 = 0, - mouse_cell: ?Coordinate = null, - petscii: bool = false, /// The view's top row, counted from the top of the history. view: usize = 0, phase: Prompt.Phase = .none, @@ -363,8 +364,8 @@ pub const Ops = struct { // ---- input ---- key: *const fn (s: *State, k: plain.Key, out: *std.Io.Writer) error{WriteFailed}!void, - mouse: *const fn (s: *State, buf: *[64]u8, action: MouseAction, button: ?MouseButton, mods: MouseMods, col: i32, row: i32, any_pressed: bool) []const u8, - wheel: *const fn (s: *State, buf: *[64]u8, turn: MouseButton, col: u16, row: u16, mods: MouseMods) ?[]const u8, + mouse: *const fn (s: *State, last: *?Parts.Coordinate, buf: *[64]u8, action: MouseAction, button: ?MouseButton, mods: MouseMods, col: i32, row: i32, any_pressed: bool) []const u8, + wheel: *const fn (s: *State, last: *?Parts.Coordinate, buf: *[64]u8, turn: MouseButton, col: u16, row: u16, mods: MouseMods) ?[]const u8, paste: *const fn (s: ?*const State, text: []u8) [3][]const u8, modes: *const fn (s: ?*const State) Modes, promptMoves: *const fn (s: *State, row: i32, col: i32) ?Moves, @@ -406,8 +407,6 @@ pub const Ops = struct { /// what it keeps to draw them is its own allocator's), and their /// placeholder cells blanked in `frame` (`frame_cols` wide). images: *const fn (s: *State, gpa: std.mem.Allocator, at: ImagesAt, out: *std.ArrayList(plain.Placement), frame: []plain.Cell, frame_cols: u16) void, - petscii: *const fn (s: ?*const State) ?bool, - togglePetscii: *const fn (s: *State) void, hasImages: *const fn (s: ?*const State) bool, // ---- its replicas ---- diff --git a/core/Term.zig b/core/Term.zig index 5025ba2b..82d5dad7 100644 --- a/core/Term.zig +++ b/core/Term.zig @@ -30,6 +30,8 @@ pub const Moves = iface.Moves; pub const MouseMods = iface.MouseMods; pub const MouseButton = iface.MouseButton; pub const MouseAction = iface.MouseAction; +/// A grid cell (the one a program last heard the mouse at: Pane.term_mouse). +pub const Coordinate = iface.Parts.Coordinate; pub const Page = iface.Page; pub const Span = iface.Span; pub const Ink = iface.Ink; @@ -212,13 +214,13 @@ pub fn key(s: *State, k: plain.Key, out: *std.Io.Writer) !void { /// One mouse event for its program, as the mode and format it set ask for /// it; empty when the program does not hear of it. -pub fn mouse(s: *State, buf: *[64]u8, action: MouseAction, button: ?MouseButton, mods: MouseMods, col: i32, row: i32, any_pressed: bool) []const u8 { - return the().mouse(s.impl, buf, action, button, mods, col, row, any_pressed); +pub fn mouse(s: *State, last: *?Coordinate, buf: *[64]u8, action: MouseAction, button: ?MouseButton, mods: MouseMods, col: i32, row: i32, any_pressed: bool) []const u8 { + return the().mouse(s.impl, last, buf, action, button, mods, col, row, any_pressed); } /// A wheel turn for its program; null when the wheel is not the program's. -pub fn wheel(s: *State, buf: *[64]u8, turn: MouseButton, col: u16, row: u16, mods: MouseMods) ?[]const u8 { - return the().wheel(s.impl, buf, turn, col, row, mods); +pub fn wheel(s: *State, last: *?Coordinate, buf: *[64]u8, turn: MouseButton, col: u16, row: u16, mods: MouseMods) ?[]const u8 { + return the().wheel(s.impl, last, buf, turn, col, row, mods); } /// A paste as xterm makes one: three parts, the text between the brackets @@ -362,17 +364,6 @@ pub fn images(s: *State, gpa: std.mem.Allocator, at: ImagesAt, out: *std.ArrayLi the().images(s.impl, gpa, at, out, frame, frame_cols); } -/// The Petscii builtin on it: its kitty graphics as glyph art, or back to -/// pixels; null for none. -pub fn petscii(s: ?*const State) ?bool { - const o = ops orelse return null; - return o.petscii(if (s) |state| state.impl else null); -} - -pub fn togglePetscii(s: *State) void { - the().togglePetscii(s.impl); -} - /// Whether the screen showing holds any kitty graphics image. pub fn hasImages(s: ?*const State) bool { const o = ops orelse return false; diff --git a/core/ansi.zig b/core/ansi.zig index 6c23fee6..5f522536 100644 --- a/core/ansi.zig +++ b/core/ansi.zig @@ -228,6 +228,7 @@ pub fn images(p: *Pardes, s: *Surface, pane: *Pane, id: usize, tx: u16, tw: u16, .cols = @min(tw, pane.cols), .rows_of = rows_of[0..vp_rows], .tty_mode = pane.body.mode == .tty, + .petscii = pane.term_petscii, .now_ms = p.clock.stepped_ns / std.time.ns_per_ms, .cell_w = @max(1, p.view().cell_pixels.w), .cell_h = @max(1, p.view().cell_pixels.h), diff --git a/core/builtins.zig b/core/builtins.zig index 6f37e86d..66b22dcc 100644 --- a/core/builtins.zig +++ b/core/builtins.zig @@ -749,13 +749,13 @@ pub const Petscii = struct { pub const takes_arg = true; pub fn run(c: Ctx) void { // `on|off` sets it, as the tag's `petscii:on` reads; none flips it. - const now = if (c.pane.addon) |k| k.art.glyph else panes.Term.petscii(c.pane.terminal) orelse false; + const now = if (c.pane.addon) |k| k.art.glyph else c.pane.terminal != null and c.pane.term_petscii; if (c.arg != null) { const want = stateArg(c, OnOff, "Petscii", "on or off") orelse return; if ((want == .on) == now) return; } if (c.pane.addon) |k| k.art.glyph = !k.art.glyph; - if (c.pane.terminal) |state| panes.Term.togglePetscii(state); + if (c.pane.terminal != null) c.pane.term_petscii = !c.pane.term_petscii; } }; diff --git a/core/mouse.zig b/core/mouse.zig index 3d7c5af9..5e240930 100644 --- a/core/mouse.zig +++ b/core/mouse.zig @@ -184,7 +184,7 @@ fn programWheel(p: *Pardes, id: usize, m: Mouse, col: u16, row: u16) bool { .wheel_left => .wheel_left, else => .wheel_right, }; - const bytes = panes.Term.wheel(pane.terminal.?, &buf, wheel, gcol, grow, .{ .alt = m.alt, .ctrl = m.ctrl }) orelse return true; + const bytes = panes.Term.wheel(pane.terminal.?, &pane.term_mouse, &buf, wheel, gcol, grow, .{ .alt = m.alt, .ctrl = m.ctrl }) orelse return true; for (0..@intCast(config.wheel_rows)) |_| p.emitWrite(id, bytes); return true; } @@ -298,7 +298,7 @@ fn programMouse(p: *Pardes, m: Mouse) bool { }; if (m.kind == .press) grab.held |= bit; if (m.kind == .release) grab.held &= ~bit; - const bytes = panes.Term.mouse(pane.terminal.?, &buf, action, button, mods, col, row, grab.held != 0); + const bytes = panes.Term.mouse(pane.terminal.?, &pane.term_mouse, &buf, action, button, mods, col, row, grab.held != 0); if (bytes.len > 0) p.emitWrite(grab.id, bytes); if (grab.held == 0) v.mouse_grab = null; return true; @@ -322,7 +322,7 @@ fn programMouse(p: *Pardes, m: Mouse) bool { pane.focus = .body; pane.closePrompt(); v.mouse_grab = .{ .id = id, .serial = pane.serial, .held = bit, .unshift = shifted_to_program }; - const bytes = panes.Term.mouse(pane.terminal.?, &buf, .press, button, mods, cell.col, cell.row, true); + const bytes = panes.Term.mouse(pane.terminal.?, &pane.term_mouse, &buf, .press, button, mods, cell.col, cell.row, true); if (bytes.len > 0) p.emitWrite(id, bytes); return true; }, @@ -330,7 +330,7 @@ fn programMouse(p: *Pardes, m: Mouse) bool { // pardes's. .release, .drag => return false, .motion => { - const bytes = panes.Term.mouse(pane.terminal.?, &buf, .motion, null, mods, cell.col, cell.row, false); + const bytes = panes.Term.mouse(pane.terminal.?, &pane.term_mouse, &buf, .motion, null, mods, cell.col, cell.row, false); if (bytes.len > 0) p.emitWrite(id, bytes); // Under 1003 the move is the program's; below that pardes // hovers as ever. @@ -432,7 +432,7 @@ fn chordCutPaste(p: *Pardes, cut: bool) edit.Error!void { const row = @max(0, pane.sel[sel_slot].r1 - @as(i32, pane.tag_rows)); var mb: [64]u8 = undefined; for ([_]panes.Term.MouseAction{ .press, .release }) |action| { - const bytes = panes.Term.mouse(pane.terminal.?, &mb, action, .left, .{}, col, row, action == .press); + const bytes = panes.Term.mouse(pane.terminal.?, &pane.term_mouse, &mb, action, .left, .{}, col, row, action == .press); if (bytes.len > 0) p.emitWrite(s.id, bytes); } } diff --git a/core/panes.zig b/core/panes.zig index b928917a..56a8e82a 100644 --- a/core/panes.zig +++ b/core/panes.zig @@ -125,6 +125,12 @@ pub const Pane = struct { pub const Focus = enum { body, tag }; pub const SessionView = struct { wrap: bool = true, layered: bool = true, body_h: u32 = 0, tagline_h: u32 = 0, rect_h: u16 = 0 }; terminal: ?*Term.State = null, + /// Its terminal's, kept by the pane, which a rollback puts back, not by + /// the emulator, which none does (a speculation writes them): the grid + /// cell its program last heard the mouse at (motion is told once a + /// cell), and its kitty graphics drawn as glyph art (Petscii). + term_mouse: ?Term.Coordinate = null, + term_petscii: bool = false, gpa: std.mem.Allocator, serial: u32 = 0, /// What the control filesystem keeps of this pane -- addr and limit, the diff --git a/core/tagline.zig b/core/tagline.zig index 456a6b6e..660ecbf9 100644 --- a/core/tagline.zig +++ b/core/tagline.zig @@ -157,7 +157,8 @@ pub fn pathPrefix(p: *const Pardes, arena: std.mem.Allocator, pane: *Pane) ![]co if (pane.file) |f| return if (pane.prompt == .name) pane.input.own orelse f.path else f.path; // A terminal showing kitty graphics says how it draws them, as an image // pane does, and so does one drawing them as glyph art by choice. - if (panes.Term.petscii(pane.terminal)) |chosen| { + if (pane.terminal != null) { + const chosen = pane.term_petscii; const on = chosen or !p.view().frontend.native_images; if (chosen or panes.Term.hasImages(pane.terminal)) return std.fmt.allocPrint(arena, "{s} petscii:{s}", .{ (try shownDir(arena, pane)), if (on) "on" else "off" }); diff --git a/core/term/TerminalImages.zig b/core/term/TerminalImages.zig index feba5ddc..d0d364a1 100644 --- a/core/term/TerminalImages.zig +++ b/core/term/TerminalImages.zig @@ -24,9 +24,6 @@ const image_decode_pixels: usize = 64 * 1024 * 1024; /// 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. /// The emulator's own allocator's, as the emulator is (`draw`'s `own`): @@ -191,7 +188,7 @@ pub fn draw( const bottom = pages.getBottomRight(.viewport) orelse return; const cw: u32 = @max(1, where.cell_w); const ch: u32 = @max(1, where.cell_h); - const ctx: Ctx = .{ .ti = ti, .own = own, .gpa = gpa, .out = out, .pane = where.pane, .serial = where.serial, .tx = where.tx, .body_y = where.body_y, .cols = where.cols, .rows_of = rows_of, .cw = cw, .ch = ch }; + const ctx: Ctx = .{ .ti = ti, .own = own, .gpa = gpa, .out = out, .pane = where.pane, .serial = where.serial, .tx = where.tx, .body_y = where.body_y, .cols = where.cols, .rows_of = rows_of, .cw = cw, .ch = ch, .glyph = where.petscii }; // 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 = out.items.len; @@ -283,6 +280,8 @@ const Ctx = struct { rows_of: []const ?u16, cw: u32, ch: u32, + /// Its images as glyph art (the pane's Petscii). + glyph: bool, /// 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. @@ -323,7 +322,7 @@ const Ctx = struct { }, .key = plain.Placement.keyOf(ctx.serial, img.id, @truncate(img.generation)), .z = z, - .art = .{ .glyph = ctx.ti.petscii }, + .art = .{ .glyph = ctx.glyph }, }) catch {}; } } diff --git a/core/term/term.zig b/core/term/term.zig index 6f62b827..4bb60079 100644 --- a/core/term/term.zig +++ b/core/term/term.zig @@ -63,8 +63,6 @@ pub const ops: iface.Ops = .{ .spanColor = terminal_.spanColor, .paint = cast(iface.Ops, "paint", terminal_.paint), .images = cast(iface.Ops, "images", terminal_.images), - .petscii = cast(iface.Ops, "petscii", terminal_.petscii), - .togglePetscii = cast(iface.Ops, "togglePetscii", terminal_.togglePetscii), .hasImages = cast(iface.Ops, "hasImages", terminal_.hasImages), .encoder = cast(iface.Ops, "encoder", terminal_.encoder), .encoderFree = cast(iface.Ops, "encoderFree", terminal_.encoderFree), diff --git a/core/term/terminal.zig b/core/term/terminal.zig index a4120f5a..4de389bd 100644 --- a/core/term/terminal.zig +++ b/core/term/terminal.zig @@ -181,9 +181,6 @@ pub const State = struct { 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): @@ -257,8 +254,6 @@ pub fn parts(s: *const State) Term.Parts { return .{ .reply = s.reply[0..s.reply_len], .reply_dropped = s.reply_dropped, - .mouse_cell = if (s.mouse_cell) |c| .{ .x = c.x, .y = c.y } else null, - .petscii = if (comptime images_enabled) s.images.petscii else false, .view = s.vt.screens.active.pages.scrollbar().offset, .phase = h.phase, .started = h.started, @@ -370,8 +365,6 @@ fn install(out: *State, continuation: ghostty_vt.snapshot.Continuation, saved: * @memcpy(out.reply[0..reply.len], reply); out.reply_len = @intCast(reply.len); out.reply_dropped = saved.reply_dropped; - out.mouse_cell = coordinate(saved.mouse_cell); - out.images = if (images_enabled) .{ .petscii = saved.petscii } else {}; const h = &out.stream.handler; h.phase = saved.phase; h.started = saved.started; @@ -674,10 +667,13 @@ pub fn paste(state: ?*const State, text: []u8) [3][]const u8 { /// 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 mouse(state: *State, buf: *[64]u8, action: Term.MouseAction, button: ?Term.MouseButton, mods: Term.MouseMods, col: i32, row: i32, any_pressed: bool) []const u8 { +/// changed from `last`, the cell it last heard of (its pane's, which a +/// rollback puts back). Empty when the program does not hear of it. +pub fn mouse(state: *State, last: *?Term.Parts.Coordinate, buf: *[64]u8, action: Term.MouseAction, button: ?Term.MouseButton, mods: Term.MouseMods, col: i32, row: i32, any_pressed: bool) []const u8 { const input = ghostty_vt.input; var writer = std.Io.Writer.fixed(buf); + var cell = coordinate(last.*); + defer last.* = if (cell) |c| .{ .x = c.x, .y = c.y } else null; var opts: input.MouseEncodeOptions = .{ .event = state.vt.flags.mouse_event, .format = state.vt.flags.mouse_format, @@ -687,7 +683,7 @@ pub fn mouse(state: *State, buf: *[64]u8, action: Term.MouseAction, button: ?Ter .padding = .{}, }, .any_button_pressed = any_pressed, - .last_cell = &state.mouse_cell, + .last_cell = &cell, }; // A release is always reported; the rest only over the grid, or held. if (action == .release) opts.any_button_pressed = true; @@ -716,7 +712,7 @@ pub fn mouse(state: *State, buf: *[64]u8, action: Term.MouseAction, button: ?Ter /// 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 (`Modes.wheel`). -pub fn wheel(state: *State, buf: *[64]u8, turn: Term.MouseButton, col: u16, row: u16, mods: Term.MouseMods) ?[]const u8 { +pub fn wheel(state: *State, last: *?Term.Parts.Coordinate, buf: *[64]u8, turn: Term.MouseButton, col: u16, row: u16, mods: Term.MouseMods) ?[]const u8 { const now = modes(state); if (!now.wheel) return null; const m = &state.vt.modes; @@ -725,7 +721,7 @@ pub fn wheel(state: *State, buf: *[64]u8, turn: Term.MouseButton, col: u16, row: .wheel_down => if (m.get(.cursor_keys)) "\x1bOB" else "\x1b[B", else => null, }; - return mouse(state, buf, .press, turn, mods, col, row, false); + return mouse(state, last, buf, .press, turn, mods, col, row, false); } /// The whole scrollback as plain text, `gpa`-owned: what a terminal's body @@ -1536,21 +1532,6 @@ pub fn due(state: *const State) ?u64 { 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(state: *State) void { - if (comptime !images_enabled) 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(maybe: ?*const State) ?bool { - if (comptime !images_enabled) return null; - const state = maybe orelse return null; - return state.images.petscii; -} - /// Whether the screen showing holds any kitty graphics image. pub fn hasImages(maybe: ?*const State) bool { if (comptime !images_enabled) return false; diff --git a/test/terminal.zig b/test/terminal.zig index e6c530d5..b7b9cd33 100644 --- a/test/terminal.zig +++ b/test/terminal.zig @@ -34,7 +34,6 @@ const prompt = Term.prompt; const scroll = Term.scroll; const scrollback = Term.scrollback; const commandOutput = Term.commandOutput; -const petscii = Term.petscii; const filterOf = emu.filterOf; const filterPalette = emu.filterPalette; const keyedRgb = emu.keyedRgb; @@ -892,7 +891,7 @@ test "a terminal's image is a placement whatever the frontend draws, and the Pet 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].?.terminal)); + try std.testing.expect(restored.panes[0].?.term_petscii); try std.testing.expect(p.executeBuiltinLine(0, "Petscii")); _ = frame.reset(.retain_capacity); s = try p.render(frame.allocator()); @@ -904,12 +903,12 @@ test "a new terminal draws its program's images as pixels, and one without image 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].?.terminal).?); + try std.testing.expect(!p.panes[0].?.term_petscii); // 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.terminal).?); + try std.testing.expect(!fresh.term_petscii); } test "a program asking the cell size in pixels (CSI 16 t) is told the shell's" { |
