From 8f6621f9f70db86d56266a59b56dfb92b324b0d9 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 00:51:06 -0300 Subject: Term: the replica half and the seam's last reaches: screen_state.zig's terminal half is reached through the interface (Term.encode, Term.encodeRows and Term.untrack on the authority, Term.wasRead and Term.clearRead for the rows a step read, Term.apply with the prompt marks riding a delta, Term.applyRows, Term.shown and Term.blank on a replica, Term.Encoder, Track, Peer and Replica its handles), so replica.zig reaches no emulator state; its kind half (a kind's frame as a Shadow, its strips and Blobs, encodeShadow) is the core's and moves, as it is, with its tests, to Kind.zig beside the Slot it shadows; predicted echo is Term.Echo; the draw entries (Term.recolor, Term.images, Term.petscii, Term.togglePetscii, Term.hasImages, Term.Filter, Term.FilterPalette, Term.palColor) are reached through the interface with their signatures as they were, not yet plain data (Term.zig says so); src/Term.zig is the one file outside src/term/ that imports one inside it (fs.zig embeds terminal.zig's text as a served source), panes.terminal, pardes.screen_state and pardes.echo go, the comments that named ghostty for the emulator say the emulator, and the tests that read emulator state read the records but three (Core.zig's palette, pty.zig's output pin and page limit); the changelog says so; nothing else changes --- src/term/terminal.zig | 91 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 75 insertions(+), 16 deletions(-) (limited to 'src/term/terminal.zig') diff --git a/src/term/terminal.zig b/src/term/terminal.zig index 70cd4e9d..85ac859d 100644 --- a/src/term/terminal.zig +++ b/src/term/terminal.zig @@ -13,6 +13,7 @@ const Key = pardes.Key; const Pane = panes.Pane; const Term = @import("../Term.zig"); const BodyWalk = @import("../body_layer.zig").BodyWalk; +const screen_state = @import("screen_state.zig"); const EditText = panes.EditText; const File = panes.File; @@ -483,6 +484,65 @@ pub fn takeMarks(state: *State, bytes: []const u8) void { m.finished = if (seq == std.math.maxInt(u32)) null else .{ .seq = seq, .status = if (status == std.math.minInt(i32)) null else status }; } +// ---- its replicas: the screen state they are sent (screen_state.zig) ---- + +/// What `peer` is owed of it, a delta against what it was sent (with the +/// marks of its prompt cycle), onto `out`; false when it has it already. +pub fn encode(state: *State, enc: *screen_state.Encoder, track: *screen_state.Track, peer: *screen_state.Peer, out: *std.Io.Writer) !bool { + var marks: [marks_len]u8 = undefined; + return screen_state.encode(enc, track, peer, &state.vt, state.changes, marksOf(state, &marks), out); +} + +/// Its rows `lo` to `hi` (absolute), as a replica holding them as holes +/// is sent them. +pub fn encodeRows(state: *State, enc: *screen_state.Encoder, track: *screen_state.Track, lo: u64, hi: u64, out: *std.Io.Writer) !void { + return screen_state.encodeRows(enc, track, &state.vt, lo, hi, out); +} + +/// What `track` kept of its versions, freed; `state` null when its pane is +/// gone with the emulator. +pub fn untrack(track: *screen_state.Track, gpa: std.mem.Allocator, state: ?*State) void { + track.deinit(gpa, if (state) |s| &s.vt else null); +} + +/// Whether a step read its rows since `clearRead` (`markRead`). +pub fn wasRead(state: *const State) bool { + return state.read; +} + +pub fn clearRead(state: *State) void { + state.read = false; +} + +/// A replica's terminal takes its authority's delta (and the marks riding +/// it); error.BaseMismatch when it is against a state not held here. +pub fn apply(state: *State, rep: *screen_state.Replica, bytes: []const u8) !screen_state.Replica.Applied { + const applied = try rep.apply(state.gpa, terminalIo(), &state.vt, bytes); + takeMarks(state, applied.extra); + return applied; +} + +/// ...and rows it asked for. +pub fn applyRows(state: *State, rep: *screen_state.Replica, bytes: []const u8) !void { + return rep.applyRows(state.gpa, &state.vt, bytes); +} + +/// The absolute rows its view shows on a replica. +pub fn shown(state: *State, rep: *const screen_state.Replica) [2]u64 { + return rep.shown(&state.vt); +} + +/// Its screen as text, every row's cells and flags (the replica tests +/// compare an authority's and a replica's). +pub fn dumpScreen(state: *State, w: *std.Io.Writer) !void { + return screen_state.dumpScreen(&state.vt, w); +} + +/// Rows `lo` to `hi` shown blank on a replica until they come. +pub fn blank(state: *State, rep: *const screen_state.Replica, lo: u64, hi: u64) void { + rep.blank(&state.vt, lo, hi); +} + const authorityOnly = pardes.tape.authorityOnly; /// Its view to row `at` of its history (from the top), or onto its output. @@ -1093,7 +1153,7 @@ pub fn textRows(state: *State, alloc: std.mem.Allocator, scratch: std.mem.Alloca var n: usize = 0; var it = std.mem.splitScalar(u8, full, '\n'); while (it.next()) |raw| { - const shown = if (pit.next()) |pin| + const kept = if (pit.next()) |pin| if (pin.rowAndCell().row.semantic_prompt != .none) promptRow(pin, raw) else raw else raw; @@ -1101,9 +1161,9 @@ pub fn textRows(state: *State, alloc: std.mem.Allocator, scratch: std.mem.Alloca text[at] = '\n'; at += 1; } - @memcpy(text[at..][0..shown.len], shown); - lines[n] = text[at..][0..shown.len]; - at += shown.len; + @memcpy(text[at..][0..kept.len], kept); + lines[n] = text[at..][0..kept.len]; + at += kept.len; n += 1; } text[at] = '\n'; @@ -1634,24 +1694,23 @@ pub fn due(state: *const State) ?u64 { /// 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 { +pub fn togglePetscii(state: *State) 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 { +pub fn petscii(maybe: ?*const State) ?bool { if (comptime !images_enabled) return null; - const state = pane.terminal orelse return null; + const state = maybe orelse return null; return state.images.petscii; } /// Whether the screen showing holds any kitty graphics image. -pub fn hasImages(pane: *const Pane) bool { +pub fn hasImages(maybe: ?*const State) bool { if (comptime !images_enabled) return false; - const state = pane.terminal orelse return false; + const state = maybe orelse return false; return state.vt.screens.active.kitty_images.images.count() > 0; } @@ -2065,9 +2124,9 @@ test "terminal Filter keys indexed truecolor OSC and background-only cells throu 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 edge = dynamic.at(tx + r.w - config.GUTTER - 1, body_y + 2).style; + try testing.expectEqual(asPardesColor(expected[196]), edge.fg); + try testing.expectEqual(asPardesColor(expected[59]), edge.bg); const explicit_before_reverse = dynamic.at(tx, body_y).style.fg; try testing.expectEqual(asPardesColor(expected[196]), explicit_before_reverse); @@ -2552,7 +2611,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].?)); + try std.testing.expectEqual(@as(?bool, true), petscii(restored.panes[0].?.terminal)); try std.testing.expect(p.executeBuiltinLine(0, "Petscii")); _ = frame.reset(.retain_capacity); s = try p.render(frame.allocator()); @@ -2564,12 +2623,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].?).?); + try std.testing.expectEqual(false, petscii(p.panes[0].?.terminal).?); // 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).?); + try std.testing.expectEqual(false, petscii(fresh.terminal).?); } test "a program asking the cell size in pixels (CSI 16 t) is told the shell's" { -- cgit v1.3