summaryrefslogtreecommitdiff
path: root/src/recent.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-24 13:47:11 -0300
committerGabriel Schneider <[email protected]>2026-10-24 15:43:44 -0300
commitf3405aaef8db79094b2741a9caaeaa5328ab6288 (patch)
tree5130b95da1d545a5f669710feca9bfa4dfba1a98 /src/recent.zig
parent9db48c4c8a9b2d239680a59d451cd8c1e290ff56 (diff)
downloadpardes-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/recent.zig')
-rw-r--r--src/recent.zig99
1 files changed, 81 insertions, 18 deletions
diff --git a/src/recent.zig b/src/recent.zig
index 112cb59f..2e3aabde 100644
--- a/src/recent.zig
+++ b/src/recent.zig
@@ -1,9 +1,11 @@
//! The files opened lately, most recent first, those since closed too, as
//! `Recent` lists them and a jump to a closed pane reopens them: a path
-//! and where the dot was when it closed. Kept across sessions in a small
-//! file under $XDG_STATE_HOME/pardes (Options.recent_path) when the host
-//! gives one. acme has nothing like it (its dump and Load are the nearest):
-//! a file closed by accident, its path forgotten, is found here again.
+//! and where the dot was when it closed. Kept across sessions, and shared
+//! by every window, in the store (Store.zig) when the host gives one: each
+//! change is a record there, and the records other windows wrote are
+//! `note`d here as they are read. acme has nothing like it (its dump and
+//! Load are the nearest): a file closed by accident, its path forgotten,
+//! is found here again.
const std = @import("std");
@@ -22,11 +24,14 @@ pub const Entry = struct {
/// cap drops. ponytail: a pane renamed away leaves its old path marked
/// open until the session ends; kept rather than tracked by pane.
open: bool = false,
+ /// Changed here since the store last heard of it: written out as a
+ /// record at the end of the step (Store.flush).
+ changed: bool = false,
};
pub const List = struct {
entries: std.ArrayList(Entry) = .empty,
- /// Changed since it was last written out.
+ /// Some entry is `changed`.
dirty: bool = false,
pub fn deinit(l: *List, gpa: std.mem.Allocator) void {
@@ -46,14 +51,8 @@ pub const List = struct {
.path = gpa.dupe(u8, path) catch return,
};
entry.open = true;
- // The oldest closed file goes for it; an open one never does.
- if (l.entries.items.len >= cap) {
- var i = l.entries.items.len;
- while (i > 0) : (i -= 1) if (!l.entries.items[i - 1].open) {
- gpa.free(l.entries.orderedRemove(i - 1).path);
- break;
- };
- }
+ entry.changed = true;
+ l.dropOldest(gpa);
l.entries.insert(gpa, 0, entry) catch {
gpa.free(entry.path);
entry = undefined;
@@ -80,13 +79,58 @@ pub const List = struct {
l.entries.items[i].aline = aline;
l.entries.items[i].acol = acol;
l.entries.items[i].open = false;
+ l.entries.items[i].changed = true;
l.dirty = true;
}
+ /// Full: the oldest closed file goes, to make room for one more; an
+ /// open one never does.
+ fn dropOldest(l: *List, gpa: std.mem.Allocator) void {
+ if (l.entries.items.len < cap) return;
+ var i = l.entries.items.len;
+ while (i > 0) : (i -= 1) if (!l.entries.items[i - 1].open) {
+ gpa.free(l.entries.orderedRemove(i - 1).path);
+ return;
+ };
+ }
+
+ /// A record read from the store (another window's, or this one's read
+ /// again): `path` was opened or closed there last, at that place. It
+ /// goes first, with that place; whether it is open HERE stays as it
+ /// was, and nothing is changed for the store to hear again.
+ pub fn note(l: *List, gpa: std.mem.Allocator, path: []const u8, line: u32, col: u32, aline: u32, acol: u32) void {
+ if (path.len == 0) return;
+ var entry: Entry = if (l.find(path)) |i| l.entries.orderedRemove(i) else blk: {
+ l.dropOldest(gpa);
+ break :blk .{ .path = gpa.dupe(u8, path) catch return };
+ };
+ entry.line = line;
+ entry.col = col;
+ entry.aline = aline;
+ entry.acol = acol;
+ l.entries.insert(gpa, 0, entry) catch {
+ gpa.free(entry.path);
+ entry = undefined;
+ };
+ }
+
+ /// This window's changes first again, in their order, after others'
+ /// records were noted over them: they are written after those, so
+ /// that is where every window's fold puts them.
+ pub fn changedFirst(l: *List) void {
+ var w: usize = 0;
+ for (0..l.entries.items.len) |i| if (l.entries.items[i].changed) {
+ const e = l.entries.items[i];
+ std.mem.copyBackwards(Entry, l.entries.items[w + 1 .. i + 1], l.entries.items[w..i]);
+ l.entries.items[w] = e;
+ w += 1;
+ };
+ }
+
/// A copy, for the session a Restore makes: its files closed until it
/// opens them again. Out of memory, what was copied so far.
pub fn clone(l: *const List, gpa: std.mem.Allocator) List {
- var copy: List = .{ .dirty = true };
+ var copy: List = .{ .dirty = l.dirty };
for (l.entries.items) |e| {
const path = gpa.dupe(u8, e.path) catch break;
var kept = e;
@@ -105,14 +149,19 @@ pub const List = struct {
}
/// One line a file, `<line> <col> <aline> <acol> <path>`, most recent
- /// first.
+ /// first: the old `recent` file, and a store record's body, a line.
pub fn write(l: *const List, w: *std.Io.Writer) !void {
+ for (l.entries.items) |e| try writeEntry(e, w);
+ }
+
+ pub fn writeEntry(e: Entry, w: *std.Io.Writer) !void {
// A name with a newline in it would read back as two lines: not kept.
- for (l.entries.items) |e| if (std.mem.indexOfScalar(u8, e.path, '\n') == null)
+ if (std.mem.indexOfScalar(u8, e.path, '\n') == null)
try w.print("{d} {d} {d} {d} {s}\n", .{ e.line, e.col, e.aline, e.acol, e.path });
}
- /// The lines `write` wrote; a line that is not one is passed over.
+ /// The lines `write` wrote (the old `recent` file), each changed for
+ /// the store to hear of; a line that is not one is passed over.
pub fn read(l: *List, gpa: std.mem.Allocator, bytes: []const u8) void {
var lines = std.mem.splitScalar(u8, bytes, '\n');
while (lines.next()) |line| {
@@ -131,11 +180,25 @@ pub const List = struct {
const path = rest;
if (path.len == 0 or path[0] != '/' or l.find(path) != null) continue;
const owned = gpa.dupe(u8, path) catch return;
- l.entries.append(gpa, .{ .path = owned, .line = nums[0], .col = nums[1], .aline = nums[2], .acol = nums[3] }) catch {
+ l.entries.append(gpa, .{ .path = owned, .line = nums[0], .col = nums[1], .aline = nums[2], .acol = nums[3], .changed = true }) catch {
gpa.free(owned);
return;
};
+ l.dirty = true;
+ }
+ }
+
+ /// One record's body read back: `<line> <col> <aline> <acol> <path>`.
+ pub fn parseLine(line: []const u8) ?struct { nums: [4]u32, path: []const u8 } {
+ var nums: [4]u32 = @splat(0);
+ var rest = line;
+ for (&nums) |*n| {
+ const sp = std.mem.indexOfScalar(u8, rest, ' ') orelse return null;
+ n.* = std.fmt.parseInt(u32, rest[0..sp], 10) catch return null;
+ rest = rest[sp + 1 ..];
}
+ if (rest.len == 0 or rest[0] != '/') return null;
+ return .{ .nums = nums, .path = rest };
}
};