summaryrefslogtreecommitdiff
path: root/src/screen_state.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/screen_state.zig')
-rw-r--r--src/screen_state.zig336
1 files changed, 168 insertions, 168 deletions
diff --git a/src/screen_state.zig b/src/screen_state.zig
index 6b12f9eb..eeb1c059 100644
--- a/src/screen_state.zig
+++ b/src/screen_state.zig
@@ -70,7 +70,7 @@ pub fn rowHash(page: *Page, row: *Row, cols: u16) Hash {
fn rowFlags(row: *const Row) u8 {
return @as(u8, @intFromBool(row.wrap)) | @as(u8, @intFromBool(row.wrap_continuation)) << 1 |
- @as(u8, @intFromEnum(row.semantic_prompt)) << 2;
+ @as(u8, @backingInt(row.semantic_prompt)) << 2;
}
/// The authority's absolute row numbers for one terminal: its primary
@@ -397,9 +397,9 @@ fn write(e: *Encoder, tr: *Track, base: ?*const Known, t: *Terminal, extra: []co
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(@intFromEnum(screen.cursor.cursor_style));
+ try out.writeByte(@backingInt(screen.cursor.cursor_style));
try out.writeByte(@intFromBool(screen.cursor.pending_wrap));
- try out.writeByte(@intFromEnum(screen.cursor.semantic_content));
+ 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);
@@ -626,7 +626,7 @@ pub const Replica = struct {
}
const screen = t.screens.active;
- const held = &r.held[@intFromEnum(t.screens.active_key)];
+ 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);
@@ -638,9 +638,9 @@ pub const Replica = struct {
}
screen.cursorReload();
screen.cursorAbsolute(@min(cx, t.cols - 1), @min(cy, t.rows - 1));
- screen.cursor.cursor_style = @enumFromInt(style);
+ screen.cursor.cursor_style = @fromBackingInt(@intCast(style));
screen.cursor.pending_wrap = wrap != 0;
- screen.cursor.semantic_content = @enumFromInt(semantic);
+ 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) });
@@ -833,10 +833,10 @@ 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,
+ 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));
@@ -1135,164 +1135,6 @@ pub fn yesStream(gpa: std.mem.Allocator, lines: usize) ![]u8 {
return out;
}
-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;
- }
- };
- }
-}
-
// ---- a pane of a kind, as a Shadow (architecture §16.5) ----
//
// A replica's core has no code for a library's kind: such a pane is drawn
@@ -1557,6 +1399,164 @@ pub fn encodeShadow(gpa: std.mem.Allocator, sent: *ShadowSent, f: *const Kind.Fr
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));