summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--build/snap.zig2
-rw-r--r--docs/next-daemon.md39
-rw-r--r--src/dump.zig33
-rw-r--r--src/views.zig181
4 files changed, 244 insertions, 11 deletions
diff --git a/build/snap.zig b/build/snap.zig
index dad23c53..ece84ad4 100644
--- a/build/snap.zig
+++ b/build/snap.zig
@@ -93,7 +93,7 @@ pub fn addTty(b: *std.Build, opts: struct {
const run_views_replay = b.addRunArtifact(monkey);
run_views_replay.addArtifactArg(snap);
run_views_replay.addArtifactArg(opts.exe);
- run_views_replay.addArgs(&.{ b.fmt("{d}", .{seeds}), "70", "--views=3", "--tape=" ++ tapes ++ "/monkey-views", "--out=" ++ tapes ++ "/monkey-views" });
+ run_views_replay.addArgs(&.{ b.fmt("{d}", .{seeds}), "70", "--views=3", "--tape=" ++ tapes ++ "/monkey-views", "--dump", "--out=" ++ tapes ++ "/monkey-views" });
run_views_replay.setCwd(b.path("."));
run_views_replay.has_side_effects = true;
run_views_replay.step.dependOn(&run_monkey_replay.step);
diff --git a/docs/next-daemon.md b/docs/next-daemon.md
index 95713b42..0462c445 100644
--- a/docs/next-daemon.md
+++ b/docs/next-daemon.md
@@ -403,7 +403,9 @@ the ponytail lens:
named in a path yet (a script's focus or layout is that view's).
- Every change draws every view (`needs_frame` is the session's); a
view's own dirtiness waits for a measurement that wants it.
- - Dump, Restore and Handover keep one view, the live one.
+ - Restore and `-l` keep one view, the live one; a dump has every view
+ (phase 2 notes), and Handover carries panes, which join the views of
+ the window they land in.
- **What proved wrong in the design.** "Views" in the design were a
struct the core reads through; the swap made them cheaper and kept every
reference. Its rollback copy (phase 5) is the same list of fields. The
@@ -1340,14 +1342,39 @@ dump hooks; `pardes-snap --dump`, `pardes-monkey --dump`, and both in
layout (`dropMissing`), never more. What was let go is the session's
first message after the load. A pane whose terminal another build could
not read is a fresh terminal (`ensureKind`).
-- **Roots, not wrappers.** A session's dump is three roots: the core,
+- **Roots, not wrappers.** A session's dump is five roots: the core,
read *into* a core `Pardes.create` made (its local, derived and
authority fields stay create's: no defaults to invent for an arena or a
host), its 9P mounts (state the user made, inside the 9P server's
- authority `fs`), and which file panes' files were on disk when it was
- made (one gone by the load is said deleted, so Del asks: its text is
- nowhere else; one never on disk comes back as it was). A pane's dump
- (the store's, a handover's) is a Pane alone.
+ authority `fs`), which file panes' files were on disk when it was made
+ (one gone by the load is said deleted, so Del asks: its text is nowhere
+ else; one never on disk comes back as it was), the theme's name, and
+ the views not live. A pane's dump (the store's, a handover's) is a Pane
+ alone.
+- **Every view is in the dump** (decided after phase 4 merged). The live
+ view is the core's own fields, so `view` and `views_open` were dumped
+ already, and a session dumped with several views loaded into a core
+ that said so with no parked slot: its first view switch, close or
+ drawing of the others unwrapped a null. Leaving them out would have
+ needed `load` to fold them anyway, and the join (phase 5) needs them:
+ a replica applies the other clients' `toView`. So `views.dumped` writes
+ each open view but the live one (the live one's slot is stale by
+ design): its share of the session but what drawing keeps
+ (`Pardes.replica.local`, computed from `views.fields`: its panels as
+ presented, its animations), its slots' serials, and its share of every
+ pane (`pv_known` says which shares own their selections, not which
+ exist: a parked view has one of every pane). `views.undumped` parks
+ them again in `load`, last, so nothing after it rolls back what it
+ moved; a pane a view has no share of gets the live view's, a view the
+ core says open that the dump lacks is closed, a dump with no views root
+ (before this) has one view. They stay out of the digest as before
+ (parked views are checked when live again); the gate is the re-dump
+ byte for byte, now also on the three-view monkey seeds. A session
+ *revived* for one client (Restore, `-l`) is one view, the live one as
+ view 0 (`views.only`, what the hosts already assumed), and a pane
+ carried or reopened takes the view it lands in (`sized_by`,
+ `prompt_view`). Cost: the views root is empty for one view; with
+ several, a share is about 100 bytes a pane a view.
- **Aliasing by reference.** Zerox twins share their text and undo again,
an `inherited` cwd points at its pane again. Keyed by address, length
and type, which is also what made twins dump their text once.
diff --git a/src/dump.zig b/src/dump.zig
index c33ac02c..e6a41f09 100644
--- a/src/dump.zig
+++ b/src/dump.zig
@@ -19,6 +19,7 @@ const MAX_COLS = pardes.MAX_COLS;
const effect_path_cap = pardes.effect_path_cap;
const Options = pardes.Options;
const Pardes = pardes.Pardes;
+const views = pardes.views;
/// Where the next dump goes. $PARDES_DUMP wins verbatim (the snapshot harness
/// pins it for deterministic goldens); else $XDG_DATA_HOME|~/.local/share
@@ -144,11 +145,12 @@ pub fn defaultDirectory(buf: []u8) ?[]const u8 {
/// the frame's or worked out again (`replica` in each type, src/walk.zig),
/// then its 9P mounts, which live with the 9P server's own state, which of
/// its panes' files were on disk when it was made (one gone by its load is
-/// said deleted: its text is nowhere else now), and its theme's name: the
+/// said deleted: its text is nowhere else now), its theme's name (the
/// setting is a place in this build's ring of themes, another build's
-/// ring is another.
-const Session = walk.Dump(&.{ Pardes, []const filesystem.Mount, OnDisk, []const u8 });
-const SessionLoad = walk.Load(&.{ Pardes, []const filesystem.Mount, OnDisk, []const u8 });
+/// ring is another), and the views not live (views.zig: the core holds
+/// the live one's).
+const Session = walk.Dump(&.{ Pardes, []const filesystem.Mount, OnDisk, []const u8, []const views.Dumped });
+const SessionLoad = walk.Load(&.{ Pardes, []const filesystem.Mount, OnDisk, []const u8, []const views.Dumped });
const OnDisk = [MAX_PANES]bool;
fn onDisk(pane: *const pardes.panes.Pane) bool {
@@ -180,6 +182,13 @@ pub fn dumpBytes(p: *Pardes) ![]u8 {
try d.put(OnDisk, &on_disk);
const theme: []const u8 = colors.themes[@min(p.settings.theme, colors.themes.len - 1)].name;
try d.put([]const u8, &theme);
+ const others = try views.dumped(p, p.gpa);
+ defer {
+ for (others) |o| p.gpa.free(o.panes);
+ p.gpa.free(others);
+ }
+ const kept: []const views.Dumped = others;
+ try d.put([]const views.Dumped, &kept);
return d.finish();
}
@@ -263,6 +272,18 @@ fn loadKnowing(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, notes:
for (p.panes) |slot| if (slot) |pane| try ensureKind(p, pane);
if (notes) |n| try n.appendSlice(gpa, l.notes.items);
}
+ // The views not live, last: what it moves into the core is never
+ // rolled back from under it, nothing after it failing.
+ const at = l.shared.items.len;
+ const others = l.get([]const views.Dumped) catch |err| switch (err) {
+ // A dump made before views were in it.
+ error.EndOfDump => &.{},
+ else => return err,
+ };
+ try views.undumped(p, others);
+ for (others) |o| gpa.free(o.panes);
+ gpa.free(others);
+ l.shared.shrinkRetainingCapacity(at);
return p;
}
@@ -351,6 +372,9 @@ fn loadAndRevive(gpa: std.mem.Allocator, opts: Options, bytes: []const u8, previ
/// panes): the store and Recent read (a Restore's carried from the session
/// it replaces) and its ThemeFile asked for again.
fn reviveSession(p: *Pardes, previous: ?*const Pardes) !void {
+ // ponytail: one client loads it, so it is one view, the live one's; a
+ // replica joining (phase 5) takes `load`'s, every view.
+ views.only(p);
if (previous) |old| {
// Never a serial or a request number the old session gave out.
p.next_serial = @max(p.next_serial, old.next_serial);
@@ -471,6 +495,7 @@ pub fn loadPane(p: *Pardes, id: usize, bytes: []const u8) !*pardes.panes.Pane {
pane.command_from = 0;
pane.places.from = 0;
pane.collapsed = false;
+ pane.prompt_view = p.view;
if (pane.file) |*f| if (f.content.len == 0 and (f.saved_len orelse 0) > 0) {
// A clean file kept slim: its text is its file's.
if (filesystem.read(p, f.path)) |text| {
diff --git a/src/views.zig b/src/views.zig
index e99e1694..329916a2 100644
--- a/src/views.zig
+++ b/src/views.zig
@@ -79,6 +79,9 @@ pub const PaneView = struct {
scroll: usize = 0,
cols: u16 = 1,
rows: u16 = 1,
+
+ /// In a dump (`Dumped`): what a pane's step works out again.
+ pub const replica = .{ .derived = .{ "view", "notices", "pan" } };
};
/// A view not live: its share of the session, and the serial of each pane
@@ -88,6 +91,141 @@ pub const Parked = struct {
serials: [MAX_PANES]u32 = @splat(0),
};
+/// What a session's dump keeps of a view's share: all of it but what
+/// drawing keeps (Pardes.replica's local fields: its animations, its
+/// panels as presented).
+const kept = blk: {
+ @setEvalBranchQuota(100_000);
+ var names: []const [:0]const u8 = &.{};
+ for (fields) |name| {
+ const local = for (Pardes.replica.local) |l| {
+ if (std.mem.eql(u8, l, name)) break true;
+ } else false;
+ if (!local) names = names ++ &[_][:0]const u8{name};
+ }
+ const out: [names.len][:0]const u8 = names[0..names.len].*;
+ break :blk out;
+};
+
+/// A view not live as a session's dump keeps it (dump.zig): its share of
+/// the session, its slots' serials, and its share of each pane. The live
+/// view is the core's own fields; its slot is stale, and no part of it.
+pub const Dumped = struct {
+ k: u8,
+ view: Part(Pardes, &kept),
+ serials: [MAX_PANES]u32 = @splat(0),
+ panes: []const Share = &.{},
+
+ pub const Share = struct { id: u16, pv: PaneView };
+};
+
+fn bit(k: usize) u8 {
+ return @as(u8, 1) << @intCast(k);
+}
+
+/// The views not live, as a dump keeps them: the slices `gpa`'s, what
+/// they hold the session's.
+pub fn dumped(p: *const Pardes, gpa: std.mem.Allocator) ![]Dumped {
+ var out: std.ArrayList(Dumped) = .empty;
+ errdefer {
+ for (out.items) |d| gpa.free(d.panes);
+ out.deinit(gpa);
+ }
+ for (0..max) |k| {
+ if (k == p.view or !isOpen(p, @intCast(k))) continue;
+ const slot = p.parked[k] orelse continue;
+ var d: Dumped = .{ .k = @intCast(k), .view = undefined, .serials = slot.serials };
+ copy(&kept, &d.view, &slot.view);
+ var shares: std.ArrayList(Dumped.Share) = .empty;
+ errdefer shares.deinit(gpa);
+ // A view not live has a share of every pane: written when it went
+ // out, made with one made since (`installed`). `pv_known` says
+ // which shares own their selections, not which there are.
+ for (p.panes, 0..) |s, id| if (s) |pane| {
+ try shares.append(gpa, .{ .id = @intCast(id), .pv = pane.pv[k] });
+ };
+ d.panes = try shares.toOwnedSlice(gpa);
+ try out.append(gpa, d);
+ }
+ return out.toOwnedSlice(gpa);
+}
+
+/// The views a dump kept (`dumped`) parked again in `p`, a session just
+/// loaded with its live view: each its slot, each pane its share in it
+/// (one a view had none of as the live view has it). What they hold is the
+/// session's then, the slices the caller's; a failure moves nothing. A
+/// view the core says open that the dump has none of is not.
+pub fn undumped(p: *Pardes, others: []const Dumped) !void {
+ var take: u8 = 0;
+ for (others) |d| if (d.k < max and d.k != p.view and isOpen(p, d.k) and take & bit(d.k) == 0) {
+ take |= bit(d.k);
+ };
+ var made: u8 = 0;
+ errdefer for (0..max) |k| if (made & bit(k) != 0) {
+ p.gpa.destroy(p.parked[k].?);
+ p.parked[k] = null;
+ };
+ if (take != 0) for (0..max) |k| if ((take | bit(p.view)) & bit(k) != 0 and p.parked[k] == null) {
+ const slot = try p.gpa.create(Parked);
+ slot.* = .{ .view = .{ .screen_w = p.screen_w, .screen_h = p.screen_h } };
+ p.parked[k] = slot;
+ made |= bit(k);
+ };
+ // Nothing fails from here.
+ p.views_open &= take | bit(p.view);
+ for (p.panes) |s| if (s) |pane| {
+ pane.pv_known |= bit(p.view);
+ };
+ var left = take;
+ for (others) |d| {
+ var view = d.view;
+ if (left & bit(d.k) == 0) {
+ // Refused (another build's, or twice): what it holds goes.
+ for (&view.col_tags) |*t| t.deinit(p.gpa);
+ if (view.chord_arg) |arg| p.gpa.free(arg);
+ for (d.panes) |sh| if (sh.pv.body.sels.len > 0) p.gpa.free(sh.pv.body.sels);
+ continue;
+ }
+ left &= ~bit(d.k);
+ const slot = p.parked[d.k].?;
+ slot.serials = d.serials;
+ copy(&kept, &slot.view, &view);
+ for (d.panes) |sh| {
+ const pane = (if (sh.id < MAX_PANES) p.panes[sh.id] else null) orelse {
+ if (sh.pv.body.sels.len > 0) p.gpa.free(sh.pv.body.sels);
+ continue;
+ };
+ if (pane.pv_known & bit(d.k) != 0) {
+ if (sh.pv.body.sels.len > 0) p.gpa.free(sh.pv.body.sels);
+ continue;
+ }
+ pane.pv[d.k] = sh.pv;
+ pane.pv_known |= bit(d.k);
+ }
+ for (p.panes) |s| if (s) |pane| if (pane.pv_known & bit(d.k) == 0) {
+ pane.pv[d.k] = fresh(pane);
+ pane.pv_known |= bit(d.k);
+ };
+ }
+}
+
+/// The live view alone, as view 0: every other closed. A session loaded
+/// for one client (a Restore, `-l`) is one view.
+pub fn only(p: *Pardes) void {
+ for (0..max) |k| if (k != p.view) close(p, @intCast(k));
+ const k = p.view;
+ if (k == 0) return;
+ std.mem.swap(?*Parked, &p.parked[0], &p.parked[k]);
+ for (p.panes) |s| if (s) |pane| {
+ pane.pv[0] = pane.pv[k];
+ pane.pv_known = if (pane.pv_known & bit(k) != 0) 1 else 0;
+ if (pane.sized_by == k) pane.sized_by = 0;
+ if (pane.prompt_view == k) pane.prompt_view = 0;
+ };
+ p.view = 0;
+ p.views_open = 1;
+}
+
/// The fields of `T` named, as a struct of their own, each of its type and
/// default.
fn Part(comptime T: type, comptime names: []const [:0]const u8) type {
@@ -612,3 +750,46 @@ test "a terminal shown in a view shorter than it keeps its cursor in sight" {
p.toView(k);
try testing.expectEqual(@as(u16, 0), term.pan);
}
+
+test "a session's dump keeps its views: loaded, each where it was; revived for one client, the live one alone" {
+ const gpa = testing.allocator;
+ const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 });
+ defer p.deinit();
+ const text = try numbered(gpa, 200);
+ defer gpa.free(text);
+ const pane = try p.setTestFile(text);
+ const id = for (p.panes, 0..) |s, i| {
+ if (s == pane) break i;
+ } else unreachable;
+ const k = p.openView().?;
+ cursorTo(p, pane, 119);
+ p.toView(k);
+ p.update(.{ .command = "Newcol" });
+ p.toView(0);
+ p.toView(k);
+ cursorTo(p, pane, 30);
+ const ncol = p.ncol;
+ const bytes = try pardes.dump.dumpBytes(p);
+ defer gpa.free(bytes);
+ const loaded = try pardes.dump.load(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }, bytes, null);
+ defer loaded.deinit();
+ try testing.expectEqual(pardes.walk.digest(Pardes, p), pardes.walk.digest(Pardes, loaded));
+ const again = try pardes.dump.dumpBytes(loaded);
+ defer gpa.free(again);
+ try testing.expectEqualSlices(u8, bytes, again);
+ const lp = loaded.panes[id].?;
+ loaded.toView(0);
+ try testing.expectEqual(@as(i32, 119), lp.body.cur_row);
+ try testing.expectEqual(ncol - 1, loaded.ncol);
+ loaded.toView(k);
+ try testing.expectEqual(@as(i32, 30), lp.body.cur_row);
+ try testing.expectEqual(ncol, loaded.ncol);
+ // For one client: the view live when dumped, as view 0.
+ const one = try pardes.dump.initFromDump(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 }, bytes);
+ defer one.deinit();
+ try testing.expectEqual(@as(usize, 1), count(one));
+ try testing.expectEqual(@as(u8, 0), one.view);
+ try testing.expectEqual(ncol, one.ncol);
+ try testing.expectEqual(@as(i32, 30), one.panes[id].?.body.cur_row);
+ try testing.expectEqual(@as(?u8, 1), one.openView());
+}