//! The panes this session closed, which Panes brings back: this session's //! alone, and in memory. Each is a one-pane Dump (dump.savePane) and where //! it was, the newest 64 within 64 MiB. Restore keeps them (`clone`); the //! session's end drops them. Recent, which every session shares, is in the //! state file (Db.zig). 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 Pardes = pardes.Pardes; /// This session keeps this many closed panes, the newest. pub const max_closed = 64; /// A closed pane's dump is never longer: a terminal's that would be is /// kept without its history, any other pane's is not kept. pub const max_dump: usize = 8 << 20; /// The closed panes' dumps together are never longer: the oldest go. pub const max_bytes: usize = 64 << 20; /// 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. name: []u8, /// The pane, as dump.savePane made it. dump: []u8, }; /// This session's closed panes, oldest first (recentGpa's). closed: std.ArrayList(Closed) = .empty, /// The panes closing now went to another window (Handover.finish): open /// there, they are no closed panes of this session, so none is kept. away: bool = false, pub fn deinit(s: *Store, gpa: std.mem.Allocator) void { for (s.closed.items) |c| freeClosed(gpa, c); s.closed.deinit(gpa); } fn freeClosed(gpa: std.mem.Allocator, c: Closed) void { gpa.free(c.name); gpa.free(c.dump); } /// A copy for the session a Restore makes: the same session (the same /// process), its closed panes still its own. pub fn clone(s: *const Store, gpa: std.mem.Allocator) Store { var copy = s.*; copy.closed = .empty; for (s.closed.items) |c| { const name = gpa.dupe(u8, c.name) catch break; const body = gpa.dupe(u8, c.dump) catch { gpa.free(name); break; }; var kept = c; kept.name = name; kept.dump = body; copy.closed.append(gpa, kept) catch { freeClosed(gpa, kept); break; }; } return copy; } // ---- the closed panes ---- /// Pane `id`, about to close, kept: 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 carried to /// another window (`away`), 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 { if (s.away) return null; 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; // An output pane's dump holds its text whole: one past the bound is // known before a dump of it is made, which for a 256 MiB +Pager held // the session up for seconds only to be thrown away. if (pane.file) |f| if (f.output != null and f.content.len > max_dump) return null; const gpa = p.recentGpa(); var owned = dump.savePane(gpa, pane, false) catch return null; // A terminal's history or a file's undo is what makes it big: kept // without them it comes back a fresh shell in its directory, or its // file as it is on disk. if (owned.len > max_dump) { gpa.free(owned); owned = dump.savePane(gpa, pane, true) catch return null; } if (owned.len > max_dump) { gpa.free(owned); return null; } const what: []const u8 = if (pane.file) |f| f.path else if (pane.addon) |k| k.path else if (pane.command) |c| c else pane.cwdSlice(); var name = gpa.alloc(u8, @min(what.len, 4096)) catch { gpa.free(owned); return null; }; // One line: 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 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.view().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); } }; /// The pane `k` kept is gone, its rows given to `gainer` (a slot, after /// they moved; null none): listed, the oldest going past `max_closed` or /// `max_bytes`. 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 bytes = k.dump.len; for (s.closed.items) |c| bytes += c.dump.len; while (s.closed.items.len > 0 and (s.closed.items.len >= max_closed or bytes > max_bytes)) { const gone = s.closed.orderedRemove(0); bytes -= gone.dump.len; freeClosed(gpa, gone); } s.closed.append(gpa, .{ .serial = k.serial, .column = k.column, .gainer = gainer_serial, .below = below, .owner = k.owner, .rows = k.rows, .name = k.name, .dump = k.dump }) catch k.free(gpa); } /// A closed pane as Panes lists it: its serial and what its row shows. pub const Named = struct { serial: u32, name: []const u8 }; /// This session's closed panes, the latest first (scratch; the names the /// store's): what a step lists of them (`tape.authority`). pub fn closedNames(p: *Pardes) []const Named { const items = p.store.closed.items; const out = p.scratch.allocator().alloc(Named, items.len) catch return &.{}; for (out, 0..) |*o, i| { const c = items[items.len - 1 - i]; o.* = .{ .serial = c.serial, .name = c.name }; } return out; } /// 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; } /// 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 dump will not /// load; the keyboard on it otherwise. pub fn reopen(s: *Store, p: *Pardes, serial: u32, from: usize) bool { // This session's: a replica has the authority's (`tape.authority`). const place = pardes.tape.authority(record, .{ s, serial }) orelse return false; const free = p.freeSlot() orelse { p.reportError(from, "Panes", error.NoPaneSlots); return false; }; _ = dump.loadPane(p, free, place.body) catch |err| { p.reportError(from, "Panes", err); return 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. for (s.closed.items, 0..) |c, i| if (c.serial == serial) { freeClosed(p.recentGpa(), s.closed.orderedRemove(i)); break; }; p.view().active = free; if (layout.findPane(p, free)) |f| p.setActiveColumn(f.col); p.sync(); return true; } /// Where a closed pane goes back, and its dump. pub const Record = struct { gainer: u32, below: bool, owner: u32, rows: u16, body: []const u8 }; /// Closed pane `serial`'s record (its dump the store's, which outlives the /// step); null when it is not this session's. fn record(s: *Store, serial: u32) ?Record { const c = s.closedBySerial(serial) orelse return null; return .{ .gainer = c.gainer, .below = c.below, .owner = c.owner, .rows = c.rows, .body = c.dump }; } /// `@p` alone, the pane and no place in it: its serial, for a pane /// this session closed to come back by (`reopen` says whether it did). pub fn closedAddress(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; return std.fmt.parseInt(u32, digits[0..end], 10) catch null; } // ---- tests ---- const testing = std.testing; const panes_mod = @import("panes.zig"); /// A window keeping its state in `dir` (never the user's DumpDir). fn testWindow(dir: []const u8) !*Pardes { var config: [4200]u8 = undefined; return Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 48, .state = .disk, .startup_config = try std.fmt.bufPrint(&config, "DumpDir {s}\n", .{dir}) }); } test "store: this session keeps its newest 64 closed panes" { const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 48 }); defer p.deinit(); var serial: u32 = 0; var text: [16]u8 = undefined; for (0..max_closed + 6) |i| { p.newScratchBelow(p.view().active); const id = p.view().active; @import("File.zig").setContent(p, &p.panes[id].?.file.?, try p.gpa.dupe(u8, std.fmt.bufPrint(&text, "scratch {d}\n", .{i}) catch unreachable)); serial = p.panes[id].?.serial; // Made outside a step: announced as a step's end would. pardes.ctlfs.events.announce(p); try p.removePane(id, null); } try testing.expectEqual(@as(usize, max_closed), p.store.closed.items.len); try testing.expectEqual(serial, p.store.closed.items[max_closed - 1].serial); try testing.expect(p.store.reopen(p, serial, p.view().active)); try testing.expectEqualStrings("scratch 69\n", p.panes[p.view().active].?.file.?.content); } 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" { var tmp = testing.tmpDir(.{}); defer tmp.cleanup(); var dir_buf: [std.Io.Dir.max_path_bytes]u8 = undefined; const dir = dir_buf[0..try tmp.dir.realPath(testing.io, &dir_buf)]; const p = try testWindow(dir); defer p.deinit(); _ = try p.setTestFile("alpha\nbeta\n"); p.newScratchBelow(0); const sid = p.view().active; @import("File.zig").setContent(p, &p.panes[sid].?.file.?, try p.gpa.dupe(u8, "kept text\n")); p.newScratchBelow(0); try testing.expect(layout.splitColumn(p, p.view().active, p.view().active, false)); const far = p.view().active; p.view().active = 0; // Made outside a step: announced as a step's end would. pardes.ctlfs.events.announce(p); layout.compute(p); const before = p.view().rects; const serial = p.panes[sid].?.serial; try p.removePane(sid, null); layout.compute(p); try testing.expectEqualDeep(before[far], p.view().rects[far]); try testing.expect(p.store.reopen(p, serial, 0)); const back = p.view().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.view().rects[back]); try testing.expectEqualDeep(before[0], p.view().rects[0]); try testing.expectEqualDeep(before[far], p.view().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.view().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. 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); // Another session on the same state file lists none of this one's. const w = try testWindow(dir); defer w.deinit(); try testing.expectEqual(@as(usize, 0), w.store.closed.items.len); }