summaryrefslogtreecommitdiff
path: root/test/snapshot.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-29 17:01:35 -0300
committerGabriel Schneider <[email protected]>2026-10-29 19:38:53 -0300
commit2cce341e2c94fb3150dc1f3e7911c26502a70487 (patch)
treec637fcfa9a52aa66f6c34600f4cdea4020088e01 /test/snapshot.zig
parentc2b4f235e78735102c691677f08bd1b2403168df (diff)
downloadpardes-2cce341e2c94fb3150dc1f3e7911c26502a70487.tar.gz
pardes-2cce341e2c94fb3150dc1f3e7911c26502a70487.zip
views: several clients on one session, each its own layout, focus, cursors and scroll over the panes they share; a second `pardes --attach` draws on its own terminal beside the first, and `pardes-gui --attach` opens another window
The core still sees one view, the live one, in the fields it always had (src/views.zig): a view's share of the session (`views.fields`: its screen, columns, rects, focus, pointer and gestures, jumps, key prefixes, column tags, presentation and animations, windowed and native_images) and of each pane (`PaneView`: its place in the layout, which text has the keyboard, the body's cursor, selections and mode, its scroll, a document's size, what the step measured of it) is copied out when another view goes live and back when it is live again. One view never parks: the single client pays a test of one byte a step. `toView`, `openView` and `closeView` are taped inputs; the pump draws each other view by going into it and back, which leaves every digested field as it was, so the replica that draws nothing skips it. A parked view is in no digest (its slot is written whenever it goes out); what drawing it made of its own (animations, presentation) is kept, whatever its unpark brought inside a changed text is done again when it is next live. Shared: the panes' text, terminals, tags, prompts, messages, registers (yanked in one view, put in another), the workspace tag and settings. A terminal shown at two sizes takes the size of the view that typed into it last (`Pane.sized_by`), and any view's once that one is gone; the others show it at that size. A text edited in one view moves the other views' cursors and scroll with the splice, as a Zerox twin's (File.ParkedPlaces). A pane closed in one view leaves the others' layouts (by its slot's serial); a pane focused in a view that does not show it is placed in its active column (views.show). tty: each terminal is a Screen of its own (its vaxis, input reader, frame answer, kitty images and shm probe, glide, trail and flash); its input reaches the loop as its view's (a `from` ahead of it when another terminal posted last, so one terminal posts none). A SIGWINCH asks every terminal its size; a SIGHUP asks each whether it hung up or lost its stub. Detach, or a terminal's end, with other views left closes only that view, its stub told `d`; the session's end tells every stub. daemon.zig: an attach names an attached session too (or the only one there is when none is detached). gui: the Gui holds the live window's share of itself (window_fields: the SDL window and its presenting, scroll, glide, scene and post chain, pointer, blink, captures); the others wait in Shell.windows, swapped in (`follow`) as the core's live view changes, and each SDL event first takes its window's view live (`route`). Closing a window while others stay closes its view. Harnesses: `start <rows> <cols> views=N` in a snapshot script gives the app N-1 more terminals (PARDES_TEST_VIEWS, opened as attached ones), `view <k>` types into and captures terminal k, every wait reads them all; `pardes-monkey --views=N` switches among N; `zig build replay` runs its seeds at three views too. views.snap pins two terminals: an edit seen in both, a scroll in one, a line yanked in one and put in the other, Newcol in one, Detach in one. Unit tests: own cursor, scroll, mode, layout and focus; edits shared; registers shared; terminal size by last typist; closing views; drawing the others leaves the digest; a view keeps its place in a text another edits. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'test/snapshot.zig')
-rw-r--r--test/snapshot.zig92
1 files changed, 73 insertions, 19 deletions
diff --git a/test/snapshot.zig b/test/snapshot.zig
index 255f55bf..8a0583e3 100644
--- a/test/snapshot.zig
+++ b/test/snapshot.zig
@@ -29,7 +29,11 @@
// lines <name> <n> [tail] create file with n numbered lines, tail on each
// dirmk <name> create a subdirectory
// run <shell...> run a command in the script's cwd (NOT in a pane)
-// start <rows> <cols> [arg] fork the app in a pty (optional extra CLI arg)
+// start <rows> <cols> [views=N] [arg] fork the app in a pty (optional
+// extra CLI arg); views=N gives it N-1 more
+// terminals, each a view of the session
+// view <k> type into and capture terminal k (1 is the
+// app's own; a resize there SIGWINCHes the app)
// wait <ms> <needle...> pump until needle appears on the grid (fails hard)
// settle <ms> pump for a fixed duration
// stable <quiet_ms> <timeout_ms> pump until the grid stops changing
@@ -74,6 +78,7 @@ const Client = @import("9p_io").Client;
pub const std_options: std.Options = .{ .log_level = .err };
extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int;
+extern "c" fn unsetenv(name: [*:0]const u8) c_int;
extern "c" fn execvp(file: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int;
extern "c" fn system(cmd: [*:0]const u8) c_int;
extern "c" fn mkdtemp(template: [*:0]u8) ?[*:0]u8;
@@ -652,6 +657,14 @@ fn runScript(arena: std.mem.Allocator, exe_z: [:0]const u8, script_path: []const
var chunk_len: usize = 0;
var h: ?eh.Harness = null;
defer if (h) |*hp| hp.deinit();
+ // `views=N`: the app's other terminals, and the one driven now.
+ var others: [3]eh.Harness = undefined;
+ var nothers: usize = 0;
+ var group: [4]*eh.Harness = undefined;
+ var cur: usize = 0;
+ defer for (others[0..nothers]) |*o| o.deinit();
+ // A capture is a delta against the previous one of its terminal's.
+ var prevs: [4]Prev = @splat(.{});
// captures are deltas against the previous one of the same kind (see Prev)
var prev: Prev = .{};
@@ -717,15 +730,46 @@ fn runScript(arena: std.mem.Allocator, exe_z: [:0]const u8, script_path: []const
const cols = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10);
var extra: [6]?[*:0]const u8 = @splat(null);
var nextra: usize = 0;
- while (tok.next()) |a| : (nextra += 1) {
+ var nviews: usize = 1;
+ while (tok.next()) |a| {
+ if (std.mem.startsWith(u8, a, "views=")) {
+ nviews = try std.fmt.parseInt(usize, a["views=".len..], 10);
+ if (nviews < 1 or nviews > 4) return error.BadScript;
+ continue;
+ }
if (nextra >= extra.len) return error.BadScript;
extra[nextra] = (try arena.dupeSentinel(u8, a, 0)).ptr;
+ nextra += 1;
}
if (h) |*hp| hp.deinit(); // scripts may restart (e.g. dump then -l)
+ for (others[0..nothers]) |*o| o.deinit();
+ nothers = 0;
+ cur = 0;
// a new process is a new screen: capture it whole, or its first
// frame would be a delta against a dead instance's rows
prev = .{};
+ prevs = @splat(.{});
+ var paths: std.ArrayList(u8) = .empty;
+ while (nothers + 1 < nviews) : (nothers += 1) {
+ var path: [64]u8 = undefined;
+ others[nothers] = try eh.Harness.initView(gpa, rows, cols, &path);
+ try paths.print(arena, "{s}{s}", .{ if (paths.items.len > 0) " " else "", std.mem.sliceTo(&path, 0) });
+ }
+ if (nothers > 0) _ = setenv("PARDES_TEST_VIEWS", try arena.dupeSentinel(u8, paths.items, 0), 1);
h = try eh.Harness.initArgv(gpa, exe_z.ptr, rows, cols, extra[0..nextra]);
+ _ = unsetenv("PARDES_TEST_VIEWS");
+ group[0] = &h.?;
+ for (others[0..nothers], 1..) |*o, i| {
+ o.pid = h.?.pid;
+ group[i] = o;
+ }
+ for (group[0 .. nothers + 1]) |v| v.views = group[0 .. nothers + 1];
+ } else if (std.mem.eql(u8, cmd, "view")) {
+ const k = try std.fmt.parseInt(usize, tok.next() orelse return error.BadScript, 10);
+ if (k < 1 or k > nothers + 1) return error.BadScript;
+ prevs[cur] = prev;
+ cur = k - 1;
+ prev = prevs[cur];
} else if (std.mem.eql(u8, cmd, "snapfile")) {
const label = tok.next() orelse return error.BadScript;
const contents = try readFileAlloc(arena, tok.rest());
@@ -735,49 +779,52 @@ fn runScript(arena: std.mem.Allocator, exe_z: [:0]const u8, script_path: []const
try appendChunk(chunks, &chunk_len, try snapFile(arena, label, contents));
} else if (std.mem.eql(u8, cmd, "wait")) {
const ms = try std.fmt.parseInt(i64, tok.next() orelse return error.BadScript, 10);
- try live(&h).expectWaitFor(tok.rest(), ms, "snapshot wait");
+ try on(&h, &group, nothers, cur).expectWaitFor(tok.rest(), ms, "snapshot wait");
} else if (std.mem.eql(u8, cmd, "settle")) {
- try live(&h).pump(try std.fmt.parseInt(i64, tok.next() orelse return error.BadScript, 10));
+ try on(&h, &group, nothers, cur).pump(try std.fmt.parseInt(i64, tok.next() orelse return error.BadScript, 10));
} else if (std.mem.eql(u8, cmd, "stable")) {
const quiet = try std.fmt.parseInt(i64, tok.next() orelse return error.BadScript, 10);
const timeout = try std.fmt.parseInt(i64, tok.next() orelse return error.BadScript, 10);
- try waitStable(live(&h), quiet, timeout);
+ try waitStable(on(&h, &group, nothers, cur), quiet, timeout);
} else if (std.mem.eql(u8, cmd, "text")) {
- try live(&h).send(tok.rest());
+ try on(&h, &group, nothers, cur).send(tok.rest());
} else if (std.mem.eql(u8, cmd, "send")) {
- try live(&h).send(try unescape(arena, tok.rest()));
+ try on(&h, &group, nothers, cur).send(try unescape(arena, tok.rest()));
} else if (std.mem.eql(u8, cmd, "key")) {
- while (tok.next()) |name| try live(&h).send(keyBytes(name) orelse return error.BadKey);
+ while (tok.next()) |name| try on(&h, &group, nothers, cur).send(keyBytes(name) orelse return error.BadKey);
} else if (std.mem.eql(u8, cmd, "press") or std.mem.eql(u8, cmd, "release") or std.mem.eql(u8, cmd, "drag")) {
const btn = buttonCode(tok.next() orelse return error.BadScript) orelse return error.BadScript;
const col_spec = tok.next() orelse return error.BadScript;
const row = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10);
- const col = try resolveCol(live(&h), col_spec, row);
+ const col = try resolveCol(on(&h, &group, nothers, cur), col_spec, row);
const b: u16 = if (std.mem.eql(u8, cmd, "drag")) btn + 32 else btn;
const fin: u8 = if (std.mem.eql(u8, cmd, "release")) 'm' else 'M';
- try live(&h).send(try std.fmt.allocPrint(arena, "\x1b[<{d};{d};{d}{c}", .{ b, col, row, fin }));
+ try on(&h, &group, nothers, cur).send(try std.fmt.allocPrint(arena, "\x1b[<{d};{d};{d}{c}", .{ b, col, row, fin }));
} else if (std.mem.eql(u8, cmd, "motion")) {
const col_spec = tok.next() orelse return error.BadScript;
const row = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10);
- const col = try resolveCol(live(&h), col_spec, row);
- try live(&h).send(try std.fmt.allocPrint(arena, "\x1b[<35;{d};{d}M", .{ col, row }));
+ const col = try resolveCol(on(&h, &group, nothers, cur), col_spec, row);
+ try on(&h, &group, nothers, cur).send(try std.fmt.allocPrint(arena, "\x1b[<35;{d};{d}M", .{ col, row }));
} else if (std.mem.eql(u8, cmd, "wheel")) {
const dir = tok.next() orelse return error.BadScript;
const b: u16 = if (std.mem.eql(u8, dir, "up")) 64 else 65;
const col_spec = tok.next() orelse return error.BadScript;
const row = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10);
- const col = try resolveCol(live(&h), col_spec, row);
- try live(&h).send(try std.fmt.allocPrint(arena, "\x1b[<{d};{d};{d}M", .{ b, col, row }));
+ const col = try resolveCol(on(&h, &group, nothers, cur), col_spec, row);
+ try on(&h, &group, nothers, cur).send(try std.fmt.allocPrint(arena, "\x1b[<{d};{d};{d}M", .{ b, col, row }));
} else if (std.mem.eql(u8, cmd, "resize")) {
const rows = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10);
const cols = try std.fmt.parseInt(u16, tok.next() orelse return error.BadScript, 10);
- try live(&h).resize(rows, cols);
+ try on(&h, &group, nothers, cur).resize(rows, cols);
+ // Another view's terminal is no one's controlling one: the app
+ // is told it changed, as a stub tells it of its own.
+ if (cur > 0) std.posix.kill(live(&h).pid, std.posix.SIG.WINCH) catch {};
} else if (std.mem.eql(u8, cmd, "snap")) {
- try appendChunk(chunks, &chunk_len, try snapText(arena, live(&h), tok.rest(), &prev));
+ try appendChunk(chunks, &chunk_len, try snapText(arena, on(&h, &group, nothers, cur), tok.rest(), &prev));
} else if (std.mem.eql(u8, cmd, "snapstyle")) {
- try appendChunk(chunks, &chunk_len, try snapStyle(arena, live(&h), tok.rest(), &prev));
+ try appendChunk(chunks, &chunk_len, try snapStyle(arena, on(&h, &group, nothers, cur), tok.rest(), &prev));
} else if (std.mem.eql(u8, cmd, "snap9p")) {
- try appendChunk(chunks, &chunk_len, try snap9p(arena, live(&h), tok.rest(), &prev));
+ try appendChunk(chunks, &chunk_len, try snap9p(arena, on(&h, &group, nothers, cur), tok.rest(), &prev));
} else {
std.debug.print("unknown command {s}\n", .{cmd});
return error.BadScript;
@@ -891,6 +938,13 @@ fn snapFile(arena: std.mem.Allocator, label: []const u8, contents: []const u8) !
return out;
}
+/// The terminal the script drives now (`view`).
+fn on(h: *?eh.Harness, group: *[4]*eh.Harness, nothers: usize, cur: usize) *eh.Harness {
+ const app = live(h);
+ if (cur == 0 or cur > nothers) return app;
+ return group[cur];
+}
+
fn live(h: *?eh.Harness) *eh.Harness {
if (h.*) |*v| return v;
fatal("script used the app before `start`", .{});
@@ -928,7 +982,7 @@ fn waitStable(h: *eh.Harness, quiet: i64, timeout: i64) !void {
// and nothing in its process tree is runnable. Held across two probes,
// because a pane's shell can write and sleep a scheduler tick before
// the app wakes to read it.
- if (since >= probe_after_ms and h.pending() == 0 and treeAsleep(h.pid)) {
+ if (since >= probe_after_ms and h.pendingAll() == 0 and treeAsleep(h.pid)) {
if (asleep_since == 0) {
asleep_since = now;
} else if (now - asleep_since >= probe_hold_ms) {