summaryrefslogtreecommitdiff
path: root/src/recent.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-04 23:28:52 -0300
committerGabriel Schneider <[email protected]>2026-11-05 01:00:59 -0300
commit43739b4eb99f2b68ec2368e6ec69491b2d9ab0b4 (patch)
tree13a088e2ab1629b520eb178e2ff972aab8675c74 /src/recent.zig
parentb6e7d53f6700988c33266b20eb3505bc3471451b (diff)
downloadpardes-43739b4eb99f2b68ec2368e6ec69491b2d9ab0b4.tar.gz
pardes-43739b4eb99f2b68ec2368e6ec69491b2d9ab0b4.zip
recent: the old `recent` file's import goes (List.read and Store.load's branch for a store not made yet, the store in use since 2026-10-24), and with it List.write, the format's other half only its test used; closed takes the selection's anchor itself, closedSel its only caller's spelling; recent.zig's test reads a record back through parseLine, docs/next-store.md says the import went
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/recent.zig')
-rw-r--r--src/recent.zig68
1 files changed, 14 insertions, 54 deletions
diff --git a/src/recent.zig b/src/recent.zig
index 2e3aabde..b8e275f5 100644
--- a/src/recent.zig
+++ b/src/recent.zig
@@ -61,14 +61,10 @@ pub const List = struct {
l.dirty = true;
}
- /// `path` closed with its dot at `line`:`col`: where a reopen lands.
- /// Its place in the list is its opening's.
- pub fn closed(l: *List, gpa: std.mem.Allocator, path: []const u8, line: u32, col: u32) void {
- l.closedSel(gpa, path, line, col, 0, 0);
- }
-
- /// `closed`, with the selection's anchor too (0 0 for none).
- pub fn closedSel(l: *List, gpa: std.mem.Allocator, path: []const u8, line: u32, col: u32, aline: u32, acol: u32) void {
+ /// `path` closed with its dot at `line`:`col` and its selection's anchor
+ /// at `aline`:`acol` (0 0 for none): where a reopen lands. Its place in
+ /// the list is its opening's.
+ pub fn closed(l: *List, gpa: std.mem.Allocator, path: []const u8, line: u32, col: u32, aline: u32, acol: u32) void {
const i = l.find(path) orelse found: {
l.opened(gpa, path);
// Out of memory for it: nothing to note the dot on.
@@ -148,46 +144,13 @@ pub const List = struct {
return l.entries.items[l.find(path) orelse return null];
}
- /// One line a file, `<line> <col> <aline> <acol> <path>`, most recent
- /// 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);
- }
-
+ /// A store record's body, `<line> <col> <aline> <acol> <path>`, a line.
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.
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 (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| {
- if (l.entries.items.len >= cap) break;
- // Numbers, then the path (which starts with `/`): two of them in
- // a list written before the anchor was kept, four since.
- var nums: [4]u32 = @splat(0);
- var n: usize = 0;
- var rest = line;
- while (rest.len > 0 and rest[0] != '/' and n < nums.len) : (n += 1) {
- const sp = std.mem.indexOfScalar(u8, rest, ' ') orelse break;
- nums[n] = std.fmt.parseInt(u32, rest[0..sp], 10) catch break;
- rest = rest[sp + 1 ..];
- }
- if (n != 2 and n != 4) continue;
- 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], .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);
@@ -208,28 +171,25 @@ test "the list keeps a path once, most recent first, its dot from closing, cappe
defer l.deinit(gpa);
l.opened(gpa, "/a");
l.opened(gpa, "/b");
- l.closed(gpa, "/a", 7, 3);
+ l.closed(gpa, "/a", 7, 3, 0, 0);
l.opened(gpa, "/a");
try std.testing.expectEqualStrings("/a", l.entries.items[0].path);
try std.testing.expectEqual(@as(usize, 2), l.entries.items.len);
try std.testing.expectEqual(@as(u32, 7), l.get("/a").?.line);
var buf: [256]u8 = undefined;
var w = std.Io.Writer.fixed(&buf);
- try l.write(&w);
+ for (l.entries.items) |e| try List.writeEntry(e, &w);
try std.testing.expectEqualStrings("7 3 0 0 /a\n0 0 0 0 /b\n", w.buffered());
- var again: List = .{};
- defer again.deinit(gpa);
- again.read(gpa, w.buffered());
- again.read(gpa, "junk\n");
- // A list written before the anchor was kept still reads.
- again.read(gpa, "4 2 /old\n");
- try std.testing.expectEqual(@as(u32, 4), again.get("/old").?.line);
- try std.testing.expectEqual(@as(usize, 3), again.entries.items.len);
+ // Each line a store record's body, read back.
+ const back = List.parseLine("7 3 0 0 /a").?;
+ try std.testing.expectEqual(@as(u32, 7), back.nums[0]);
+ try std.testing.expectEqualStrings("/a", back.path);
+ try std.testing.expect(List.parseLine("junk") == null);
for (0..cap + 5) |i| {
var name: [16]u8 = undefined;
const path = std.fmt.bufPrint(&name, "/f{d}", .{i}) catch unreachable;
l.opened(gpa, path);
- l.closed(gpa, path, 1, 1);
+ l.closed(gpa, path, 1, 1, 0, 0);
}
try std.testing.expectEqual(@as(usize, cap), l.entries.items.len);
}
@@ -242,7 +202,7 @@ test "the cap drops the oldest closed file, never an open one" {
// The oldest is open; the next is closed.
l.opened(gpa, "/open");
l.opened(gpa, "/shut");
- l.closed(gpa, "/shut", 1, 1);
+ l.closed(gpa, "/shut", 1, 1, 0, 0);
for (2..cap) |i| l.opened(gpa, std.fmt.bufPrint(&name, "/f{d}", .{i}) catch unreachable);
try std.testing.expectEqual(@as(usize, cap), l.entries.items.len);
l.opened(gpa, "/new");