diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-24 13:47:11 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-24 15:43:44 -0300 |
| commit | f3405aaef8db79094b2741a9caaeaa5328ab6288 (patch) | |
| tree | 5130b95da1d545a5f669710feca9bfa4dfba1a98 /src/dump.zig | |
| parent | 9db48c4c8a9b2d239680a59d451cd8c1e290ff56 (diff) | |
| download | pardes-f3405aaef8db79094b2741a9caaeaa5328ab6288.tar.gz pardes-f3405aaef8db79094b2741a9caaeaa5328ab6288.zip | |
Store 2/3: one bounded store every window shares (Store.zig): Recent as records read on use, the panes a session closes kept as one-pane dumps; Panes lists them (closed), Enter or a look at @pN brings one back beside the pane that took its rows
An append-only log under $XDG_STATE_HOME/pardes/store, framed records with
a CRC, flock on store.lock (never blocking a step), a stat on use, garbage
passed over and compacted away by the next writer, compaction by rename,
100 MiB cap. dump.dumpPane/restorePane lifted out of dumpState/initDump;
layout.giveBack; Options.recent_path is store_path. Host calls yield one at
a time; /recent refreshes only when no step is out. Tests: two windows and a
busy lock, the cap, garbage/torn/partial records, a closed pane's reopen
with the layout invariant; fs.py: two live sessions over 9P.
Diffstat (limited to 'src/dump.zig')
| -rw-r--r-- | src/dump.zig | 216 |
1 files changed, 116 insertions, 100 deletions
diff --git a/src/dump.zig b/src/dump.zig index 0ce44c40..140bc476 100644 --- a/src/dump.zig +++ b/src/dump.zig @@ -717,34 +717,7 @@ pub fn dumpState(p: *Pardes) !void { for (p.panes, 0..) |slot, id| { const pane = slot orelse continue; slot_to_pane[id] = panes_len; - const tag = try tagline.tagText(p, arena, pane); - const body = try body_layer.bodyText(p, arena, pane); - const scroll: usize = @intCast(@max(0, pane.scroll())); - var dp: Pane = if (pane.file) |*f| try pardes.panes.File.dumpPane( - p, - arena, - pane, - f, - tag, - body, - scroll, - if (f.output) |o| pardes.panes.Output.word(o.from) else "", - if (f.output) |*o| o.arg() else "", - ) else if (pane.hasPdf()) blk: { - if (comptime !pdf_enabled) unreachable; - const pv = &pane.pdf.?; - break :blk try pardes.panes.Image.dumpPane(p, arena, pane, tag, body, pv.page, pv.path, &.{}); - } else if (pane.image) |iv| - try pardes.panes.Image.dumpPane(p, arena, pane, tag, body, scroll, iv.path, iv.raw) - else - try pardes.panes.terminal.dumpPane(pane, arena, tag, body, scroll); - dp.tag_tail = pane.tag.own; - if (dp.file) |*df| { - const dot = ctlfs.pane.dotOf(pane); - df.dot = .{ dot.q0, dot.q1 }; - } - dp.serial = pane.serial; - dump_panes[panes_len] = dp; + dump_panes[panes_len] = try dumpPane(p, arena, pane); panes_len += 1; } // An attachment names its owner by its place in the dump: serials are @@ -814,6 +787,39 @@ pub fn dumpState(p: *Pardes) !void { p.emit(.write_dump); } +/// One pane as a dump keeps it, its strings in `arena`: a Restore's, and a +/// closed pane's in the store (Store.zig), which Panes brings back. +pub fn dumpPane(p: *Pardes, arena: std.mem.Allocator, pane: *pardes.panes.Pane) !Pane { + const tag = try tagline.tagText(p, arena, pane); + const body = try body_layer.bodyText(p, arena, pane); + const scroll: usize = @intCast(@max(0, pane.scroll())); + var dp: Pane = if (pane.file) |*f| try pardes.panes.File.dumpPane( + p, + arena, + pane, + f, + tag, + body, + scroll, + if (f.output) |o| pardes.panes.Output.word(o.from) else "", + if (f.output) |*o| o.arg() else "", + ) else if (pane.hasPdf()) blk: { + if (comptime !pdf_enabled) unreachable; + const pv = &pane.pdf.?; + break :blk try pardes.panes.Image.dumpPane(p, arena, pane, tag, body, pv.page, pv.path, &.{}); + } else if (pane.image) |iv| + try pardes.panes.Image.dumpPane(p, arena, pane, tag, body, scroll, iv.path, iv.raw) + else + try pardes.panes.terminal.dumpPane(pane, arena, tag, body, scroll); + dp.tag_tail = pane.tag.own; + if (dp.file) |*df| { + const dot = ctlfs.pane.dotOf(pane); + df.dot = .{ dot.q0, dot.q1 }; + } + dp.serial = pane.serial; + return dp; +} + /// The setting lines that differ from a fresh session's, as the root ctl /// reads them, less the ones a dump keeps apart. fn changedSettings(p: *Pardes, arena: std.mem.Allocator) ![]const []const u8 { @@ -901,7 +907,8 @@ fn initDump(gpa: std.mem.Allocator, opts: Options, zon_bytes: []const u8, previo if (old.last_dump) |d| p.last_dump = try gpa.dupe(u8, d); // Recent goes on across a Restore, the old session's own list. p.recent = old.recent.clone(p.recentGpa()); - p.recent_loaded = old.recent_loaded; + // The same session: its closed panes go on being its own. + p.store = old.store.clone(p.recentGpa()); } else p.loadRecent(); var parsed = try readZon(gpa, zon_bytes, "load"); defer parsed.deinit(); @@ -944,77 +951,7 @@ fn initDump(gpa: std.mem.Allocator, opts: Options, zon_bytes: []const u8, previo for (st.panes, 0..) |src, i| { if (i >= MAX_PANES) break; if (old_serials) |o| o.panes[i] = src.serial; - const pane: *pardes.panes.Pane = switch (src.kind) { - .terminal => terminal: { - const t = src.terminal.?; - // A host that runs shells gets them back: the old history - // above a new shell in the directory it was in. The web - // shell and an isolated build only ever replay it. - // A command pane comes back finished, showing what it said - // and how it ended: its command is not run again. - const live = comptime (terminal_panes and platform != .web and !isolated); - const revive = live and t.cwd.len <= effect_path_cap and t.command.len == 0; - const restored = try pardes.panes.terminal.restore(p, src, revive); - p.installPane(i, restored); - try restored.setOwnedCwd(t.cwd); - if (t.shell.len > 0) restored.shell = try gpa.dupe(u8, t.shell); - if (t.command.len > 0) { - restored.command = try gpa.dupe(u8, t.command); - restored.command_done = true; - restored.command_status = t.status; - } - if (revive) p.emitSpawn(i, restored.serial, t.cwd); - if (t.petscii and pardes.panes.terminal.petscii(restored) == false) pardes.panes.terminal.togglePetscii(restored); - break :terminal restored; - }, - .file => file: { - const restored = try pardes.panes.File.restore(p, i, src); - // A twin: on the earlier pane's buffer again. - if (src.file.?.twin_of) |j| if (j < i) pardes.panes.File.relink(p, j, i, src.file.?.dot); - break :file restored; - }, - .image => restore_image: { - const im = src.image.?; - if (pardes.panes.Pdf.isPath(im.path)) { - var raw: []u8 = if (im.bytes_b64.len > 0) - try decodeBytes(gpa, im.bytes_b64) - else - &.{}; - errdefer if (raw.len > 0) gpa.free(raw); - if (comptime pdf_enabled) { - if (pardes.panes.Pdf.openPane(p, i, im.path, src.scroll + 1, .last) catch null) |restored| { - if (raw.len > 0) gpa.free(raw); - raw = &.{}; - // Its fit and tint as they were (PdfFit, PdfTint). - const pv = &restored.pdf.?; - if (std.meta.stringToEnum(@TypeOf(pv.fit), im.pdf_fit)) |fit| if (fit != pv.fit) pardes.panes.Pdf.toggleFit(restored); - if (std.meta.stringToEnum(@TypeOf(pv.tint), im.pdf_tint)) |tint| while (pv.tint != tint) pardes.panes.Pdf.toggleTint(restored); - restored.cols = @max(1, src.cols); - restored.rows = @max(1, src.rows); - break :restore_image restored; - } - } - // If the source path is unavailable, retain its bytes - // as an ordinary file rather than dropping the pane. - const path = try gpa.dupe(u8, im.path); - errdefer gpa.free(path); - const content = if (raw.len > 0) raw else try gpa.dupe(u8, ""); - raw = &.{}; - errdefer if (content.len > 0) gpa.free(content); - const history = try pardes.panes.File.History.create(gpa); - errdefer gpa.destroy(history); - const restored = try p.newDocPane(i); - restored.file = .{ .path = path, .content = content, .history = history }; - restored.body.cur_pinned = true; - restored.cols = @max(1, src.cols); - restored.rows = @max(1, src.rows); - p.emit(.{ .watch = .{ .pane = @intCast(i), .on = true, .mode = .baseline_disk } }); - break :restore_image restored; - } - break :restore_image try pardes.panes.Image.restore(p, i, src); - }, - }; - tagline.restoreDumpTail(p, pane, src); + const pane = try restorePane(p, i, src); pane.vweight = src.vweight; pane.collapsed = src.collapsed; } @@ -1049,6 +986,85 @@ fn initDump(gpa: std.mem.Allocator, opts: Options, zon_bytes: []const u8, previo return p; } +/// Dump pane `src` made a pane again in slot `i`, installed (a fresh +/// serial) and not yet placed: a Restore's, and a closed pane Panes brings +/// back (Store.zig). +pub fn restorePane(p: *Pardes, i: usize, src: Pane) !*pardes.panes.Pane { + const gpa = p.gpa; + const pane: *pardes.panes.Pane = switch (src.kind) { + .terminal => terminal: { + const t = src.terminal.?; + // A host that runs shells gets them back: the old history + // above a new shell in the directory it was in. The web + // shell and an isolated build only ever replay it. + // A command pane comes back finished, showing what it said + // and how it ended: its command is not run again. + const live = comptime (terminal_panes and platform != .web and !isolated); + const revive = live and t.cwd.len <= effect_path_cap and t.command.len == 0; + const restored = try pardes.panes.terminal.restore(p, src, revive); + p.installPane(i, restored); + try restored.setOwnedCwd(t.cwd); + if (t.shell.len > 0) restored.shell = try gpa.dupe(u8, t.shell); + if (t.command.len > 0) { + restored.command = try gpa.dupe(u8, t.command); + restored.command_done = true; + restored.command_status = t.status; + } + if (revive) p.emitSpawn(i, restored.serial, t.cwd); + if (t.petscii and pardes.panes.terminal.petscii(restored) == false) pardes.panes.terminal.togglePetscii(restored); + break :terminal restored; + }, + .file => file: { + const restored = try pardes.panes.File.restore(p, i, src); + // A twin: on the earlier pane's buffer again. + if (src.file.?.twin_of) |j| if (j < i) pardes.panes.File.relink(p, j, i, src.file.?.dot); + break :file restored; + }, + .image => restore_image: { + const im = src.image.?; + if (pardes.panes.Pdf.isPath(im.path)) { + var raw: []u8 = if (im.bytes_b64.len > 0) + try decodeBytes(gpa, im.bytes_b64) + else + &.{}; + errdefer if (raw.len > 0) gpa.free(raw); + if (comptime pdf_enabled) { + if (pardes.panes.Pdf.openPane(p, i, im.path, src.scroll + 1, .last) catch null) |restored| { + if (raw.len > 0) gpa.free(raw); + raw = &.{}; + // Its fit and tint as they were (PdfFit, PdfTint). + const pv = &restored.pdf.?; + if (std.meta.stringToEnum(@TypeOf(pv.fit), im.pdf_fit)) |fit| if (fit != pv.fit) pardes.panes.Pdf.toggleFit(restored); + if (std.meta.stringToEnum(@TypeOf(pv.tint), im.pdf_tint)) |tint| while (pv.tint != tint) pardes.panes.Pdf.toggleTint(restored); + restored.cols = @max(1, src.cols); + restored.rows = @max(1, src.rows); + break :restore_image restored; + } + } + // If the source path is unavailable, retain its bytes + // as an ordinary file rather than dropping the pane. + const path = try gpa.dupe(u8, im.path); + errdefer gpa.free(path); + const content = if (raw.len > 0) raw else try gpa.dupe(u8, ""); + raw = &.{}; + errdefer if (content.len > 0) gpa.free(content); + const history = try pardes.panes.File.History.create(gpa); + errdefer gpa.destroy(history); + const restored = try p.newDocPane(i); + restored.file = .{ .path = path, .content = content, .history = history }; + restored.body.cur_pinned = true; + restored.cols = @max(1, src.cols); + restored.rows = @max(1, src.rows); + p.emit(.{ .watch = .{ .pane = @intCast(i), .on = true, .mode = .baseline_disk } }); + break :restore_image restored; + } + break :restore_image try pardes.panes.Image.restore(p, i, src); + }, + }; + tagline.restoreDumpTail(p, pane, src); + return pane; +} + test "a dump keeps the settings that differ from a fresh session's, and a restore applies them" { const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .tty_only = true }); |
