diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/dump.zig | 33 | ||||
| -rw-r--r-- | src/views.zig | 181 |
2 files changed, 210 insertions, 4 deletions
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()); +} |
