From f3405aaef8db79094b2741a9caaeaa5328ab6288 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sat, 24 Oct 2026 13:47:11 -0300 Subject: 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. --- src/Query.zig | 57 ++- src/Store.zig | 1193 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/dump.zig | 216 +++++----- src/layout.zig | 26 ++ src/look.zig | 8 +- src/main.zig | 13 +- src/ninep/tree.zig | 4 + src/pardes.zig | 49 +-- src/recent.zig | 99 ++++- 9 files changed, 1500 insertions(+), 165 deletions(-) create mode 100644 src/Store.zig (limited to 'src') diff --git a/src/Query.zig b/src/Query.zig index a871b71e..98419c7b 100644 --- a/src/Query.zig +++ b/src/Query.zig @@ -580,6 +580,8 @@ fn startSource(p: *Pardes, id: usize, q: *Query, source: Source, owner_id: ?usiz if (p.fs.serving) Stream.step(p); }, .recent => { + // Every window's files: what the others wrote since, read. + p.store.refresh(p); var out: std.Io.Writer.Allocating = .init(gpa); defer out.deinit(); Output.recentText(p, &out.writer, true) catch {}; @@ -591,15 +593,29 @@ fn startSource(p: *Pardes, id: usize, q: *Query, source: Source, owner_id: ?usiz q.names.add(gpa, rel, 0, path_end, Names.baseOf(rel[0..path_end]), 0) catch break; } }, - .panes => for (p.panes) |slot| { - const other = slot orelse continue; - if (other == rp or other.query != null) continue; - var buf: [4096 + 32]u8 = undefined; - const what: []const u8 = if (other.file) |f| f.path else if (other.command) |c| c else other.cwdSlice(); - const row = std.fmt.bufPrint(&buf, config.pane_addr ++ "{d} {s}", .{ other.serial, Names.relative(what, dir) }) catch continue; - const key_at = std.mem.indexOfScalar(u8, row, ' ').? + 1; - const ln = row[0 .. std.mem.indexOfAny(u8, row, "\r\n") orelse row.len]; - q.names.add(gpa, ln, key_at, ln.len -| key_at, Names.baseOf(ln[@min(key_at, ln.len)..]), other.serial) catch break; + .panes => { + for (p.panes) |slot| { + const other = slot orelse continue; + if (other == rp or other.query != null) continue; + var buf: [4096 + 32]u8 = undefined; + const what: []const u8 = if (other.file) |f| f.path else if (other.command) |c| c else other.cwdSlice(); + const row = std.fmt.bufPrint(&buf, config.pane_addr ++ "{d} {s}", .{ other.serial, Names.relative(what, dir) }) catch continue; + const key_at = std.mem.indexOfScalar(u8, row, ' ').? + 1; + const ln = row[0 .. std.mem.indexOfAny(u8, row, "\r\n") orelse row.len]; + q.names.add(gpa, ln, key_at, ln.len -| key_at, Names.baseOf(ln[@min(key_at, ln.len)..]), other.serial) catch break; + } + // Then the panes this session closed, the latest first, each + // `(closed)` and dimmer (makeNameRows): Enter brings one back + // where it was. Its name alone is matched, not the word. + var i = p.store.closed.items.len; + while (i > 0) : (i -= 1) { + const c = p.store.closed.items[i - 1]; + var buf: [4096 + 48]u8 = undefined; + const row = std.fmt.bufPrint(&buf, config.pane_addr ++ "{d} {s}" ++ closed_word, .{ c.serial, Names.relative(c.name, dir) }) catch continue; + const key_at = std.mem.indexOfScalar(u8, row, ' ').? + 1; + const key_len = row.len - closed_word.len - key_at; + q.names.add(gpa, row, key_at, key_len, Names.baseOf(row[key_at..][0..key_len]), c.serial) catch break; + } }, .symbols => { const oid = owner_id orelse return; @@ -622,6 +638,9 @@ fn startSource(p: *Pardes, id: usize, q: *Query, source: Source, owner_id: ?usiz } } +/// What a Panes row of a closed pane ends with. +const closed_word = " (closed)"; + /// The owner's declarations, from a parse that already exists: its sticky /// headers' (TreeContext) at this revision, or the locations cache's. None /// is made here, on the loop, unless `small` and the text is small: a @@ -1020,13 +1039,21 @@ fn makeNameRows(p: *Pardes, id: usize, pat: []const u8) !void { } try q.items.append(p.gpa, .{ .kind = .hit, .ref = cand.ref }); try writeRow(p, &out, &rows, row, meta); + // A closed pane's row is dimmer, the gutter's colour, between its + // marks (spans are walked in order, never overlapping). + const dim = q.source == .panes and p.paneBySerial(cand.ref) == null; + const dimmed: pardes.CellStyle = .{ .fg = .{ .rgb = chrome.lineno } }; + var gap: usize = 0; var k: usize = 0; while (k < nmarks) { var e = k + 1; while (e < nmarks and marks[e] == marks[e - 1] + 1) e += 1; + if (dim and marks[k] > gap) try spans.append(p.gpa, .{ .start = @intCast(at + gap), .end = @intCast(at + marks[k]), .style = dimmed }); try spans.append(p.gpa, .{ .start = @intCast(at + marks[k]), .end = @intCast(at + marks[e - 1] + 1), .style = tint }); + gap = marks[e - 1] + 1; k = e; } + if (dim and row.len > gap) try spans.append(p.gpa, .{ .start = @intCast(at + gap), .end = @intCast(at + row.len), .style = dimmed }); } if (q.names.hits.items.len > shown) try noteRow(p, q, &out, &rows, " ... and more: type more of the name"); if (q.source == .find) try skippedRow(p, q, rp, &out, &rows); @@ -1636,9 +1663,15 @@ pub fn land(p: *Pardes, id: usize, row: ?usize) void { look.focusPaneLine(p, oid, .{ .line = it.ref }, .center); return; }, - .panes => if (p.paneBySerial(it.ref)) |tid| { - p.active = tid; - if (layout.findPane(p, tid)) |at| p.setActiveColumn(at.col); + .panes => { + if (p.paneBySerial(it.ref)) |tid| { + p.active = tid; + if (layout.findPane(p, tid)) |at| p.setActiveColumn(at.col); + return; + } + // A pane this session closed: back where it was, from the + // pane that asked (the owner, not this one, which may go). + _ = p.store.reopen(p, it.ref, owner_id orelse id); return; }, else => {}, diff --git a/src/Store.zig b/src/Store.zig new file mode 100644 index 00000000..35033e04 --- /dev/null +++ b/src/Store.zig @@ -0,0 +1,1193 @@ +//! The store: the state every pardes window shares, one bounded file under +//! $XDG_STATE_HOME/pardes (Options.store_path), an append-only log of +//! framed records, `@ \n\n`: the files opened +//! lately (Recent, recent.zig) and the panes each session closed, each a +//! one-pane Dump, which Panes brings back where it was. docs/next-store.md +//! argues the format and the sharing; in short: +//! +//! - A writer takes `flock` on `store.lock` (never blocking a frame: busy, +//! its records wait for the next step), reads what others appended since +//! it last read, and appends its own with one write at the end. +//! - A reader takes no lock: on use, one stat of the path; longer, it reads +//! the tail; another inode (a compaction), all of it again. +//! - A record running past the end is being written: read next time. One +//! that is garbage is passed over to the next valid one, and the next +//! writer compacts. A missing, foreign or garbage file is an empty store: +//! pardes always starts. +//! - Compaction (under the lock): the live records, the closed panes of +//! sessions still running, into `store.new`, renamed over the store. The +//! file never passes `max_bytes`. +//! +//! With no path (no home, the web, a test that gives none) nothing is +//! kept past the session, and a closed pane's dump is held here instead. +const Store = @This(); + +const std = @import("std"); +const pardes = @import("pardes.zig"); +const dump = @import("dump.zig"); +const layout = @import("layout.zig"); +const exec = @import("exec.zig"); +const recent = @import("recent.zig"); +const Pardes = pardes.Pardes; +const Pane = pardes.Pane; + +/// The file is never longer: an append that would pass it compacts first, +/// and a compaction keeps the newest live records within half of it. +pub const max_bytes: u64 = 100 << 20; +/// A closed pane's record is never longer: a terminal's that would be is +/// kept without its history, any other pane's is not kept. +pub const max_record: usize = 8 << 20; +/// A session keeps this many closed panes, the newest. +pub const max_closed = 64; +/// The first record's body, its version; then a generation, new at each +/// compaction, so an inode number used again is no other file read on. +const magic = "pardes-store 1"; +/// A header line `@k \n` is never longer. +const max_head = 32; +/// A closed pane's first line, its place and name, is never longer. +const max_meta = 4096 + 128; + +/// A pane closed in this session, as Panes lists it and brings it back. +pub const Closed = struct { + serial: u32, + /// The column it was in, by serial. + column: u32, + /// The pane its rows went to, by serial, and whether it was below it: + /// brought back, it takes them from that pane alone. + gainer: u32, + below: bool, + /// The pane it was attached to, by serial (0 none). + owner: u32, + rows: u16, + /// What its Panes row shows: its file, command or directory (gpa). + name: []u8, + /// Its record's offset in the file read as `inode`. + at: u64 = 0, + /// Its record's body while it is in no file (not written yet, or no + /// file to write it to); empty once written. + body: []u8 = &.{}, + /// Where its record is in `pending`, until that is written. + pending_at: ?usize = null, +}; + +/// The store file; null, nothing is kept past the session. +path: ?[]const u8 = null, +/// Its bound: `max_bytes`, less in a test. +cap: u64 = max_bytes, +/// This session's, on its closed panes' records. +session: u64 = 0, +pid: i32 = 0, +/// The file as read so far: its inode, generation and how far. +inode: std.Io.File.INode = 0, +gen: u64 = 0, +end: u64 = 0, +/// Its length when first read whole: a file past twice that (and 1 MiB) +/// is compacted. +base: u64 = 0, +/// A record that is not one was passed over: the next writer compacts. +garbage: bool = false, +/// Read once at start (or there was none): only then is anything written, +/// so a session never writes over a store it did not read. +loaded: bool = false, +/// Framed records waiting for the lock. +pending: std.ArrayList(u8) = .empty, +/// This session's closed panes, oldest first. +closed: std.ArrayList(Closed) = .empty, + +fn clockNs() i128 { + var ts: std.c.timespec = .{ .sec = 0, .nsec = 0 }; + _ = std.c.clock_gettime(.REALTIME, &ts); + return @as(i128, ts.sec) * std.time.ns_per_s + ts.nsec; +} + +fn ioOf() std.Io { + return std.Io.Threaded.global_single_threaded.io(); +} + +/// One host file call, `f(args)`, made out of the turn (pardes.turn): a +/// store under a mount of this session's own tree is answered by this +/// session. Only the call: while a step is out nothing may change the core, +/// so the store and Recent change between these, never inside one. +fn host(comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? { + pardes.turn.yield(); + defer pardes.turn.back(); + return @call(.auto, f, args); +} + +const cwd = std.Io.Dir.cwd; +const File = std.Io.File; +const Dir = std.Io.Dir; + +/// A new session's store at `path`: its id from the pid and the clock. +pub fn init(path: ?[]const u8) Store { + var s: Store = .{ .path = path }; + if (comptime pardes.hosted) { + s.pid = std.c.getpid(); + const now = clockNs(); + s.session = std.hash.Wyhash.hash(@intCast(s.pid), std.mem.asBytes(&now)); + } else s.session = 1; + return s; +} + +pub fn deinit(s: *Store, gpa: std.mem.Allocator) void { + for (s.closed.items) |c| freeClosed(gpa, c); + s.closed.deinit(gpa); + s.pending.deinit(gpa); +} + +fn freeClosed(gpa: std.mem.Allocator, c: Closed) void { + gpa.free(c.name); + gpa.free(c.body); +} + +/// A copy for the session a Restore makes: the same session (the same +/// process), its closed panes still its own. What is pending is the old +/// session's to write (its deinit does); the copies hold their bodies +/// until they are read back from the file. +pub fn clone(s: *const Store, gpa: std.mem.Allocator) Store { + var copy = s.*; + copy.pending = .empty; + copy.closed = .empty; + for (s.closed.items) |c| { + const name = gpa.dupe(u8, c.name) catch break; + const body = gpa.dupe(u8, c.body) catch { + gpa.free(name); + break; + }; + var kept = c; + kept.name = name; + kept.body = body; + kept.pending_at = null; + copy.closed.append(gpa, kept) catch { + freeClosed(gpa, kept); + break; + }; + } + return copy; +} + +// ---- reading ---- + +/// A window of the file, read ahead: records are parsed from it, and a +/// body that is skipped is never read. +const Reader = struct { + file: std.Io.File, + io: std.Io, + size: u64, + buf: []u8, + at: u64 = 0, + len: usize = 0, + + /// Bytes `[from, from + n)`, `n` at most the window; null past the end. + fn bytes(r: *Reader, from: u64, n: usize) ?[]const u8 { + if (n > r.buf.len or from + n > r.size) return null; + if (from < r.at or from + n > r.at + r.len) { + r.len = host(File.readPositionalAll, .{ r.file, r.io, r.buf, from }) catch return null; + r.at = from; + if (n > r.len) return null; + } + const o: usize = @intCast(from - r.at); + return r.buf[o..][0..n]; + } +}; + +const Rec = struct { + kind: u8, + at: u64, + body: u64, + len: usize, + crc: u32, + + fn end(rec: Rec) u64 { + return rec.body + rec.len + 1; + } +}; + +const Next = union(enum) { rec: Rec, partial, bad }; + +/// The record at `at`: whole, still being written (`partial`: its length +/// runs past the end), or not a record. +fn next(r: *Reader, at: u64) Next { + if (at >= r.size) return .partial; + const head = r.bytes(at, @intCast(@min(max_head, r.size - at))) orelse return .partial; + const nl = std.mem.indexOfScalar(u8, head, '\n') orelse return if (head.len < max_head) .partial else .bad; + const line = head[0..nl]; + if (line.len < 5 or line[0] != '@' or line[2] != ' ') return .bad; + var fields = std.mem.splitScalar(u8, line[3..], ' '); + const len = std.fmt.parseInt(usize, fields.next() orelse return .bad, 10) catch return .bad; + const crc_text = fields.next() orelse return .bad; + if (crc_text.len != 8 or fields.next() != null or len > max_record + max_meta) return .bad; + const crc = std.fmt.parseInt(u32, crc_text, 16) catch return .bad; + const rec: Rec = .{ .kind = line[1], .at = at, .body = at + nl + 1, .len = len, .crc = crc }; + if (rec.end() > r.size) return .partial; + const tail = r.bytes(rec.end() - 1, 1) orelse return .partial; + if (tail[0] != '\n') return .bad; + return .{ .rec = rec }; +} + +/// Whether `rec`'s body is the one its CRC says. +fn verify(r: *Reader, rec: Rec) bool { + var crc = std.hash.Crc32.init(); + var at = rec.body; + var left = rec.len; + while (left > 0) { + const n = @min(left, r.buf.len); + crc.update(r.bytes(at, n) orelse return false); + at += n; + left -= n; + } + return crc.final() == rec.crc; +} + +/// After garbage: the next `\n@` that starts a whole, valid record (or one +/// still being written), else the end. +fn resync(r: *Reader, from: u64) u64 { + var at = from; + while (at + 1 < r.size) : (at += 1) { + const b = r.bytes(at, 2) orelse return r.size; + if (b[0] != '\n' or b[1] != '@') continue; + switch (next(r, at + 1)) { + .rec => |rec| if (verify(r, rec)) return at + 1, + .partial => return at + 1, + .bad => {}, + } + } + return r.size; +} + +/// A closed pane's first line: ` +/// \t`. +const Meta = struct { + session: u64, + pid: i32, + serial: u32, + column: u32, + gainer: u32, + below: bool, + owner: u32, + rows: u16, + name: []const u8, + /// Where its dump starts in the body. + dump_at: usize, +}; + +fn parseMeta(body: []const u8) ?Meta { + const nl = std.mem.indexOfScalar(u8, body, '\n') orelse return null; + const tab = std.mem.indexOfScalar(u8, body[0..nl], '\t') orelse return null; + var f = std.mem.splitScalar(u8, body[0..tab], ' '); + var m: Meta = undefined; + m.session = std.fmt.parseInt(u64, f.next() orelse return null, 16) catch return null; + m.pid = std.fmt.parseInt(i32, f.next() orelse return null, 10) catch return null; + m.serial = std.fmt.parseInt(u32, f.next() orelse return null, 10) catch return null; + m.column = std.fmt.parseInt(u32, f.next() orelse return null, 10) catch return null; + m.gainer = std.fmt.parseInt(u32, f.next() orelse return null, 10) catch return null; + m.below = (std.fmt.parseInt(u1, f.next() orelse return null, 10) catch return null) == 1; + m.owner = std.fmt.parseInt(u32, f.next() orelse return null, 10) catch return null; + m.rows = std.fmt.parseInt(u16, f.next() orelse return null, 10) catch return null; + if (f.next() != null) return null; + m.name = body[tab + 1 .. nl]; + m.dump_at = nl + 1; + return m; +} + +/// The store read at start; the old `recent` file beside it read into the +/// list once, when there is no store yet, so nobody's list is lost. +pub fn load(s: *Store, p: *Pardes) void { + const path = s.path orelse return; + if (comptime !pardes.hosted) return; + defer s.loaded = true; + const io = ioOf(); + const file = host(Dir.openFile, .{ cwd(), io, path, Dir.OpenFileOptions{} }) catch |err| { + if (err != error.FileNotFound) return; + const dir = std.fs.path.dirname(path) orelse return; + var buf: [4096 + 16]u8 = undefined; + const old = std.fmt.bufPrint(&buf, "{s}/recent", .{dir}) catch return; + const bytes = host(Dir.readFileAlloc, .{ cwd(), io, old, p.gpa, std.Io.Limit.limited(1 << 20) }) catch return; + defer p.gpa.free(bytes); + p.recent.read(p.recentGpa(), bytes); + return; + }; + defer host(File.close, .{ file, io }); + const st = host(File.stat, .{ file, io }) catch return; + s.readFile(p, file, st); +} + +/// On use: what other windows wrote since this one last read, read. One +/// stat when nothing changed. +pub fn refresh(s: *Store, p: *Pardes) void { + const path = s.path orelse return; + if (comptime !pardes.hosted) return; + if (!s.loaded) return; + const io = ioOf(); + const st = host(Dir.statFile, .{ cwd(), io, path, Dir.StatFileOptions{} }) catch return; + if (st.inode == s.inode and st.size == s.end) return; + const file = host(Dir.openFile, .{ cwd(), io, path, Dir.OpenFileOptions{} }) catch return; + defer host(File.close, .{ file, io }); + const fst = host(File.stat, .{ file, io }) catch return; + s.readFile(p, file, fst); +} + +/// `file` read on from where this session left it, or all of it again +/// when it is another file than the one read so far. +fn readFile(s: *Store, p: *Pardes, file: File, st: File.Stat) void { + const gpa = p.recentGpa(); + const buf = gpa.alloc(u8, 64 << 10) catch return; + defer gpa.free(buf); + var r: Reader = .{ .file = file, .io = ioOf(), .size = st.size, .buf = buf }; + var whole = st.inode != s.inode or st.size < s.end; + // The same inode number may be a new file (one compaction freed it, + // the next took it): its header's generation says. + if (!whole and s.end > 0) { + switch (next(&r, 0)) { + .rec => |rec| whole = rec.kind != 'h' or headerGen(&r, rec) != s.gen, + else => whole = true, + } + } + if (whole) { + s.inode = st.inode; + s.end = 0; + s.gen = 0; + s.base = st.size; + s.garbage = false; + // Its closed panes are read again from it, at their offsets there; + // the ones in no file yet stay. + var w: usize = 0; + for (s.closed.items) |c| { + if (c.body.len == 0) { + freeClosed(gpa, c); + continue; + } + s.closed.items[w] = c; + w += 1; + } + s.closed.shrinkRetainingCapacity(w); + } + if (st.size == s.end) return; + s.end = s.scan(p, &r, s.end); +} + +fn headerGen(r: *Reader, rec: Rec) ?u64 { + if (rec.len > 64) return null; + const b = r.bytes(rec.body, rec.len) orelse return null; + if (!std.mem.startsWith(u8, b, magic ++ " ")) return null; + return std.fmt.parseInt(u64, b[magic.len + 1 ..], 16) catch null; +} + +/// The records from `from` applied; where reading stopped (before a +/// record still being written, or the end). +fn scan(s: *Store, p: *Pardes, r: *Reader, from: u64) u64 { + var at = from; + while (at < r.size) { + switch (next(r, at)) { + .partial => return at, + .bad => { + s.garbage = true; + // A file that does not start as a store is none: empty. + if (at == 0) return r.size; + at = resync(r, at + 1); + }, + .rec => |rec| { + if (at == 0) { + s.gen = (if (rec.kind == 'h') headerGen(r, rec) else null) orelse { + s.garbage = true; + return r.size; + }; + } else if (!s.apply(p, r, rec)) s.garbage = true; + at = rec.end(); + }, + } + } + return at; +} + +/// One record read: noted in Recent, or a closed pane of this session's +/// listed or unlisted. False when it is not what its header says. +fn apply(s: *Store, p: *Pardes, r: *Reader, rec: Rec) bool { + const gpa = p.recentGpa(); + switch (rec.kind) { + 'r' => { + if (rec.len > 8192) return false; + const b = r.bytes(rec.body, rec.len) orelse return false; + if (std.hash.Crc32.hash(b) != rec.crc) return false; + const e = recent.List.parseLine(b) orelse return false; + p.recent.note(gpa, e.path, e.nums[0], e.nums[1], e.nums[2], e.nums[3]); + }, + 'c' => { + // Only its first line is read: a closed pane is this session's + // or none of its business, and its dump is read when it comes + // back (`reopen`, which checks the CRC). + const b = r.bytes(rec.body, @min(rec.len, max_meta)) orelse return false; + const m = parseMeta(b) orelse return false; + if (m.session != s.session) return true; + s.list(gpa, m, rec.at, &.{}); + }, + 'x' => { + if (rec.len > 64) return false; + const b = r.bytes(rec.body, rec.len) orelse return false; + if (std.hash.Crc32.hash(b) != rec.crc) return false; + const sp = std.mem.indexOfScalar(u8, b, ' ') orelse return false; + const session = std.fmt.parseInt(u64, b[0..sp], 16) catch return false; + const serial = std.fmt.parseInt(u32, b[sp + 1 ..], 10) catch return false; + if (session == s.session) s.unlist(gpa, serial); + }, + // A header in the middle is no record. + 'h' => return false, + // A kind a newer pardes writes: passed over. + else => {}, + } + return true; +} + +/// A closed pane listed: from the file (`body` empty, its record at `at`) +/// or not in one yet. In the order closed, the oldest going past +/// `max_closed`. +fn list(s: *Store, gpa: std.mem.Allocator, m: Meta, at: u64, body: []u8) void { + for (s.closed.items) |c| if (c.serial == m.serial) { + gpa.free(body); + return; + }; + const name = gpa.dupe(u8, m.name) catch { + gpa.free(body); + return; + }; + const c: Closed = .{ .serial = m.serial, .column = m.column, .gainer = m.gainer, .below = m.below, .owner = m.owner, .rows = m.rows, .name = name, .at = at, .body = body }; + if (s.closed.items.len >= max_closed) freeClosed(gpa, s.closed.orderedRemove(0)); + // One read from the file goes before those in none yet: they are newer. + var i = s.closed.items.len; + if (body.len == 0) while (i > 0 and s.closed.items[i - 1].body.len > 0) : (i -= 1) {}; + s.closed.insert(gpa, i, c) catch freeClosed(gpa, c); +} + +fn unlist(s: *Store, gpa: std.mem.Allocator, serial: u32) void { + for (s.closed.items, 0..) |c, i| if (c.serial == serial) { + freeClosed(gpa, s.closed.orderedRemove(i)); + return; + }; +} + +/// The closed pane with this serial, if this session has it. +pub fn closedBySerial(s: *const Store, serial: u32) ?*Closed { + for (s.closed.items) |*c| if (c.serial == serial) return c; + return null; +} + +// ---- writing ---- + +fn frame(gpa: std.mem.Allocator, out: *std.ArrayList(u8), kind: u8, parts: []const []const u8) !void { + var len: usize = 0; + var crc = std.hash.Crc32.init(); + for (parts) |part| { + len += part.len; + crc.update(part); + } + try out.print(gpa, "@{c} {d} {x:0>8}\n", .{ kind, len, crc.final() }); + for (parts) |part| try out.appendSlice(gpa, part); + try out.append(gpa, '\n'); +} + +/// Pane `id`, about to close, kept as a record: what it was (a one-pane +/// Dump) and where (its column, its rows, what it was attached to). Null +/// for a pane that is no pane of the user's: a query pane, a preview, one +/// made and refused in the same step (never announced), or one whose dump +/// fails or is too big. `close` finishes it once its rows have gone. +pub fn keep(s: *Store, p: *Pardes, id: usize) ?Kept { + _ = s; + const pane = p.panes[id] orelse return null; + if (pane.fs.unannounced or pane.query != null) return null; + if (pane.file) |f| if (f.output) |o| if (o.from == .preview) return null; + const gpa = p.recentGpa(); + const arena = p.scratch.allocator(); + var dp = dump.dumpPane(p, arena, pane) catch return null; + var zon = oneDump(p, arena, dp) catch return null; + // A terminal's history is what makes it big: kept without it, it comes + // back a fresh shell in its directory. + if (zon.len > max_record) if (dp.terminal) |*t| { + t.stream = ""; + t.stream_b64 = ""; + dp.body = ""; + zon = oneDump(p, arena, dp) catch return null; + }; + if (zon.len > max_record) return null; + const what: []const u8 = if (pane.file) |f| f.path else if (pane.pdfPath()) |pdf| pdf else if (pane.image) |iv| iv.path else if (pane.command) |c| c else pane.cwdSlice(); + var name = gpa.alloc(u8, @min(what.len, 4096)) catch return null; + // One line, a tab its end: a newline or a tab in it is a space. + for (what[0..name.len], 0..) |c, i| name[i] = if (c == '\n' or c == '\t') ' ' else c; + const owned = gpa.dupe(u8, zon) catch { + gpa.free(name); + return null; + }; + const at = layout.findPane(p, id); + const owner = layout.ownerOf(p, id); + return .{ + .serial = pane.serial, + .column = if (at) |f| layout.columnSerial(p, f.col) else 0, + .owner = if (owner) |o| p.panes[o].?.serial else 0, + .idx = if (at) |f| f.idx else 0, + .rows = p.rects[id].h, + .name = name, + .dump = owned, + }; +} + +pub const Kept = struct { + serial: u32, + column: u32, + owner: u32, + /// Its place in its column, to say which side of the gainer it was. + idx: usize, + rows: u16, + name: []u8, + dump: []u8, + + pub fn free(k: Kept, gpa: std.mem.Allocator) void { + gpa.free(k.name); + gpa.free(k.dump); + } +}; + +/// A Dump of the one pane `dp`, as a Restore would read it. +fn oneDump(p: *Pardes, arena: std.mem.Allocator, dp: dump.Pane) ![]const u8 { + const ids = [_]usize{0}; + const columns = [_]dump.Column{.{ .panes = &ids }}; + const one = [_]dump.Pane{dp}; + const state: dump.State = .{ .screen = .{ .cols = p.screen_w, .rows = p.screen_h }, .columns = &columns, .panes = &one }; + try dump.validate(state); + var out: std.Io.Writer.Allocating = .init(arena); + try std.zon.stringify.serialize(state, .{}, &out.writer); + return out.written(); +} + +/// The pane `k` kept is gone, its rows given to `gainer` (a slot, after +/// they moved; null none): listed, and its record framed for the store. +pub fn close(s: *Store, p: *Pardes, k: Kept, gainer: ?usize) void { + const gpa = p.recentGpa(); + var below = false; + var gainer_serial: u32 = 0; + if (gainer) |g| if (p.panes[g]) |gp| { + gainer_serial = gp.serial; + // It went from just below the gainer's place, or it was the first + // and the rows went down. + if (layout.findPane(p, g)) |f| below = f.idx < k.idx; + }; + var meta_buf: [max_meta]u8 = undefined; + const meta = std.fmt.bufPrint(&meta_buf, "{x:0>16} {d} {d} {d} {d} {d} {d} {d}\t{s}\n", .{ + s.session, s.pid, k.serial, k.column, gainer_serial, @intFromBool(below), k.owner, k.rows, k.name, + }) catch { + k.free(gpa); + return; + }; + const m = parseMeta(meta).?; + const body = std.mem.concat(gpa, u8, &.{ meta, k.dump }) catch { + k.free(gpa); + return; + }; + gpa.free(k.dump); + const pending_at = s.pending.items.len; + const framed = if (s.path != null) blk: { + frame(gpa, &s.pending, 'c', &.{body}) catch break :blk false; + break :blk true; + } else false; + s.list(gpa, m, 0, body); + gpa.free(k.name); + if (framed) if (s.closedBySerial(m.serial)) |c| { + c.pending_at = pending_at; + }; +} + +/// Written out when anything changed, at the end of a step and at the end +/// (`wait`: the lock is waited for there, never in a step). +pub fn flush(s: *Store, p: *Pardes, wait: bool) void { + const gpa = p.recentGpa(); + if (!p.recent.dirty and s.pending.items.len == 0) return; + const path = if (comptime pardes.hosted) s.path else null; + if (path == null or !s.loaded) { + if (path == null) { + for (p.recent.entries.items) |*e| e.changed = false; + p.recent.dirty = false; + s.pending.clearRetainingCapacity(); + for (s.closed.items) |*c| c.pending_at = null; + } + return; + } + const io = ioOf(); + var name_buf: [4096 + 16]u8 = undefined; + const lock_path = std.fmt.bufPrint(&name_buf, "{s}.lock", .{path.?}) catch return; + const lock = host(Dir.createFile, .{ cwd(), io, lock_path, Dir.CreateFileOptions{ .truncate = false } }) catch return; + // Closing it lets go of the lock. + defer host(File.close, .{ lock, io }); + if (wait) { + host(File.lock, .{ lock, io, File.Lock.exclusive }) catch return; + } else if (!(host(File.tryLock, .{ lock, io, File.Lock.exclusive }) catch return)) return; + const file = host(Dir.createFile, .{ cwd(), io, path.?, Dir.CreateFileOptions{ .read = true, .truncate = false } }) catch return; + defer host(File.close, .{ file, io }); + const st = host(File.stat, .{ file, io }) catch return; + // Others' records first, so this window's go after them here too. + s.readFile(p, file, st); + // Under the lock, a record still "being written" is one whose writer + // died: garbage. + if (s.end < st.size) s.garbage = true; + p.recent.changedFirst(); + var i = p.recent.entries.items.len; + var line_buf: [4096 + 64]u8 = undefined; + while (i > 0) : (i -= 1) { + const e = &p.recent.entries.items[i - 1]; + if (!e.changed) continue; + e.changed = false; + var w = std.Io.Writer.fixed(&line_buf); + recent.List.writeEntry(e.*, &w) catch continue; + const line = std.mem.trimEnd(u8, w.buffered(), "\n"); + if (line.len > 0) frame(gpa, &s.pending, 'r', &.{line}) catch {}; + } + p.recent.dirty = false; + if (s.garbage or st.size == 0 or st.size + s.pending.items.len > s.cap or st.size > 2 * s.base + (1 << 20)) { + s.compact(p, file, st, path.?); + return; + } + host(File.writePositionalAll, .{ file, io, s.pending.items, st.size }) catch { + // Out of disk: kept for no one, as the old list was. + s.dropPending(gpa); + return; + }; + for (s.closed.items) |*c| if (c.pending_at) |off| { + c.at = st.size + off; + c.pending_at = null; + gpa.free(c.body); + c.body = &.{}; + }; + s.end = st.size + s.pending.items.len; + s.pending.clearRetainingCapacity(); +} + +fn dropPending(s: *Store, gpa: std.mem.Allocator) void { + _ = gpa; + s.pending.clearRetainingCapacity(); + for (s.closed.items) |*c| c.pending_at = null; +} + +fn alive(pid: i32) bool { + if (pid <= 0) return false; + const rc = std.c.kill(pid, @enumFromInt(0)); + return rc == 0 or std.c.errno(rc) == .PERM; +} + +/// Under the lock: the live records into `.new`, this window's +/// pending ones after them, renamed over the store; then read again whole. +fn compact(s: *Store, p: *Pardes, file: File, st: File.Stat, path: []const u8) void { + const gpa = p.recentGpa(); + const io = ioOf(); + defer s.dropPending(gpa); + const buf = gpa.alloc(u8, 64 << 10) catch return; + defer gpa.free(buf); + var r: Reader = .{ .file = file, .io = io, .size = st.size, .buf = buf }; + var fold: recent.List = .{}; + defer fold.deinit(gpa); + const Live = struct { at: u64, end: u64, session: u64, pid: i32, serial: u32, keep: bool = false }; + var lives: std.ArrayList(Live) = .empty; + defer lives.deinit(gpa); + const Gone = struct { session: u64, serial: u32 }; + var gone: std.ArrayList(Gone) = .empty; + defer gone.deinit(gpa); + var at: u64 = 0; + while (at < r.size) { + switch (next(&r, at)) { + .partial => break, + .bad => at = resync(&r, at + 1), + .rec => |rec| { + at = rec.end(); + if (rec.kind == 'h' or !verify(&r, rec)) continue; + const b = r.bytes(rec.body, @min(rec.len, max_meta)) orelse continue; + switch (rec.kind) { + 'r' => if (recent.List.parseLine(b)) |e| fold.note(gpa, e.path, e.nums[0], e.nums[1], e.nums[2], e.nums[3]), + 'c' => if (parseMeta(b)) |m| lives.append(gpa, .{ .at = rec.at, .end = rec.end(), .session = m.session, .pid = m.pid, .serial = m.serial }) catch {}, + 'x' => { + const sp = std.mem.indexOfScalar(u8, b, ' ') orelse continue; + gone.append(gpa, .{ + .session = std.fmt.parseInt(u64, b[0..sp], 16) catch continue, + .serial = std.fmt.parseInt(u32, b[sp + 1 ..], 10) catch continue, + }) catch {}; + }, + else => {}, + } + }, + } + } + // The closed panes kept: of running sessions, not brought back, the + // newest `max_closed` a session, the newest within half the cap. + var budget: u64 = s.cap / 2 -| s.pending.items.len; + var k = lives.items.len; + while (k > 0) : (k -= 1) { + const l = &lives.items[k - 1]; + const reopened = for (gone.items) |g| { + if (g.session == l.session and g.serial == l.serial) break true; + } else false; + if (reopened or (l.session != s.session and !alive(l.pid))) continue; + var newer: usize = 0; + for (lives.items[k..]) |o| if (o.keep and o.session == l.session) { + newer += 1; + }; + if (newer >= max_closed or l.end - l.at > budget) continue; + budget -= l.end - l.at; + l.keep = true; + } + var name_buf: [4096 + 16]u8 = undefined; + const new_path = std.fmt.bufPrint(&name_buf, "{s}.new", .{path}) catch return; + const out = host(Dir.createFile, .{ cwd(), io, new_path, Dir.CreateFileOptions{} }) catch return; + var ok = false; + defer if (!ok) { + host(File.close, .{ out, io }); + host(Dir.deleteFile, .{ cwd(), io, new_path }) catch {}; + }; + var head: std.ArrayList(u8) = .empty; + defer head.deinit(gpa); + var gen_buf: [64]u8 = undefined; + const now = clockNs(); + const gen = std.hash.Wyhash.hash(s.session, std.mem.asBytes(&now)); + frame(gpa, &head, 'h', &.{std.fmt.bufPrint(&gen_buf, magic ++ " {x}", .{gen}) catch return}) catch return; + var line_buf: [4096 + 64]u8 = undefined; + var i = fold.entries.items.len; + while (i > 0) : (i -= 1) { + var w = std.Io.Writer.fixed(&line_buf); + recent.List.writeEntry(fold.entries.items[i - 1], &w) catch continue; + const line = std.mem.trimEnd(u8, w.buffered(), "\n"); + if (line.len > 0) frame(gpa, &head, 'r', &.{line}) catch return; + } + host(File.writePositionalAll, .{ out, io, head.items, 0 }) catch return; + var off: u64 = head.items.len; + for (lives.items) |l| if (l.keep) { + var from = l.at; + while (from < l.end) { + const n: usize = @intCast(@min(buf.len, l.end - from)); + const b = r.bytes(from, n) orelse return; + host(File.writePositionalAll, .{ out, io, b, off }) catch return; + off += n; + from += n; + } + }; + host(File.writePositionalAll, .{ out, io, s.pending.items, off }) catch return; + host(File.sync, .{ out, io }) catch return; + host(File.close, .{ out, io }); + ok = true; + host(Dir.rename, .{ cwd(), new_path, cwd(), path, io }) catch { + host(Dir.deleteFile, .{ cwd(), io, new_path }) catch {}; + return; + }; + // Every closed pane of this session is in the new file now: read again + // from it, at its offsets there. + for (s.closed.items) |c| freeClosed(gpa, c); + s.closed.clearRetainingCapacity(); + s.inode = 0; + const again = host(Dir.openFile, .{ cwd(), io, path, Dir.OpenFileOptions{} }) catch return; + defer host(File.close, .{ again, io }); + const nst = host(File.stat, .{ again, io }) catch return; + s.readFile(p, again, nst); +} + +// ---- bringing a closed pane back ---- + +/// Closed pane `serial` of this session made again, where it was: beside +/// the pane its rows went to, taking them from that pane alone (attached +/// again to its owner when that is still open), else placed as a new pane +/// from `from`. False when it is not this session's, or its record is gone +/// or bad; the keyboard on it otherwise. +pub fn reopen(s: *Store, p: *Pardes, serial: u32, from: usize) bool { + s.refresh(p); + const gpa = p.recentGpa(); + const place = (s.closedBySerial(serial) orelse return false).*; + const body = s.bodyOf(p, place) orelse return false; + // Read from the file: this call's to free (`unlist` frees one held). + defer if (place.body.len == 0) gpa.free(body); + const m = parseMeta(body) orelse return false; + var parsed = dump.readZon(p.gpa, body[m.dump_at..], "closed pane") catch return false; + defer parsed.deinit(); + const free = p.freeSlot() orelse { + p.reportError(from, "Panes", error.NoPaneSlots); + return false; + }; + const src = parsed.value.panes[0]; + const pane = dump.restorePane(p, free, src) catch |err| { + p.reportError(from, "Panes", err); + return false; + }; + pane.collapsed = false; + const owner = if (place.owner != 0) p.paneBySerial(place.owner) else null; + const gainer = if (place.gainer != 0) p.paneBySerial(place.gainer) else null; + const placed = if (owner) |o| + layout.attachFrom(p, o, free, place.rows, o) + else if (gainer) |g| + layout.giveBack(p, free, g, place.below, place.rows) + else + false; + if (!placed) exec.placeNew(p, from, from, free, .doc); + if (layout.findPane(p, free) == null) { + p.removePane(free, null) catch {}; + return false; + } + // No room anywhere: refused, it closes at the step's end, still listed. + if (p.unplaced.isSet(free)) return false; + // Brought back: no longer listed, here or when read again. + var rec_buf: [64]u8 = undefined; + const rec = std.fmt.bufPrint(&rec_buf, "{x:0>16} {d}", .{ s.session, serial }) catch unreachable; + if (s.path != null) frame(gpa, &s.pending, 'x', &.{rec}) catch {}; + s.unlist(gpa, serial); + p.active = free; + if (layout.findPane(p, free)) |f| p.setActiveColumn(f.col); + p.sync(); + return true; +} + +/// A closed pane's record body: held here, or read from the file and +/// checked against its CRC (gpa; the caller frees it when it is not +/// `c.body`). +fn bodyOf(s: *Store, p: *Pardes, c: Closed) ?[]u8 { + if (c.body.len > 0) return c.body; + const path = s.path orelse return null; + if (comptime !pardes.hosted) return null; + const gpa = p.recentGpa(); + const io = ioOf(); + const file = host(Dir.openFile, .{ cwd(), io, path, Dir.OpenFileOptions{} }) catch return null; + defer host(File.close, .{ file, io }); + const st = host(File.stat, .{ file, io }) catch return null; + if (st.inode != s.inode) return null; + var head_buf: [max_head]u8 = undefined; + var r: Reader = .{ .file = file, .io = io, .size = st.size, .buf = &head_buf }; + const rec = switch (next(&r, c.at)) { + .rec => |rec| rec, + else => return null, + }; + if (rec.kind != 'c') return null; + const body = gpa.alloc(u8, rec.len) catch return null; + const n = host(File.readPositionalAll, .{ file, io, body, rec.body }) catch 0; + if (n != rec.len or std.hash.Crc32.hash(body) != rec.crc) { + gpa.free(body); + return null; + } + return body; +} + +/// `@p` naming a pane this session closed: its serial. +pub fn closedAddress(s: *const Store, word: []const u8) ?u32 { + const addr = @import("config.zig").pane_addr; + if (!std.mem.startsWith(u8, word, addr)) return null; + const digits = word[addr.len..]; + const end = for (digits, 0..) |ch, i| { + if (!std.ascii.isDigit(ch)) break i; + } else digits.len; + // `@p7` alone, or `@p7:` with nothing after it: the pane, no place in it. + if (end == 0 or (end < digits.len and !(digits[end] == ':' and end + 1 == digits.len))) return null; + const serial = std.fmt.parseInt(u32, digits[0..end], 10) catch return null; + return if (s.closedBySerial(serial) != null) serial else null; +} + +// ---- tests ---- + +const testing = std.testing; + +/// The store file of a test, in its own directory (never the user's). +fn testPath(tmp: *std.testing.TmpDir) ![]u8 { + var base_buf: [std.fs.max_path_bytes]u8 = undefined; + const dir = base_buf[0..try tmp.dir.realPath(testing.io, &base_buf)]; + return std.fs.path.join(testing.allocator, &.{ dir, "store" }); +} + +fn testWindow(path: []const u8) !*Pardes { + return Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 48, .store_path = path }); +} + +fn fileSize(path: []const u8) u64 { + const st = std.Io.Dir.cwd().statFile(testing.io, path, .{}) catch return 0; + return st.size; +} + +test "store: two windows share Recent, each reading the other's writes on use; a busy lock delays, never loses" { + if (comptime !pardes.hosted) return; + var tmp = testing.tmpDir(.{}); + defer tmp.cleanup(); + const path = try testPath(&tmp); + defer testing.allocator.free(path); + const a = try testWindow(path); + defer a.deinit(); + const b = try testWindow(path); + defer b.deinit(); + try testing.expect(a.store.session != b.store.session); + a.recent.opened(a.recentGpa(), "/w/a.zig"); + a.saveRecent(); + b.recent.opened(b.recentGpa(), "/w/b.zig"); + b.saveRecent(); + a.recent.closed(a.recentGpa(), "/w/a.zig", 7, 3); + a.saveRecent(); + // Each has both, in the order of the log: a closed last, then b. + a.store.refresh(a); + b.store.refresh(b); + for ([_]*Pardes{ a, b }) |w| { + try testing.expectEqual(@as(usize, 2), w.recent.entries.items.len); + try testing.expectEqualStrings("/w/a.zig", w.recent.entries.items[0].path); + try testing.expectEqualStrings("/w/b.zig", w.recent.entries.items[1].path); + try testing.expectEqual(@as(u32, 7), w.recent.get("/w/a.zig").?.line); + } + // Another window holding the lock: b's change waits in memory... + const lock_path = try std.fmt.allocPrint(testing.allocator, "{s}.lock", .{path}); + defer testing.allocator.free(lock_path); + const lock = try std.Io.Dir.cwd().createFile(testing.io, lock_path, .{ .truncate = false }); + try lock.lock(testing.io, .exclusive); + b.recent.opened(b.recentGpa(), "/w/c.zig"); + const size = fileSize(path); + b.saveRecent(); + try testing.expectEqual(size, fileSize(path)); + try testing.expect(b.recent.dirty); + // ...and is written at the next step after it is let go. + lock.close(testing.io); + b.saveRecent(); + try testing.expect(fileSize(path) > size); + a.store.refresh(a); + try testing.expectEqualStrings("/w/c.zig", a.recent.entries.items[0].path); + // A window started now folds the same list. + const c = try testWindow(path); + defer c.deinit(); + try testing.expectEqual(@as(usize, 3), c.recent.entries.items.len); + for (a.recent.entries.items, c.recent.entries.items) |x, y| try testing.expectEqualStrings(x.path, y.path); +} + +test "store: the cap holds, compaction keeps the newest, Recent keeps its 200" { + if (comptime !pardes.hosted) return; + var tmp = testing.tmpDir(.{}); + defer tmp.cleanup(); + const path = try testPath(&tmp); + defer testing.allocator.free(path); + const a = try testWindow(path); + defer a.deinit(); + a.store.cap = 48 << 10; + var name: [32]u8 = undefined; + var inodes: usize = 0; + var last_inode = a.store.inode; + for (0..3000) |i| { + const file = std.fmt.bufPrint(&name, "/w/file{d}.zig", .{i}) catch unreachable; + a.recent.opened(a.recentGpa(), file); + a.recent.closed(a.recentGpa(), file, 1, 1); + a.saveRecent(); + try testing.expect(fileSize(path) <= a.store.cap); + if (a.store.inode != last_inode) inodes += 1; + last_inode = a.store.inode; + } + try testing.expect(inodes > 2); + try testing.expectEqual(@as(usize, recent.cap), a.recent.entries.items.len); + try testing.expectEqualStrings("/w/file2999.zig", a.recent.entries.items[0].path); + const b = try testWindow(path); + defer b.deinit(); + try testing.expectEqual(@as(usize, recent.cap), b.recent.entries.items.len); + try testing.expectEqualStrings("/w/file2999.zig", b.recent.entries.items[0].path); + try testing.expectEqualStrings("/w/file2800.zig", b.recent.entries.items[recent.cap - 1].path); + // Closed panes' text counts too: the newest stay, within the cap. + a.store.cap = 256 << 10; + var text: [20 << 10]u8 = @splat('t'); + var serial: u32 = 0; + for (0..30) |i| { + a.newScratchBelow(a.active); + const id = a.active; + const f = &a.panes[id].?.file.?; + text[0] = @intCast('a' + i % 26); + @import("File.zig").setContent(a, f, try a.gpa.dupe(u8, &text)); + serial = a.panes[id].?.serial; + // Made outside a step: announced as a step's end would. + pardes.ctlfs.events.announce(a); + try a.removePane(id, null); + a.saveRecent(); + try testing.expect(fileSize(path) <= a.store.cap); + } + try testing.expect(a.store.closed.items.len < 30); + try testing.expectEqual(serial, a.store.closed.items[a.store.closed.items.len - 1].serial); + try testing.expect(a.store.reopen(a, serial, a.active)); + try testing.expectEqual(@as(u8, 'a' + 29 % 26), a.panes[a.active].?.file.?.content[0]); +} + +test "store: a garbage or torn file never stops pardes, records past garbage are read, the next writer mends it" { + if (comptime !pardes.hosted) return; + var tmp = testing.tmpDir(.{}); + defer tmp.cleanup(); + const path = try testPath(&tmp); + defer testing.allocator.free(path); + try std.Io.Dir.cwd().writeFile(testing.io, .{ .sub_path = path, .data = "\x00\xffnot a store at all\n@r 9 zz\n" }); + const a = try testWindow(path); + defer a.deinit(); + try testing.expectEqual(@as(usize, 0), a.recent.entries.items.len); + try testing.expect(a.store.garbage); + a.recent.opened(a.recentGpa(), "/w/a.zig"); + a.saveRecent(); + try testing.expect(!a.store.garbage); + // A torn record at the end (its writer died), then garbage, then a + // whole record: the whole one is read, the rest passed over. + var tail: std.ArrayList(u8) = .empty; + defer tail.deinit(testing.allocator); + try tail.appendSlice(testing.allocator, "@r 40 0000"); + try tail.appendSlice(testing.allocator, "\x00junk\n@x not a header\n"); + try frame(testing.allocator, &tail, 'r', &.{"3 1 0 0 /w/after.zig"}); + { + const f = try std.Io.Dir.cwd().openFile(testing.io, path, .{ .mode = .read_write }); + defer f.close(testing.io); + try f.writePositionalAll(testing.io, tail.items, (try f.stat(testing.io)).size); + } + const b = try testWindow(path); + defer b.deinit(); + try testing.expect(b.recent.get("/w/a.zig") != null); + try testing.expectEqual(@as(u32, 3), b.recent.get("/w/after.zig").?.line); + try testing.expect(b.store.garbage); + // A record half written (the length runs past the end) is waited for, + // not garbage: a reader reads it once it is whole. + var half: std.ArrayList(u8) = .empty; + defer half.deinit(testing.allocator); + try frame(testing.allocator, &half, 'r', &.{"5 1 0 0 /w/late.zig"}); + { + const f = try std.Io.Dir.cwd().openFile(testing.io, path, .{ .mode = .read_write }); + defer f.close(testing.io); + const end = (try f.stat(testing.io)).size; + try f.writePositionalAll(testing.io, half.items[0..10], end); + a.store.refresh(a); + try testing.expect(a.recent.get("/w/late.zig") == null); + try f.writePositionalAll(testing.io, half.items[10..], end + 10); + } + a.store.refresh(a); + try testing.expectEqual(@as(u32, 5), a.recent.get("/w/late.zig").?.line); + // The next write compacts it: a reader then finds no garbage. + b.recent.opened(b.recentGpa(), "/w/b.zig"); + b.saveRecent(); + const c = try testWindow(path); + defer c.deinit(); + try testing.expect(!c.store.garbage); + for ([_][]const u8{ "/w/a.zig", "/w/after.zig", "/w/late.zig", "/w/b.zig" }) |want| try testing.expect(c.recent.get(want) != null); +} + +test "store: a closed pane comes back where it was, its rows from the pane that took them, nothing else moving; Panes lists it, marked" { + if (comptime !pardes.hosted) return; + var tmp = testing.tmpDir(.{}); + defer tmp.cleanup(); + const path = try testPath(&tmp); + defer testing.allocator.free(path); + const p = try testWindow(path); + defer p.deinit(); + _ = try p.setTestFile("alpha\nbeta\n"); + p.newScratchBelow(0); + const sid = p.active; + @import("File.zig").setContent(p, &p.panes[sid].?.file.?, try p.gpa.dupe(u8, "kept text\n")); + const other = p.col_panes[0][1]; + p.newScratchBelow(0); + try testing.expect(layout.splitColumn(p, p.active, p.active, false)); + const far = p.active; + _ = other; + p.active = 0; + // Made outside a step: announced as a step's end would. + pardes.ctlfs.events.announce(p); + layout.compute(p); + const before = p.rects; + const serial = p.panes[sid].?.serial; + try p.removePane(sid, null); + layout.compute(p); + try testing.expectEqualDeep(before[far], p.rects[far]); + // Written to the store: brought back from the file, not memory. + p.saveRecent(); + try testing.expectEqual(@as(usize, 0), p.store.closedBySerial(serial).?.body.len); + try testing.expect(p.store.reopen(p, serial, 0)); + const back = p.active; + layout.compute(p); + try testing.expectEqualStrings("kept text\n", p.panes[back].?.file.?.content); + try testing.expect(p.panes[back].?.serial != serial); + // Its rect as it was, the one above as it was, the other column's + // untouched: its rows came from the pane that took them alone. + try testing.expectEqualDeep(before[sid], p.rects[back]); + try testing.expectEqualDeep(before[0], p.rects[0]); + try testing.expectEqualDeep(before[far], p.rects[far]); + try testing.expect(p.store.closedBySerial(serial) == null); + // Closed again: Panes lists it after the open ones, `(closed)`. + const again = p.panes[back].?.serial; + pardes.ctlfs.events.announce(p); + try p.removePane(back, null); + p.saveRecent(); + p.active = 0; + p.update(.{ .key = .{ .cp = ' ', .text = " " } }); + for ("bb") |ch| p.update(.{ .key = .{ .cp = ch, .text = &.{ch} } }); + const qid = for (p.panes, 0..) |slot, i| { + if (slot) |qp| if (qp.query != null) break i; + } else return error.NoQueryPane; + const q = p.panes[qid].?.query.?; + const row = for (q.items.items, 0..) |it, i| { + if (it.ref == again) break i; + } else return error.NotListed; + const line = @import("modal.zig").lineSlice(p.panes[qid].?.file.?.content, @intCast(row + 1)); + try testing.expect(std.mem.endsWith(u8, line, "(closed)")); + // Enter on it: back, and listed no more (nor in the store when read). + panes_mod.Query.land(p, qid, row); + const found = for (p.panes) |slot| { + if (slot) |pp| if (pp.file) |f| if (std.mem.eql(u8, f.content, "kept text\n")) break true; + } else false; + try testing.expect(found); + try testing.expect(p.store.closedBySerial(again) == null); + p.saveRecent(); + p.store.inode = 0; + p.store.refresh(p); + try testing.expect(p.store.closedBySerial(again) == null); + // Another session lists none of this one's. + const w = try testWindow(path); + defer w.deinit(); + try testing.expectEqual(@as(usize, 0), w.store.closed.items.len); +} + +const panes_mod = @import("panes.zig"); + +test "zz store timings on a 90 MiB store (STORE_TIMES=)" { + const dir_z = std.c.getenv("STORE_TIMES") orelse return error.SkipZigTest; + const gpa = testing.allocator; + const path = try std.fs.path.join(gpa, &.{ std.mem.span(dir_z), "store" }); + defer gpa.free(path); + // A store as another running session leaves it: 200 files, then 90 + // closed panes of 1 MiB each (this pid: alive, so a compaction keeps + // what fits). + { + var bytes: std.ArrayList(u8) = .empty; + defer bytes.deinit(gpa); + try frame(gpa, &bytes, 'h', &.{magic ++ " 1"}); + var line: [64]u8 = undefined; + for (0..200) |i| try frame(gpa, &bytes, 'r', &.{std.fmt.bufPrint(&line, "1 1 0 0 /w/file{d}.zig", .{i}) catch unreachable}); + const out = try std.Io.Dir.cwd().createFile(testing.io, path, .{}); + defer out.close(testing.io); + try out.writePositionalAll(testing.io, bytes.items, 0); + var off: u64 = bytes.items.len; + const big = try gpa.alloc(u8, 1 << 20); + defer gpa.free(big); + @memset(big, 'z'); + for (0..90) |i| { + bytes.clearRetainingCapacity(); + var meta: [128]u8 = undefined; + const m = std.fmt.bufPrint(&meta, "{x:0>16} {d} {d} 1 0 0 0 10\t/w/closed{d}\n", .{ @as(u64, 0xfeed), std.c.getpid(), i + 1, i }) catch unreachable; + try frame(gpa, &bytes, 'c', &.{ m, big }); + try out.writePositionalAll(testing.io, bytes.items, off); + off += bytes.items.len; + } + } + const t0 = nowNs(); + const p = try testWindow(path); + defer p.deinit(); + const load_ns = nowNs() - t0; + var t = nowNs(); + for (0..1000) |_| p.store.refresh(p); + const refresh_ns = (nowNs() - t) / 1000; + t = nowNs(); + var name: [32]u8 = undefined; + for (0..100) |i| { + const f = std.fmt.bufPrint(&name, "/w/new{d}.zig", .{i}) catch unreachable; + p.recent.opened(p.recentGpa(), f); + p.recent.closed(p.recentGpa(), f, 1, 1); + p.saveRecent(); + } + const flush_ns = (nowNs() - t) / 100; + const before = fileSize(path); + p.store.garbage = true; + p.recent.opened(p.recentGpa(), "/w/last.zig"); + t = nowNs(); + p.saveRecent(); + const compact_ns = nowNs() - t; + std.debug.print("store timings: load {d} us, refresh (unchanged) {d} ns, flush one record {d} us, compaction {d} -> {d} bytes {d} ms\n", .{ + load_ns / 1000, refresh_ns, flush_ns / 1000, before, fileSize(path), compact_ns / 1_000_000, + }); +} + +fn nowNs() u64 { + return @intCast(clockNs()); +} 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 }); diff --git a/src/layout.zig b/src/layout.zig index e674ccbf..efa79394 100644 --- a/src/layout.zig +++ b/src/layout.zig @@ -1070,6 +1070,32 @@ pub fn attachFrom(p: *Pardes, owner: usize, id: usize, rows: u16, giver: usize) return true; } +/// A closed pane brought back (Store.reopen) where it was: next to +/// `gainer`, the pane its rows went to when it closed, below it (under its +/// group) or above it, `rows` tall, those rows taken from `gainer` alone, +/// so no other pane moves. False when the gainer is folded or too short +/// to give a pane its minimum and keep its own. +pub fn giveBack(p: *Pardes, id: usize, gainer: usize, below: bool, rows: u16) bool { + const g = p.panes[gainer] orelse return false; + const nw = p.panes[id] orelse return false; + if (g.collapsed) return false; + compute(p); + const f = findPane(p, gainer) orelse return false; + // Its tag wraps at the column's width: measured there. + p.rects[id].w = p.col_w[f.col]; + const gh = p.rects[gainer].h; + const least = minRows(p, id); + const keeps = minRows(p, gainer); + if (gh < least + keeps) return false; + const give = std.math.clamp(rows, least, gh - keeps); + snapColWeights(p, f.col); + insert(p, f.col, if (below) groupEnd(p, f.col, f.idx) else f.idx, id); + g.vweight = @floatFromInt(gh - give); + nw.vweight = @floatFromInt(give); + compute(p); + return true; +} + /// Collapse on an owner with attachments: the whole group folds to its /// tags, every row it gave up to the one unfolded neighbour outside it, /// and unfolds again from that neighbour, as one pane's Collapse does. diff --git a/src/look.zig b/src/look.zig index cd8df4fc..ede7d910 100644 --- a/src/look.zig +++ b/src/look.zig @@ -1813,8 +1813,12 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void { switch (found) { .none => lookUnresolved(p, id, pane, trimmed, std.mem.trimEnd(u8, txt, "\r\n")), .pane => |t| { - // `@p` names no open pane: a miss, said. - if (t.id >= MAX_PANES or p.panes[t.id] == null) return missed(p, id, "no pane {s}", .{pl.path[0..@min(pl.path.len, 32)]}); + // `@p` names no open pane: one this session closed comes + // back where it was (Store.reopen), any other is a miss, said. + if (t.id >= MAX_PANES or p.panes[t.id] == null) { + if (p.store.closedAddress(pl.path)) |serial| if (p.store.reopen(p, serial, id)) return; + return missed(p, id, "no pane {s}", .{pl.path[0..@min(pl.path.len, 32)]}); + } const target = p.panes[t.id].?; // A PDF's page past its last is a miss, as a line past a file's // end is: never the last page in its place (opening one, the diff --git a/src/main.zig b/src/main.zig index 9086fa90..26431324 100644 --- a/src/main.zig +++ b/src/main.zig @@ -707,7 +707,7 @@ fn nativeMain(std_init: std.process.Init) !void { opts.startup_config = found.bytes; opts.startup_config_path = found.path; opts.config_dir = found.dir; - opts.recent_path = recentPath(init.io, arena, init.environ_map); + opts.store_path = storePath(init.io, arena, init.environ_map); if (found.dir) |d| @import("crash.zig").setDir(d); if (detach) |name| { const named = if (name.len != 0) @@ -789,10 +789,11 @@ test { if (comptime pardes.font_picker) _ = @import("fonts.zig"); } -/// Where the list of files opened lately is kept: $XDG_STATE_HOME/pardes -/// /recent, else ~/.local/state/pardes/recent, the directory made if need -/// be; null with no home to keep it in (the list then lasts the session). -fn recentPath(io: std.Io, arena: std.mem.Allocator, env: *const std.process.Environ.Map) ?[]const u8 { +/// Where the store every window shares is kept (Store.zig): +/// $XDG_STATE_HOME/pardes/store, else ~/.local/state/pardes/store, the +/// directory made if need be; null with no home to keep it in (Recent and +/// the closed panes then last the session). +fn storePath(io: std.Io, arena: std.mem.Allocator, env: *const std.process.Environ.Map) ?[]const u8 { const dir = dir: { if (env.get("XDG_STATE_HOME")) |base| if (base.len != 0 and std.fs.path.isAbsolute(base)) break :dir std.fs.path.join(arena, &.{ base, "pardes" }) catch return null; @@ -801,5 +802,5 @@ fn recentPath(io: std.Io, arena: std.mem.Allocator, env: *const std.process.Envi break :dir std.fs.path.join(arena, &.{ home, ".local", "state", "pardes" }) catch return null; }; std.Io.Dir.cwd().createDirPath(io, dir) catch return null; - return std.fs.path.join(arena, &.{ dir, "recent" }) catch null; + return std.fs.path.join(arena, &.{ dir, "store" }) catch null; } diff --git a/src/ninep/tree.zig b/src/ninep/tree.zig index 59ef5ef4..7b107ea7 100644 --- a/src/ninep/tree.zig +++ b/src/ninep/tree.zig @@ -1110,6 +1110,10 @@ fn readFile(p: *Pardes, req: Req, target: Target) Reply { .ctl => ctl.readRoot(p, req), .commands => ctl.readCommands(p, req), .recent => recent: { + // Every window's files: what the others wrote since, read, + // unless a step is out mid-syscall (nothing may change the + // core under it): then the list as it was. + if (pardes.turn.out == 0) p.store.refresh(p); var text: std.Io.Writer.Allocating = .init(p.scratch.allocator()); pardes.panes.Output.recentText(p, &text.writer, false) catch break :recent Reply.fail(req.tag, E.NOMEM); p.fs.stage(p.gpa).appendSlice(p.gpa, text.written()) catch break :recent Reply.fail(req.tag, E.NOMEM); diff --git a/src/pardes.zig b/src/pardes.zig index 3c5cf6a7..7f79f094 100644 --- a/src/pardes.zig +++ b/src/pardes.zig @@ -16,6 +16,7 @@ const locations = @import("locations.zig"); pub const tagline = @import("tagline.zig"); pub const exec = @import("exec.zig"); const recent_files = @import("recent.zig"); +const Store = @import("Store.zig"); pub const mouse = @import("mouse.zig"); pub const normal = @import("normal.zig"); pub const edit = @import("edit.zig"); @@ -484,6 +485,7 @@ test { _ = @import("draw.zig"); _ = @import("dump.zig"); // its tests ran nowhere before _ = @import("recent.zig"); + _ = @import("Store.zig"); _ = @import("Presentation.zig"); _ = @import("animation.zig"); _ = @import("pager.zig"); @@ -4326,10 +4328,10 @@ pub const Options = struct { ninep_quic: ?[]const u8 = null, mounts: []const filesystem.Mount = &.{}, config_dir: ?[]const u8 = null, - /// Where the list of files opened lately is kept across sessions - /// (recent.zig), set by the host that has a state directory; none, it - /// lives as long as the session. - recent_path: ?[]const u8 = null, + /// The store every window shares (Store.zig): the files opened lately, + /// the panes closed. Set by the host that has a state directory; none, + /// they last as long as the session. + store_path: ?[]const u8 = null, startup_config_path: ?[]const u8 = null, image_allocator: ?std.mem.Allocator = null, pdf_allocator: ?std.mem.Allocator = null, @@ -4402,6 +4404,9 @@ pub const Pardes = struct { /// The files opened lately, closed ones too (recent.zig): `Recent`, /// /recent, and the jumplist's way back to a closed pane. recent: recent_files.List = .{}, + /// Where Recent is kept, shared with every other window, and the panes + /// this session closed, which Panes brings back (Store.zig). + store: Store = .{}, /// acme's Incl: the directories a look at a name that is nowhere else /// (`stdio.h`, ``) is tried in, in order. The session's, seeded /// with C's (/usr/include, /usr/local/include), kept in a dump. @@ -4410,9 +4415,6 @@ pub const Pardes = struct { /// the next takes. twin_groups: std.ArrayList(panes.File.TwinGroup) = .empty, next_twin: u32 = 1, - /// The kept list was read (or there was none): only then is it written, - /// so a session never writes over one it did not read. - recent_loaded: bool = false, jumps: [MAX_JUMPS]Loc = undefined, njumps: usize = 0, jcur: usize = 0, @@ -4852,29 +4854,15 @@ pub const Pardes = struct { return if (comptime hosted) std.heap.smp_allocator else p.gpa; } - /// The kept list of files opened lately, read once at start. + /// The store read once at start: a new session's, Recent from it. pub fn loadRecent(p: *Pardes) void { - const path = p.opts.recent_path orelse return; - if (comptime !hosted) return; - defer p.recent_loaded = true; - const bytes = filesystem.readFile(p.gpa, path) catch return; - defer p.gpa.free(bytes); - p.recent.read(p.recentGpa(), bytes); + p.store = Store.init(p.opts.store_path); + p.store.load(p); } - /// Written out when it changed, at the end of a step and at the end. + /// What changed written out, at the end of a step and at the end. pub fn saveRecent(p: *Pardes) void { - if (!p.recent.dirty) return; - p.recent.dirty = false; - const path = p.opts.recent_path orelse return; - if (comptime !hosted) return; - if (!p.recent_loaded) return; - var out: std.Io.Writer.Allocating = .init(p.gpa); - defer out.deinit(); - p.recent.write(&out.writer) catch return; - turn.yield(); - defer turn.back(); - std.Io.Dir.cwd().writeFile(std.Io.Threaded.global_single_threaded.io(), .{ .sub_path = path, .data = out.written() }) catch {}; + p.store.flush(p, false); } /// A file pane's path and dot, for the recent list (0 when none). @@ -4928,7 +4916,8 @@ pub const Pardes = struct { pub fn deinit(p: *Pardes) void { // Every file open now is closed with its dot, and the list kept. for (p.panes) |slot| if (slot) |pane| p.noteClosed(pane); - p.saveRecent(); + p.store.flush(p, true); + p.store.deinit(p.recentGpa()); p.recent.deinit(p.recentGpa()); for (p.incl.items) |dir| p.gpa.free(dir); p.incl.deinit(p.gpa); @@ -7230,12 +7219,16 @@ pub const Pardes = struct { const owner = layout.ownerOf(p, id); const place = layout.findPane(p, id); const emptied = if (place) |f| (if (p.col_n[f.col] == 1) f.col else null) else null; + // What it was and where, for Panes to bring back (Store.zig). + const kept = p.store.keep(p, id); + errdefer if (kept) |k| k.free(p.recentGpa()); // Before anything is torn down, so a failure here changes nothing. try p.deinitPane(pane); const gainer = layout.absorbVWeight(p, id, owner orelse to); const beside = if (emptied) |c| p.besideColumn(c, p.rects[id]) else null; layout.removePane(p, id); p.panes[id] = null; + if (kept) |k| p.store.close(p, k, gainer); // The finder's last pane takes the column with it. const finder_gone = if (emptied) |c| p.finder.emptied(p, c) else false; if (p.active != id) return; @@ -7257,6 +7250,8 @@ pub const Pardes = struct { } try p.detachCwds(parents[0..count]); for (ids[0..count], parents[0..count]) |closed, pane| { + // Kept for Panes; its column goes, so it comes back as a new pane. + if (p.store.keep(p, closed)) |k| p.store.close(p, k, null); p.retirePane(pane); layout.removePane(p, closed); p.panes[closed] = null; 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, ` `, 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: ` `. + 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 }; } }; -- cgit v1.3