From d8e30740463dda5a87fb8a4f6e9755fbb84b7220 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 29 Oct 2026 20:08:12 -0300 Subject: dump: a session's dump keeps every view, the ones not live parked again by load; a session revived for one client is its live view alone, as view 0 Co-Authored-By: Claude Opus 5.5 --- src/views.zig | 181 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) (limited to 'src/views.zig') 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()); +} -- cgit v1.3