summaryrefslogtreecommitdiff
path: root/src/dump.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/dump.zig')
-rw-r--r--src/dump.zig288
1 files changed, 144 insertions, 144 deletions
diff --git a/src/dump.zig b/src/dump.zig
index 9849c8e4..82b55ae6 100644
--- a/src/dump.zig
+++ b/src/dump.zig
@@ -57,14 +57,6 @@ pub fn directory(buf: []u8, dir_setting: []const u8) ?[]const u8 {
return std.fmt.bufPrint(buf, "{s}", .{resolved}) catch null;
}
-test "a DumpDir reads back absolute, however it was written" {
- var buf: [4096]u8 = undefined;
- try std.testing.expectEqualStrings("/tmp/dumps", directory(&buf, "/tmp/dumps/").?);
- try std.testing.expectEqualStrings("/tmp/dumps", directory(&buf, "/tmp//x/../dumps").?);
- const rel = directory(&buf, "rel/");
- try std.testing.expect(std.Io.Dir.path.isAbsolute(rel.?) and std.mem.endsWith(u8, rel.?, "/rel"));
-}
-
/// The standard dump directory, without creating it or generating a filename.
pub fn defaultDirectory(buf: []u8) ?[]const u8 {
if (hostEnv("XDG_DATA_HOME")) |x|
@@ -696,6 +688,149 @@ pub fn loadPane(p: *Pardes, id: usize, bytes: []const u8) !*pardes.panes.Pane {
return pane;
}
+// ---- a pane of a kind (Kind.Slot) ----
+
+/// What a dump keeps of a pane of a kind: the kind, its path, its place,
+/// the art bytes, its words' last values, whether its kind was here, and
+/// the kind's own record when it has one.
+const SlotRecord = struct {
+ name: []const u8 = "",
+ version: u32 = 0,
+ path: []const u8 = "",
+ where: []const u8 = "",
+ art: @import("surface.zig").Placement.Art = .{},
+ record: ?[]const u8 = null,
+ values: []const u8 = "",
+ live: bool = false,
+ /// What decides its next calls (Kind.step): a replica joining keeps
+ /// them, its Shadow calling where the authority's kind does.
+ next_tick: u64 = 0,
+ views: [views.max]u64 = @splat(0),
+ focused: bool = false,
+ /// The jobs whose answers it dropped (api.cancel): a replica's drop them too.
+ cancelled: [4]u32 = @splat(0),
+ /// Its tick's prefix: a replica's tag laid out as the session's.
+ prefix: []const u8 = "",
+ /// A placeholder's why (its file not here).
+ refused: []const u8 = "",
+ /// What a step reads of it that its kind's open made (Kind.reopen) or
+ /// a call set: its tag's default words and what it has selected. A
+ /// replica's load opens no kind; without them its tag wrapped another
+ /// way and its yank took nothing.
+ tail: []const u8 = "",
+ selection: []const u8 = "",
+};
+
+pub fn slotWrite(s: *const Kind.Slot, gpa: std.mem.Allocator, w: *std.Io.Writer) !void {
+ var place: [32]u8 = undefined;
+ var r: SlotRecord = .{ .name = s.nameOf(), .version = s.version, .path = s.path, .art = s.art, .record = s.record, .where = s.where_text.slice(), .values = s.values.slice(), .live = s.live, .next_tick = s.next_tick, .views = s.views, .focused = s.focused, .cancelled = s.cancelled, .prefix = s.prefix.slice(), .refused = s.refused.slice(), .tail = s.tail.slice(), .selection = s.selection };
+ var owned: ?[]u8 = null;
+ defer if (owned) |o| s.gpa.free(o);
+ if (s.ops != null) {
+ r.where = Kind.where(@constCast(s), &place);
+ owned = try slotRecord(@constCast(s));
+ r.record = owned;
+ }
+ const bytes = try walk.save(SlotRecord, gpa, &r);
+ defer gpa.free(bytes);
+ try w.writeAll(bytes);
+}
+
+/// The kind's record of a slot's state (`s.gpa`'s), or null for none: at
+/// most PA_RECORD_MAX bytes.
+pub fn slotRecord(s: *Kind.Slot) !?[]u8 {
+ const f = (s.ops orelse return null).dump orelse return null;
+ const buf = try s.gpa.alloc(u8, Kind.c.PA_RECORD_MAX);
+ const n = Kind.dumpCall(s, f, buf);
+ if (n == 0 or n > buf.len) {
+ s.gpa.free(buf);
+ return null;
+ }
+ return s.gpa.realloc(buf, n) catch buf[0..n];
+}
+
+/// The pane back from its record, a placeholder holding it until its
+/// kind opens it (`revivePane`; a replica's core keeps it a Shadow).
+pub fn slotRead(out: *Kind.Slot, gpa: std.mem.Allocator, bytes: []const u8) !void {
+ var arena: std.heap.ArenaAllocator = .init(gpa);
+ defer arena.deinit();
+ const r = try walk.load(SlotRecord, arena.allocator(), bytes, null);
+ out.* = .{ .gpa = gpa, .arena = .init(gpa), .version = r.version, .art = r.art, .live = r.live, .next_tick = r.next_tick, .views = r.views, .focused = r.focused, .cancelled = r.cancelled };
+ errdefer out.free();
+ @memcpy(out.name[0..@min(15, r.name.len)], r.name[0..@min(15, r.name.len)]);
+ out.path = try gpa.dupe(u8, r.path);
+ out.where_text.set(r.where);
+ out.prefix.set(r.prefix);
+ out.refused.set(r.refused);
+ out.tail.set(r.tail);
+ out.values.set(r.values);
+ out.selection = try gpa.dupe(u8, r.selection);
+ if (r.record) |rec| out.record = try gpa.dupe(u8, rec);
+}
+
+pub fn slotFree(s: *Kind.Slot, gpa: std.mem.Allocator) void {
+ _ = gpa;
+ s.free();
+}
+
+/// A loaded pane of a kind opened by its kind from its file as a Look
+/// reads it, then given its record, if this core has the kind; else, or
+/// refused (its file not here, its record not taken), a placeholder saying
+/// what it waits for, its record kept. One with no record reopens at its
+/// place with its words. A placeholder comes back at the next Look or
+/// watch of its path, or the load of its kind.
+pub fn reviveSlot(p: *Pardes, id: usize, s: *Kind.Slot) void {
+ const k = p.kinds.byName(s.nameOf()) orelse {
+ s.live = false;
+ return;
+ };
+ s.ops = p.kinds.ops[k];
+ s.gen = p.kinds.kinds[k].gen;
+ // A replica has no code to read it for: the authority's outcome is taped.
+ const bytes: ?[]u8 = if (s.ops != null and s.path.len > 0) filesystem.read(p, s.path) catch null else null;
+ defer if (bytes) |b| p.gpa.free(b);
+ const kept = .{ s.next_tick, s.views, s.focused };
+ if (!Kind.reopen(p, id, s, bytes)) {
+ s.ops = null;
+ s.live = false;
+ } else if (s.ops == null) {
+ // A replica's Shadow is the authority's pane as it is: it ticks
+ // when that one does, not because it was loaded (a kind opened
+ // here ticks to lay itself out).
+ s.next_tick, s.views, s.focused = kept;
+ }
+}
+
+/// A PDF pane of a dump from before the pdf addon: a pane of the addon's
+/// kind `pdf` on its file, at its page, its fit and tint given again as
+/// its words; a placeholder until the addon is loaded.
+fn revivePdf(p: *Pardes, id: usize, pane: *pardes.panes.Pane, old: *pardes.panes.Pane.LegacyPdf) void {
+ defer {
+ p.image_gpa.free(old.path);
+ p.image_gpa.destroy(old);
+ pane.pdf = null;
+ }
+ const s = p.image_gpa.create(Kind.Slot) catch return;
+ s.* = .{ .gpa = p.image_gpa, .arena = .init(p.image_gpa), .version = 1, .live = true };
+ @memcpy(s.name[0..3], "pdf");
+ s.path = p.image_gpa.dupe(u8, old.path) catch {
+ s.free();
+ return p.image_gpa.destroy(s);
+ };
+ _ = s.where_text.print("{d}", .{old.page + 1});
+ _ = s.values.print("PdfFit {t}\nPdfTint {t}\n", .{ old.fit, old.tint });
+ pane.addon = s;
+ reviveSlot(p, id, s);
+}
+
+test "a DumpDir reads back absolute, however it was written" {
+ var buf: [4096]u8 = undefined;
+ try std.testing.expectEqualStrings("/tmp/dumps", directory(&buf, "/tmp/dumps/").?);
+ try std.testing.expectEqualStrings("/tmp/dumps", directory(&buf, "/tmp//x/../dumps").?);
+ const rel = directory(&buf, "rel/");
+ try std.testing.expect(std.Io.Dir.path.isAbsolute(rel.?) and std.mem.endsWith(u8, rel.?, "/rel"));
+}
+
test "a dump keeps the session's settings, and a restore applies them" {
const gpa = std.testing.allocator;
const p = try Pardes.init(gpa, .{ .tty_only = true });
@@ -814,7 +949,7 @@ test "a loaded terminal's history stays above its banner, the cursor where it wa
const text = try term.screenTextAlloc(q.panes[0].?, gpa);
defer gpa.free(text);
try std.testing.expect(std.mem.indexOf(u8, text, "one\ntwo\n") != null);
- try std.testing.expect(std.mem.indexOf(u8, text, "two") .? < std.mem.indexOf(u8, text, "restored history").?);
+ try std.testing.expect(std.mem.indexOf(u8, text, "two").? < std.mem.indexOf(u8, text, "restored history").?);
}
test "an upgraded session's dump keeps its views and its running terminal as they were, and settings the startup config would change" {
@@ -939,138 +1074,3 @@ test "an old dump's file, a byte dump, is read by -l and by a Restore of its pat
defer gpa.free(again);
try std.testing.expectEqualSlices(u8, bytes, again);
}
-
-// ---- a pane of a kind (Kind.Slot) ----
-
-/// What a dump keeps of a pane of a kind: the kind, its path, its place,
-/// the art bytes, its words' last values, whether its kind was here, and
-/// the kind's own record when it has one.
-const SlotRecord = struct {
- name: []const u8 = "",
- version: u32 = 0,
- path: []const u8 = "",
- where: []const u8 = "",
- art: @import("surface.zig").Placement.Art = .{},
- record: ?[]const u8 = null,
- values: []const u8 = "",
- live: bool = false,
- /// What decides its next calls (Kind.step): a replica joining keeps
- /// them, its Shadow calling where the authority's kind does.
- next_tick: u64 = 0,
- views: [views.max]u64 = @splat(0),
- focused: bool = false,
- /// The jobs whose answers it dropped (api.cancel): a replica's drop them too.
- cancelled: [4]u32 = @splat(0),
- /// Its tick's prefix: a replica's tag laid out as the session's.
- prefix: []const u8 = "",
- /// A placeholder's why (its file not here).
- refused: []const u8 = "",
- /// What a step reads of it that its kind's open made (Kind.reopen) or
- /// a call set: its tag's default words and what it has selected. A
- /// replica's load opens no kind; without them its tag wrapped another
- /// way and its yank took nothing.
- tail: []const u8 = "",
- selection: []const u8 = "",
-};
-
-pub fn slotWrite(s: *const Kind.Slot, gpa: std.mem.Allocator, w: *std.Io.Writer) !void {
- var place: [32]u8 = undefined;
- var r: SlotRecord = .{ .name = s.nameOf(), .version = s.version, .path = s.path, .art = s.art, .record = s.record, .where = s.where_text.slice(), .values = s.values.slice(), .live = s.live, .next_tick = s.next_tick, .views = s.views, .focused = s.focused, .cancelled = s.cancelled, .prefix = s.prefix.slice(), .refused = s.refused.slice(), .tail = s.tail.slice(), .selection = s.selection };
- var owned: ?[]u8 = null;
- defer if (owned) |o| s.gpa.free(o);
- if (s.ops != null) {
- r.where = Kind.where(@constCast(s), &place);
- owned = try slotRecord(@constCast(s));
- r.record = owned;
- }
- const bytes = try walk.save(SlotRecord, gpa, &r);
- defer gpa.free(bytes);
- try w.writeAll(bytes);
-}
-
-/// The kind's record of a slot's state (`s.gpa`'s), or null for none: at
-/// most PA_RECORD_MAX bytes.
-pub fn slotRecord(s: *Kind.Slot) !?[]u8 {
- const f = (s.ops orelse return null).dump orelse return null;
- const buf = try s.gpa.alloc(u8, Kind.c.PA_RECORD_MAX);
- const n = Kind.dumpCall(s, f, buf);
- if (n == 0 or n > buf.len) {
- s.gpa.free(buf);
- return null;
- }
- return s.gpa.realloc(buf, n) catch buf[0..n];
-}
-
-/// The pane back from its record, a placeholder holding it until its
-/// kind opens it (`revivePane`; a replica's core keeps it a Shadow).
-pub fn slotRead(out: *Kind.Slot, gpa: std.mem.Allocator, bytes: []const u8) !void {
- var arena: std.heap.ArenaAllocator = .init(gpa);
- defer arena.deinit();
- const r = try walk.load(SlotRecord, arena.allocator(), bytes, null);
- out.* = .{ .gpa = gpa, .arena = .init(gpa), .version = r.version, .art = r.art, .live = r.live, .next_tick = r.next_tick, .views = r.views, .focused = r.focused, .cancelled = r.cancelled };
- errdefer out.free();
- @memcpy(out.name[0..@min(15, r.name.len)], r.name[0..@min(15, r.name.len)]);
- out.path = try gpa.dupe(u8, r.path);
- out.where_text.set(r.where);
- out.prefix.set(r.prefix);
- out.refused.set(r.refused);
- out.tail.set(r.tail);
- out.values.set(r.values);
- out.selection = try gpa.dupe(u8, r.selection);
- if (r.record) |rec| out.record = try gpa.dupe(u8, rec);
-}
-
-pub fn slotFree(s: *Kind.Slot, gpa: std.mem.Allocator) void {
- _ = gpa;
- s.free();
-}
-
-/// A loaded pane of a kind opened by its kind from its file as a Look
-/// reads it, then given its record, if this core has the kind; else, or
-/// refused (its file not here, its record not taken), a placeholder saying
-/// what it waits for, its record kept. One with no record reopens at its
-/// place with its words. A placeholder comes back at the next Look or
-/// watch of its path, or the load of its kind.
-pub fn reviveSlot(p: *Pardes, id: usize, s: *Kind.Slot) void {
- const k = p.kinds.byName(s.nameOf()) orelse {
- s.live = false;
- return;
- };
- s.ops = p.kinds.ops[k];
- s.gen = p.kinds.kinds[k].gen;
- // A replica has no code to read it for: the authority's outcome is taped.
- const bytes: ?[]u8 = if (s.ops != null and s.path.len > 0) filesystem.read(p, s.path) catch null else null;
- defer if (bytes) |b| p.gpa.free(b);
- const kept = .{ s.next_tick, s.views, s.focused };
- if (!Kind.reopen(p, id, s, bytes)) {
- s.ops = null;
- s.live = false;
- } else if (s.ops == null) {
- // A replica's Shadow is the authority's pane as it is: it ticks
- // when that one does, not because it was loaded (a kind opened
- // here ticks to lay itself out).
- s.next_tick, s.views, s.focused = kept;
- }
-}
-
-/// A PDF pane of a dump from before the pdf addon: a pane of the addon's
-/// kind `pdf` on its file, at its page, its fit and tint given again as
-/// its words; a placeholder until the addon is loaded.
-fn revivePdf(p: *Pardes, id: usize, pane: *pardes.panes.Pane, old: *pardes.panes.Pane.LegacyPdf) void {
- defer {
- p.image_gpa.free(old.path);
- p.image_gpa.destroy(old);
- pane.pdf = null;
- }
- const s = p.image_gpa.create(Kind.Slot) catch return;
- s.* = .{ .gpa = p.image_gpa, .arena = .init(p.image_gpa), .version = 1, .live = true };
- @memcpy(s.name[0..3], "pdf");
- s.path = p.image_gpa.dupe(u8, old.path) catch {
- s.free();
- return p.image_gpa.destroy(s);
- };
- _ = s.where_text.print("{d}", .{old.page + 1});
- _ = s.values.print("PdfFit {t}\nPdfTint {t}\n", .{ old.fit, old.tint });
- pane.addon = s;
- reviveSlot(p, id, s);
-}