1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
|
//! What outlives a pane: Recent's file, which every session of the user's
//! shares, and the panes this session closed, which Panes brings back.
//!
//! Recent's file (Options.recent_path, $XDG_STATE_HOME/pardes/recent) is
//! the list, a line a file, the newest first: `<line> <col> <aline> <acol>
//! <path>` (recent.zig). A session reads it at start, and again on use
//! (Recent, /recent) when a stat says another session wrote it since. A
//! step that changed the list writes it at its end: `flock` on the file
//! (held by another session, the next step tries again: a frame never
//! waits for one), the file read again under this session's changes
//! (`recent.List.merge`) when another session wrote it since, and the
//! whole list written over it in place. A line that is no entry is passed
//! over, so a missing, unreadable or foreign file is an empty list (and a
//! line a crash tore, one entry fewer): pardes always starts.
//!
//! The closed panes are this session's alone, and in memory: each 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.
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;
/// 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;
/// Recent's file is read this far at most (200 lines of the longest paths).
const max_file = 1 << 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,
};
/// Recent's file; null, nothing is kept past the session.
path: ?[]const u8 = null,
/// The file as this session last read or wrote it: a stat that says
/// otherwise is another session's write.
inode: std.Io.File.INode = 0,
size: u64 = 0,
mtime: i96 = 0,
/// Read once at start (or there was none): only then is it written, so a
/// session never writes over a list it did not read.
loaded: bool = false,
/// 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,
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
/// file 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 Recent changes between these, never inside one.
const host = pardes.tape.out;
const cwd = std.Io.Dir.cwd;
const File = std.Io.File;
const Dir = std.Io.Dir;
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;
}
// ---- Recent's file ----
/// Recent read at start: the file, and the store it replaced.
pub fn load(s: *Store, p: *Pardes) void {
const path = s.path orelse return;
s.loaded = true;
s.refresh(p);
// ponytail: the store this file replaced (until 2026-11-05), a log
// whose `@r` records' bodies are this file's lines, oldest first: read
// once more, over the file's list, while it is the newer of the two
// (an older pardes still running writes it, so it is left there).
const io = ioOf();
var old_buf: [4096 + 8]u8 = undefined;
const old = std.fmt.bufPrint(&old_buf, "{s}/store", .{std.fs.path.dirname(path) orelse return}) catch return;
const st = host(Dir.statFile, .{ cwd(), io, old, Dir.StatFileOptions{} }) catch return;
if (st.mtime.nanoseconds <= s.mtime) return;
const gpa = p.recentGpa();
const bytes = host(Dir.readFileAlloc, .{ cwd(), io, old, gpa, std.Io.Limit.limited(128 << 20) }) catch return;
defer gpa.free(bytes);
var lines = std.mem.splitScalar(u8, bytes, '\n');
while (lines.next()) |line| {
const e = recent.List.parseLine(line) orelse continue;
const open = if (p.recent.get(e.path)) |here| here.open else false;
p.recent.opened(gpa, e.path);
p.recent.closed(gpa, e.path, e.nums[0], e.nums[1], e.nums[2], e.nums[3]);
// Open here, it stays open: the cap never drops it.
if (open) p.recent.opened(gpa, e.path);
}
}
/// On use: what other sessions wrote since this one last read or wrote
/// the file, read. One stat when nothing changed.
pub fn refresh(s: *Store, p: *Pardes) void {
const path = s.path orelse 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.size and st.mtime.nanoseconds == s.mtime) return;
const file = host(Dir.openFile, .{ cwd(), io, path, Dir.OpenFileOptions{} }) catch return;
defer host(File.close, .{ file, io });
s.read(p, file);
}
/// `file` read into Recent, under this session's changes, when it is not
/// as this session last read or wrote it; then noted as read.
fn read(s: *Store, p: *Pardes, file: File) void {
const io = ioOf();
const st = host(File.stat, .{ file, io }) catch return;
if (st.inode == s.inode and st.size == s.size and st.mtime.nanoseconds == s.mtime) return;
const gpa = p.recentGpa();
const buf = gpa.alloc(u8, @min(st.size, max_file)) catch return;
defer gpa.free(buf);
const n = host(File.readPositionalAll, .{ file, io, buf, 0 }) catch return;
p.recent.merge(gpa, buf[0..n]);
s.inode = st.inode;
s.size = st.size;
s.mtime = st.mtime.nanoseconds;
}
/// Recent written out when it 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 {
if (!p.recent.dirty) return;
if (s.path) |path| {
if (!s.loaded) return;
// Another session's lock: the next step tries again.
if (!s.write(p, path, wait)) return;
}
// Written, or nowhere to write it (or the disk refused it: kept for
// no one, as before).
for (p.recent.entries.items) |*e| e.changed = false;
p.recent.dirty = false;
}
/// Under the file's lock: the file read again when another session wrote
/// it since, this session's changes over it, the whole list written in
/// place. False when another session holds the lock (and `wait` is not
/// set).
fn write(s: *Store, p: *Pardes, path: []const u8, wait: bool) bool {
const io = ioOf();
const file = host(Dir.createFile, .{ cwd(), io, path, Dir.CreateFileOptions{ .read = true, .truncate = false } }) catch return true;
// Closing it lets go of the lock.
defer host(File.close, .{ file, io });
if (wait) {
host(File.lock, .{ file, io, File.Lock.exclusive }) catch return true;
} else if (!(host(File.tryLock, .{ file, io, File.Lock.exclusive }) catch return true)) return false;
s.read(p, file);
p.recent.changedFirst();
const gpa = p.recentGpa();
var text: std.Io.Writer.Allocating = .init(gpa);
defer text.deinit();
const entries = p.recent.entries.items;
for (entries[0..@min(entries.len, recent.cap)]) |e| recent.List.writeEntry(e, &text.writer) catch return true;
// In place: a reader that meets it half written reads it again once
// its stat moves on, and a line a crash tore is passed over. Out of
// disk, it is kept for no one, as before.
host(File.writePositionalAll, .{ file, io, text.written(), 0 }) catch return true;
host(File.setLength, .{ file, io, text.written().len }) catch return true;
// This session's own write is not read back at the next refresh.
const st = host(File.stat, .{ file, io }) catch return true;
s.inode = st.inode;
s.size = st.size;
s.mtime = st.mtime.nanoseconds;
return true;
}
// ---- 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.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 {
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.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.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<serial>` 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;
/// Recent's 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, "recent" });
}
fn testWindow(path: []const u8) !*Pardes {
return Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 48, .recent_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" {
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();
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, 0, 0);
a.saveRecent();
// Each has both, in the order they were written: 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 file's lock: b's change waits in memory...
const lock = try std.Io.Dir.cwd().openFile(testing.io, path, .{ .mode = .read_write });
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 reads 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: Recent's file holds the newest 200, the newest first, and a window started on it reads them so" {
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();
var name: [32]u8 = undefined;
for (0..recent.cap + 100) |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, 0, 0);
a.saveRecent();
}
try testing.expectEqual(@as(usize, recent.cap), a.recent.entries.items.len);
const text = try std.Io.Dir.cwd().readFileAlloc(testing.io, path, testing.allocator, .limited(1 << 20));
defer testing.allocator.free(text);
try testing.expectEqual(@as(usize, recent.cap), std.mem.count(u8, text, "\n"));
try testing.expect(std.mem.startsWith(u8, text, "1 1 0 0 /w/file299.zig\n"));
const b = try testWindow(path);
defer b.deinit();
try testing.expectEqual(@as(usize, recent.cap), b.recent.entries.items.len);
try testing.expectEqualStrings("/w/file299.zig", b.recent.entries.items[0].path);
try testing.expectEqualStrings("/w/file100.zig", b.recent.entries.items[recent.cap - 1].path);
}
test "store: a foreign file never stops pardes: its lines that are Recent's are read, the next write mends it" {
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 list at all\n7 3 0 0 /w/ok.zig\n@r 9 zz\n1 2 /w/short.zig\n" });
const a = try testWindow(path);
defer a.deinit();
try testing.expectEqual(@as(usize, 1), a.recent.entries.items.len);
try testing.expectEqual(@as(u32, 7), a.recent.get("/w/ok.zig").?.line);
a.recent.opened(a.recentGpa(), "/w/a.zig");
a.saveRecent();
const text = try std.Io.Dir.cwd().readFileAlloc(testing.io, path, testing.allocator, .limited(1 << 20));
defer testing.allocator.free(text);
try testing.expectEqualStrings("0 0 0 0 /w/a.zig\n7 3 0 0 /w/ok.zig\n", text);
}
test "store: the store Recent's file replaced is read once, its newest records first, over the file's list" {
var tmp = testing.tmpDir(.{});
defer tmp.cleanup();
const path = try testPath(&tmp);
defer testing.allocator.free(path);
// An older list, then the store written after it: its records oldest
// first, a closed pane's dump between them.
try std.Io.Dir.cwd().writeFile(testing.io, .{ .sub_path = path, .data = "5 5 0 0 /w/listed.zig\n" });
const store = try std.fmt.allocPrint(testing.allocator, "{s}/store", .{std.fs.path.dirname(path).?});
defer testing.allocator.free(store);
try std.Io.Dir.cwd().writeFile(testing.io, .{ .sub_path = store, .data = "@h 31 3c6ce28a\npardes-store 1 ca2a9583be7d1ba3\n@r 20 0\n1 1 0 0 /w/old.zig\n@c 12 0\n0 1 7 0 0 0 0 9\t/w/x\n.{}\n@r 21 0\n2 2 0 0 /w/both.zig\n@r 20 0\n9 9 0 0 /w/old.zig\n" });
{
// Made newer than the list, as on a disk where both were written.
const f = try std.Io.Dir.cwd().openFile(testing.io, store, .{ .mode = .read_write });
defer f.close(testing.io);
const st = try f.stat(testing.io);
try f.setTimestamps(testing.io, .{ .modify_timestamp = .{ .new = .{ .nanoseconds = st.mtime.nanoseconds + std.time.ns_per_s } } });
}
const a = try testWindow(path);
defer a.deinit();
try testing.expectEqual(@as(usize, 3), a.recent.entries.items.len);
try testing.expectEqualStrings("/w/old.zig", a.recent.entries.items[0].path);
try testing.expectEqual(@as(u32, 9), a.recent.entries.items[0].line);
try testing.expectEqualStrings("/w/both.zig", a.recent.entries.items[1].path);
try testing.expectEqualStrings("/w/listed.zig", a.recent.entries.items[2].path);
// Written to the file at the step's end; read from it alone after.
a.saveRecent();
const b = try testWindow(path);
defer b.deinit();
for (a.recent.entries.items, b.recent.entries.items) |x, y| try testing.expectEqualStrings(x.path, y.path);
}
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.active);
const id = p.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.active));
try testing.expectEqualStrings("scratch 69\n", p.panes[p.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();
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"));
p.newScratchBelow(0);
try testing.expect(layout.splitColumn(p, p.active, p.active, false));
const far = p.active;
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]);
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.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 file 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");
|