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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
|
//! docs/typ/reference.typ, Panes <panes>.
const std = @import("std");
const Session = @import("session").Session;
test "panes: a collapsed pane's ctl says so" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const first = try s.focused();
_ = try s.newPane();
try s.write(s.at("/pane/{d}/ctl", .{first}), "Collapse\n");
try std.testing.expect(std.mem.indexOf(u8, try s.read(s.at("/pane/{d}/ctl", .{first})), " collapsed ") != null);
}
test "panes: a pane's ctl read ends with its mode, as its tag's box shows it" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const ctl = s.at("/pane/{d}/ctl", .{try s.focused()});
try std.testing.expect(std.mem.endsWith(u8, try s.read(ctl), " mode:normal "));
try s.keys("i");
try std.testing.expect(std.mem.endsWith(u8, try s.read(ctl), " mode:insert "));
try s.keys("\x1bv");
try std.testing.expect(std.mem.endsWith(u8, try s.read(ctl), " mode:select "));
}
test "panes: the ctl word name refuses a leading blank, as the name file does" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const ctl = s.at("/pane/{d}/ctl", .{try s.focused()});
try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "name /tmp/lead.txt\n"));
try std.testing.expectEqualStrings("bad character in file name: a blank at its start", s.said);
try s.write(ctl, "name /tmp/ok.txt\n");
try s.expect(s.at("/pane/{d}/name", .{try s.focused()}), "/tmp/ok.txt\n");
}
test "panes: Putall saves every pane with unsaved edits to a file, one each, and leaves the rest" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "clean.txt", .data = "clean\n" });
var dir_buf: [4096]u8 = undefined;
const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)];
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const first = try s.focused();
try s.write(s.at("/pane/{d}/name", .{first}), "/tmp/pardes-putall-a.txt\n");
try s.write(s.at("/pane/{d}/body", .{first}), "edited\n");
// A file opened from disk, unedited.
try s.write(s.at("/pane/{d}/look", .{first}), s.at("{s}/clean.txt\n", .{dir}));
try std.testing.expectEqual(@as(usize, 0), s.saved);
try s.write("/ctl", "Putall\n");
try std.testing.expectEqual(@as(usize, 1), s.saved);
}
fn has(hay: []const u8, needle: []const u8) bool {
return std.mem.indexOf(u8, hay, needle) != null;
}
test "panes: focus reads the pane with the keyboard, and a serial written to it moves the keyboard there, and only that" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const first = try s.focused();
const second = try s.newPane();
try s.expect("/focus", s.at("{d}\n", .{second}));
// A stat says the length a read gives.
try std.testing.expectEqual(@as(u64, s.at("{d}\n", .{second}).len), (try s.stat("/focus")).size);
try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{first})), " notcurrent "));
// A column's tag has the keyboard: no pane does, and a write takes it back.
try s.write("/ctl", "Newcol\n");
try s.expect("/focus", "");
try std.testing.expectEqual(@as(u64, 0), (try s.stat("/focus")).size);
try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " notcurrent "));
try s.write("/focus", s.at("{d}\n", .{first}));
try s.expect("/focus", s.at("{d}\n", .{first}));
try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{first})), " current "));
try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " notcurrent "));
try std.testing.expectEqual(@as(u16, 2), try s.refused("/focus", "99999\n"));
try std.testing.expectEqualStrings("no such pane", s.said);
for ([_][]const u8{ "", "two", "-1", "1 2" }) |bad| try std.testing.expectEqual(@as(u16, 22), try s.refused("/focus", bad));
try s.expect("/focus", s.at("{d}\n", .{first}));
// Focus gives the keyboard only: a folded pane stays folded.
try s.write(s.at("/pane/{d}/ctl", .{second}), "Collapse\n");
try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " collapsed "));
try s.write("/focus", s.at("{d}", .{second}));
try s.expect("/focus", s.at("{d}\n", .{second}));
try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " collapsed "));
}
test "panes: the pane ctl takes acme's verbs and the pane's builtins, and refuses the rest in Plan 9's words" {
var s = try Session.initFile(std.testing.allocator, "one\ntwo\n");
defer s.deinit();
const n = try s.focused();
const ctl = s.at("/pane/{d}/ctl", .{n});
// A builtin is its own capitalised word: `GET` and `DEL` are none.
for ([_][]const u8{ "lock x", "bogus", "DEL", "GET", "get x", "look /tmp", "exec Del" }) |bad| {
try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, s.at("{s}\n", .{bad})));
try std.testing.expectEqualStrings(s.at("unknown control message \"{s}\"", .{std.mem.sliceTo(bad, ' ')}), s.said);
}
_ = try s.refused(ctl, "Newcol\n");
try std.testing.expectEqualStrings("not a window control message \"Newcol\": write it to /ctl", s.said);
_ = try s.refused(ctl, "Verbose off\n");
try std.testing.expectEqualStrings("not a window control message \"Verbose\": write it to /ctl", s.said);
_ = try s.refused(ctl, "Collapse now\n");
try std.testing.expectEqualStrings("wrong #args in control message \"Collapse\"", s.said);
// Checked whole first: a bad line anywhere and nothing ran.
try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "Msg first\nbogus\n"));
// A pane's builtin runs at that pane.
try s.write(ctl, "Msg from ctl\n");
try std.testing.expect(has(try s.read("/log"), s.at("msg {d} from ctl\n", .{n})));
// One that means nothing bare is refused before anything runs.
_ = try s.refused(ctl, "Find\n");
try std.testing.expectEqualStrings("wrong #args in control message \"Find\"", s.said);
try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "Msg first\nMsg\n"));
try std.testing.expect(!has(try s.read("/log"), "first"));
// Save with no name on a scratch would ask for one: refused, not asked.
const other = try s.newPane();
_ = try s.refused(s.at("/pane/{d}/ctl", .{other}), "Save\n");
try std.testing.expectEqualStrings("control message needs its argument \"Save\"", s.said);
// A line after the one that closed the pane has nowhere to run.
try std.testing.expectEqual(@as(u16, 2), try s.refused(s.at("/pane/{d}/ctl", .{other}), "Del\nMsg after\n"));
try std.testing.expectError(error.Refused, s.read(s.at("/pane/{d}/ctl", .{other})));
}
test "panes: a second lock is refused until the holder unlocks or closes, and binds nobody else" {
var s = try Session.initFile(std.testing.allocator, "one\ntwo\n");
defer s.deinit();
const n = try s.focused();
const ctl = s.at("/pane/{d}/ctl", .{n});
const a = try s.hold(ctl, 2);
const b = try s.hold(ctl, 1);
// An open that only reads the status line holds nothing to lock with.
const reader = try s.hold(ctl, 0);
try std.testing.expect(try s.writeFid(reader, "lock\n") != 0);
s.clunk(reader);
try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "lock\n"));
try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "lock\n"));
// Another open's lock is refused at once, and so is its unlock; writes
// to the pane's other files are not.
try std.testing.expectEqual(@as(u16, 16), try s.writeFid(b, "lock\n"));
try std.testing.expectEqualStrings("file in use: another open of this ctl holds the lock", s.said);
try std.testing.expectEqual(@as(u16, 22), try s.writeFid(b, "unlock\n"));
try std.testing.expectEqualStrings("pane not locked by this open", s.said);
try s.write(s.at("/pane/{d}/addr", .{n}), "1");
try s.write(s.at("/pane/{d}/data", .{n}), "ONE\n");
try s.expect(s.at("/pane/{d}/body", .{n}), "ONE\ntwo\n");
// Once the holder unlocks, the other's try takes it.
try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "unlock\n"));
try std.testing.expectEqual(@as(u16, 0), try s.writeFid(b, "lock\n"));
try std.testing.expectEqual(@as(u16, 16), try s.writeFid(a, "lock\nunlock\n"));
// Closing the holder gives it up.
s.clunk(b);
try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "lock\nunlock\nlock\n"));
try std.testing.expectEqual(@as(u16, 22), try s.writeFid(a, "unlock\nunlock\n"));
// Still a's: another open is refused it.
const c = try s.hold(ctl, 1);
try std.testing.expectEqual(@as(u16, 16), try s.writeFid(c, "lock\n"));
s.clunk(c);
// The lock lives and dies with the pane.
const other = try s.newPane();
const d = try s.hold(s.at("/pane/{d}/ctl", .{other}), 1);
try std.testing.expectEqual(@as(u16, 0), try s.writeFid(d, "lock\n"));
try s.write(s.at("/pane/{d}/ctl", .{other}), "Del\n");
try std.testing.expectEqual(@as(u16, 2), try s.writeFid(d, "lock\n"));
s.clunk(d);
s.clunk(a);
// a's close gave its lock up too.
const e = try s.hold(ctl, 1);
try std.testing.expectEqual(@as(u16, 0), try s.writeFid(e, "lock\n"));
s.clunk(e);
}
test "panes: an unsaved file's tag is its name alone, its dirty file says it is unsaved" {
var s = try Session.initFile(std.testing.allocator, "before\n");
defer s.deinit();
const n = try s.focused();
const tag = s.at("/pane/{d}/tag", .{n});
const clean = try s.read(tag);
try s.write(s.at("/pane/{d}/body", .{n}), "after\n");
try std.testing.expectEqualStrings(clean, try s.read(tag));
try std.testing.expect(std.mem.indexOf(u8, clean, " *") == null);
try s.expect(s.at("/pane/{d}/dirty", .{n}), "1\n");
}
test "panes: a pane closed while its question stands ends the question in the log" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const top = try s.focused();
try s.write(s.at("/pane/{d}/exec", .{top}), "New\n");
const closing = try s.focused();
try s.write(s.at("/pane/{d}/exec", .{closing}), "New\n");
// A middle pane: Del from a key asks which side.
try s.write("/focus", s.at("{d}\n", .{closing}));
try s.keys(" d");
try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), s.at("ask {d} del k j\n", .{closing})) != null);
// A bare answer says what this question takes.
const ctl = s.at("/pane/{d}/ctl", .{closing});
_ = try s.refused(ctl, "answer\n");
try std.testing.expectEqualStrings("answer takes k j or -", s.said);
try s.write(ctl, "Del\n");
try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), s.at("answer {d} -\ndel {d}", .{ closing, closing })) != null);
}
test "panes: focus on a closed pane goes to the pane that takes its place, not the one focused before" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const left = try s.focused();
try s.write("/ctl", "Newcol\n");
try s.write(s.at("/col/{d}/ctl", .{try s.column(1)}), "New\n");
const right = try s.focused();
try s.write(s.at("/pane/{d}/exec", .{left}), "New\n");
const low = try s.focused();
// `right` was focused just before `low`, but `left` grows into `low`'s
// rows, so `left` gets the focus.
try s.write(s.at("/pane/{d}/ctl", .{low}), "Del\n");
try std.testing.expectEqual(left, try s.focused());
// Closing a pane that has no focus leaves the focus where it is.
try s.write(s.at("/pane/{d}/exec", .{left}), "New\n");
const other = try s.focused();
try s.write("/focus", s.at("{d}\n", .{right}));
try s.write(s.at("/pane/{d}/ctl", .{other}), "Del\n");
try std.testing.expectEqual(right, try s.focused());
// Delcol: the column that takes the width holds, level with the closed
// pane, the pane that takes its place; `right` was focused just before.
try s.write(s.at("/pane/{d}/exec", .{right}), "New\n");
const right_low = try s.focused();
try s.write(s.at("/pane/{d}/exec", .{left}), "New\n");
const left_low = try s.focused();
try s.write("/focus", s.at("{d}\n", .{right}));
try s.write("/focus", s.at("{d}\n", .{left_low}));
try s.write(s.at("/col/{d}/ctl", .{try s.column(0)}), "Delcol\n");
try std.testing.expectEqual(right_low, try s.focused());
}
test "panes: Collapse keeps the text and folds the pane to its tag, and again restores it" {
var s = try Session.initFile(std.testing.allocator, "keep this text\n");
defer s.deinit();
var t = try Session.initTerm(std.testing.allocator);
defer t.deinit();
const n = try s.focused();
for ([_][]const u8{
try s.read(s.at("/pane/{d}/tag", .{n})),
try s.read(s.at("/pane/{d}/tag", .{try s.newPane()})),
try t.read(t.at("/pane/{d}/tag", .{try t.focused()})),
}) |tag| try std.testing.expect(std.mem.endsWith(u8, tag, " Collapse Del"));
try s.write(s.at("/pane/{d}/ctl", .{try s.focused()}), "Del\n");
const ctl = s.at("/pane/{d}/ctl", .{n});
try s.write(ctl, "Collapse\n");
// Folded to its tag: no text drawn, no cursor in it, the text kept.
try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), " collapsed ") != null);
try std.testing.expect(std.mem.indexOf(u8, try s.screen(), "keep this text") == null);
try std.testing.expectEqual(null, try s.cursor());
try s.expect(s.at("/pane/{d}/body", .{n}), "keep this text\n");
try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n");
// Its tag still takes the keyboard, the cursor on the tag's row.
try s.keys(":");
try std.testing.expectEqual((try s.where("/test.txt"))[1], (try s.cursor()).?[1]);
try s.write(ctl, "Collapse\n");
try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), " collapsed ") == null);
try std.testing.expectEqual((try s.where("/test.txt"))[1] + 1, (try s.where("1 keep this text"))[1]);
}
test "panes: a pane its whole text fits in never scrolls to keep a margin" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "f\n" });
var dir: [4096]u8 = undefined;
const at = dir[0..try tmp.dir.realPath(std.testing.io, &dir)];
var s = try Session.initFile(std.testing.allocator, "one\ntwo\nthree\n");
defer s.deinit();
const n = try s.focused();
// A second pane under it, as acme's placement puts one: right under the
// three lines, which leaves the file exactly three body rows.
try s.write(s.at("/pane/{d}/look", .{n}), s.at("{s}/f.txt\n", .{at}));
try s.write("/focus", s.at("{d}\n", .{n}));
try s.keys("jj");
const top = (try s.where("/test.txt"))[1];
try std.testing.expectEqual(top + 4, (try s.where(at[0..@min(at.len, 20)]))[1]);
// On the last line, and line 1 still the first drawn.
try std.testing.expectEqual(.{ @as(u32, 8), @as(u32, 8) }, try s.range(s.at("/pane/{d}/dot", .{n})));
try std.testing.expectEqual(top + 1, (try s.where("1 one"))[1]);
}
/// `name` in `dir`: a one-page PDF with no outline and no text.
fn plainPdf(dir: std.Io.Dir, name: []const u8) !void {
const objects = [_][]const u8{
"<< /Type /Catalog /Pages 2 0 R >>",
"<< /Type /Pages /Count 1 /Kids [3 0 R] >>",
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 300] /Resources << >> >>",
};
var buf: [1024]u8 = undefined;
var w: std.Io.Writer = .fixed(&buf);
var at: [objects.len]usize = undefined;
try w.writeAll("%PDF-1.4\n");
for (objects, 0..) |object, i| {
at[i] = w.end;
try w.print("{d} 0 obj\n{s}\nendobj\n", .{ i + 1, object });
}
const xref = w.end;
try w.print("xref\n0 {d}\n0000000000 65535 f \n", .{objects.len + 1});
for (at) |off| try w.print("{d:0>10} 00000 n \n", .{off});
try w.print("trailer\n<< /Size {d} /Root 1 0 R >>\nstartxref\n{d}\n%%EOF\n", .{ objects.len + 1, xref });
try dir.writeFile(std.testing.io, .{ .sub_path = name, .data = w.buffered() });
}
test "panes: a PDF's body is the text of the page shown, each line of it a line, never a heading run into the text under it" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const n = try s.lookFile("docs/design.pdf");
const body = s.at("/pane/{d}/body", .{n});
try std.testing.expect(std.mem.startsWith(u8, try s.read(body), "Pardes: A Text Environment\nArchitecture and implementation of the rewrite\n"));
try s.write(s.at("/pane/{d}/look", .{n}), ":2\n");
try std.testing.expect(!std.mem.startsWith(u8, try s.read(body), "Pardes: A Text Environment\n"));
}
test "panes: PdfTint steps a PDF's tint, read in its ctl and never in its tag, and only a PDF pane takes it" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const text = try s.focused();
const n = try s.lookFile("docs/design.pdf");
const ctl = s.at("/pane/{d}/ctl", .{n});
const tag = try s.read(s.at("/pane/{d}/tag", .{n}));
try std.testing.expect(std.mem.endsWith(u8, tag, " PdfSections PdfTint PdfFit Collapse Del"));
for ([_][]const u8{ "full", "disabled", "filtered" }) |want| {
try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), "fit:width ") != null);
try s.write(ctl, "PdfTint\n");
try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), s.at(" tint:{s} ", .{want})) != null);
try s.expect(s.at("/pane/{d}/tag", .{n}), tag);
// Another pane's PdfTint is refused and leaves the PDF's alone.
try std.testing.expectEqual(@as(u16, 5), try s.refused(s.at("/pane/{d}/ctl", .{text}), "PdfTint\n"));
try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), s.at(" tint:{s} ", .{want})) != null);
}
}
test "panes: PdfSections on a PDF with no outline says so and opens nothing" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try plainPdf(tmp.dir, "plain.pdf");
var buf: [4096]u8 = undefined;
const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const n = try s.lookFile(s.at("{s}/plain.pdf", .{dir}));
try std.testing.expectEqual(@as(u32, 1), (try s.pdfPage(n))[1]);
const index = try s.read("/index");
for (0..2) |_| {
_ = try s.refused(s.at("/pane/{d}/exec", .{n}), "PdfSections\n");
try s.expect("/index", index);
}
try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "PdfSections: this PDF has no outline") != null);
}
test "panes: a Mini that cannot read its file leaves the panes, the last Mini's snapshot and the keyboard as they were" {
var s = try Session.initFile(std.testing.allocator, "abc\n");
defer s.deinit();
const text = try s.focused();
const exec = s.at("/pane/{d}/exec", .{text});
try s.write(exec, s.at("Mini /virtual/pane/{d}/body\n", .{text}));
const mini = try s.focused();
try std.testing.expect(mini != text);
const body = try s.read(s.at("/pane/{d}/body", .{mini}));
const index = try s.read("/index");
try std.testing.expectEqual(@as(u16, 2), try s.refused(exec, "Mini /virtual/does-not-exist\n"));
try std.testing.expectEqual(mini, try s.focused());
try s.expect("/index", index);
try s.expect(s.at("/pane/{d}/body", .{mini}), body);
}
test "panes: Mini with no argument is wrong #args, EINVAL, as every word that needs one" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/ctl", .{try s.focused()}), "Mini\n"));
try std.testing.expectEqualStrings("wrong #args in control message \"Mini\"", s.said);
}
test "panes: the ctl word name with nothing after it says it names nothing" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
try std.testing.expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/ctl", .{try s.focused()}), "name \n"));
try std.testing.expectEqualStrings("invalid file name: name takes the name, and there is none", s.said);
}
test "panes: ctl reads acme's fields in acme's order, undo and redo flags included, then pardes's own" {
var s = try Session.initFile(std.testing.allocator, "one\n");
defer s.deinit();
const n = try s.focused();
const ctl = s.at("/pane/{d}/ctl", .{n});
const field = struct {
fn at(text: []const u8, i: usize) []const u8 {
var it = std.mem.tokenizeScalar(u8, text, ' ');
for (0..i) |_| _ = it.next();
return it.next().?;
}
}.at;
try std.testing.expectEqualStrings("0", field(try s.read(ctl), 8)); // nothing to undo
try std.testing.expectEqualStrings("0", field(try s.read(ctl), 9)); // nothing to redo
const current = field(try s.read(ctl), 10);
try std.testing.expect(std.mem.eql(u8, current, "current") or std.mem.eql(u8, current, "notcurrent"));
try s.write(s.at("/pane/{d}/body", .{n}), "two\n");
try std.testing.expectEqualStrings("1", field(try s.read(ctl), 8));
try s.write(ctl, "Undo\n");
try std.testing.expectEqualStrings("1", field(try s.read(ctl), 9));
}
/// A fresh directory's absolute path, kept until deinit.
fn tmpPath(s: *Session, tmp: *std.testing.TmpDir) ![]const u8 {
var buf: [4096]u8 = undefined;
return s.at("{s}", .{buf[0..try tmp.dir.realPath(std.testing.io, &buf)]});
}
/// The `i`th blank-separated field of a pane's ctl read.
fn ctlField(s: *Session, n: u32, i: usize) ![]const u8 {
var it = std.mem.tokenizeScalar(u8, try s.read(s.at("/pane/{d}/ctl", .{n})), ' ');
for (0..i) |_| _ = it.next();
return it.next() orelse error.NoField;
}
test "panes: get reloads the pane from disk and reports a missing file without losing dirty contents" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "note.txt", .data = "from disk\n" });
var s = try Session.initFile(std.testing.allocator, "in memory\n");
defer s.deinit();
const dir = try tmpPath(&s, &tmp);
const n = try s.focused();
const ctl = s.at("/pane/{d}/ctl", .{n});
const body = s.at("/pane/{d}/body", .{n});
try s.write(s.at("/pane/{d}/name", .{n}), s.at("{s}/note.txt\n", .{dir}));
try s.write(ctl, "get\n");
try s.expect(body, "from disk\n");
try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n");
try std.testing.expectEqualStrings("1", try ctlField(&s, n, 8)); // an undo step
// Renamed but never edited, it was clean: nothing thrown away, nothing
// logged as reloaded over edits.
try std.testing.expect(!has(try s.read("/log"), s.at("changed {d} reloaded\n", .{n})));
try s.write(s.at("/pane/{d}/name", .{n}), s.at("{s}/missing.txt\n", .{dir}));
try s.write(body, "unsaved\n");
// Unsaved text is asked about once, as acme's get asks winclean.
try std.testing.expectEqual(@as(u16, 5), try s.refused(ctl, "get\n"));
try std.testing.expect(std.mem.endsWith(u8, s.said, ": Modified (get again to discard)"));
try std.testing.expect(has(try s.read("/log"), s.at("unsaved {d} {s}/missing.txt\n", .{ n, dir })));
try std.testing.expectEqual(@as(u16, 2), try s.refused(ctl, "get\n"));
try s.expect(body, "from disk\nunsaved\n");
try s.expect(s.at("/pane/{d}/dirty", .{n}), "1\n");
}
test "panes: Get reads the pane's file again, refusing unsaved edits once, and Get file loads that file into the pane" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "note.txt", .data = "from disk\n" });
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "other.txt", .data = "the other\n" });
var s = try Session.initFile(std.testing.allocator, "in memory\n");
defer s.deinit();
const dir = try tmpPath(&s, &tmp);
const n = try s.focused();
const exec = s.at("/pane/{d}/exec", .{n});
const body = s.at("/pane/{d}/body", .{n});
try s.write(s.at("/pane/{d}/name", .{n}), s.at("{s}/note.txt\n", .{dir}));
try s.write(exec, "Get\n");
try s.expect(body, "from disk\n");
try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n");
// Edited: asked once, then read again.
try s.write(body, "unsaved\n");
try std.testing.expect(try s.refused(exec, "Get\n") != 0);
try s.expect(body, "from disk\nunsaved\n");
try s.write(exec, "Get\n");
try s.expect(body, "from disk\n");
// Get file: that file, by a name from the pane's directory.
try s.write(exec, "Get other.txt\n");
try s.expect(body, "the other\n");
try s.expect(s.at("/pane/{d}/name", .{n}), s.at("{s}/other.txt\n", .{dir}));
try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n");
}
test "panes: Undo after a load under another name (Get file, Edit e) puts the old name back with the old text and its clean state; Putall then writes nothing over the loaded file" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "other.txt", .data = "other\n" });
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "keep.txt", .data = "keep\n" });
for ([_][]const u8{ "Get keep.txt\n", "Edit e keep.txt\n" }) |load| {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const dir = try tmpPath(&s, &tmp);
const n = try s.focused();
const exec = s.at("/pane/{d}/exec", .{n});
const body = s.at("/pane/{d}/body", .{n});
const name = s.at("/pane/{d}/name", .{n});
const dirty = s.at("/pane/{d}/dirty", .{n});
try s.write(name, s.at("{s}/other.txt\n", .{dir}));
try s.write(exec, "Get\n");
try s.expect(body, "other\n");
try s.expect(dirty, "0\n");
// Loaded: keep.txt's text under its name.
try s.write(exec, load);
try s.expect(body, "keep\n");
try s.expect(name, s.at("{s}/keep.txt\n", .{dir}));
if (std.mem.startsWith(u8, load, "Get")) try s.expect(dirty, "0\n");
// Undo: other.txt's text, other.txt's name, clean as it was.
try s.write(exec, "Undo\n");
try s.expect(body, "other\n");
try s.expect(name, s.at("{s}/other.txt\n", .{dir}));
try s.expect(dirty, "0\n");
// Putall saves nothing, and nothing lands on keep.txt.
try s.write("/ctl", "Putall\n");
try std.testing.expectEqual(@as(usize, 0), s.saved);
// Redo: keep.txt again.
try s.write(exec, "Redo\n");
try s.expect(name, s.at("{s}/keep.txt\n", .{dir}));
try s.expect(body, "keep\n");
}
}
test "panes: a Get file that fails changes nothing: no such file, a directory, or unsaved edits asked about once" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "here.txt", .data = "here\n" });
try tmp.dir.createDirPath(std.testing.io, "sub");
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const dir = try tmpPath(&s, &tmp);
const n = try s.focused();
const exec = s.at("/pane/{d}/exec", .{n});
const body = s.at("/pane/{d}/body", .{n});
const name = s.at("/pane/{d}/name", .{n});
const here = s.at("{s}/here.txt\n", .{dir});
try s.write(name, here);
try s.write(exec, "Get\n");
for ([_][]const u8{ "Get nosuch.txt\n", "Get sub\n" }) |line| {
try std.testing.expect(try s.refused(exec, line) != 0);
try s.expect(name, here);
try s.expect(body, "here\n");
}
// No undo step left by them: one Undo takes back the first Get.
try s.write(exec, "Undo\n");
try s.expect(body, "x\n");
try s.write(exec, "Redo\n");
// Edited, the first Get asks and renames nothing.
try s.write(body, "more\n");
try std.testing.expect(try s.refused(exec, "Get other.txt\n") != 0);
try s.expect(name, here);
try s.expect(body, "here\nmore\n");
}
/// The serials /index lists with a name ending `suffix`, in its order.
fn panesNamed(s: *Session, suffix: []const u8) ![]u32 {
var out: std.ArrayList(u32) = .empty;
var rows = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n');
while (rows.next()) |line| {
const name = line[0 .. std.mem.lastIndexOfScalar(u8, line, ' ') orelse continue];
if (std.mem.endsWith(u8, name, suffix)) try out.append(s.arena.allocator(), try std.fmt.parseInt(u32, line[0..std.mem.indexOfScalar(u8, line, ' ').?], 10));
}
return out.items;
}
test "panes: Zerox makes a second pane on one buffer: one text, undo and dirty state, its own cursor; Del of one asks nothing" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var s = try Session.initFile(std.testing.allocator, "one\ntwo\nthree\n");
defer s.deinit();
const dir = try tmpPath(&s, &tmp);
const a = try s.focused();
try s.write(s.at("/pane/{d}/name", .{a}), s.at("{s}/z.txt\n", .{dir}));
try s.write(s.at("/pane/{d}/exec", .{a}), "Zerox\n");
// Both in the index, each its serial, one name.
const twins = try panesNamed(&s, "/z.txt");
try std.testing.expectEqual(@as(usize, 2), twins.len);
const b = if (twins[0] == a) twins[1] else twins[0];
// b's cursor on `three`; a line written above it in a moves it down.
try s.write(s.at("/pane/{d}/dot", .{b}), "9 9");
try s.write(s.at("/pane/{d}/addr", .{a}), "0");
try s.write(s.at("/pane/{d}/data", .{a}), "zero\n");
try s.expect(s.at("/pane/{d}/body", .{b}), "zero\none\ntwo\nthree\n");
try std.testing.expectEqual(.{ @as(u32, 14), @as(u32, 14) }, try s.range(s.at("/pane/{d}/dot", .{b})));
try s.expect(s.at("/pane/{d}/dirty", .{a}), "1\n");
try s.expect(s.at("/pane/{d}/dirty", .{b}), "1\n");
// A write through b is a's too; Undo in a takes it back from both.
try s.write(s.at("/pane/{d}/body", .{b}), "four\n");
try s.expect(s.at("/pane/{d}/body", .{a}), "zero\none\ntwo\nthree\nfour\n");
try s.write(s.at("/pane/{d}/exec", .{a}), "Undo\n");
try s.expect(s.at("/pane/{d}/body", .{b}), "zero\none\ntwo\nthree\n");
// Del of a asks nothing: b holds the text.
try s.write(s.at("/pane/{d}/ctl", .{a}), "Del\n");
try std.testing.expectEqualSlices(u32, &.{b}, try panesNamed(&s, "/z.txt"));
try s.expect(s.at("/pane/{d}/dirty", .{b}), "1\n");
try s.expect(s.at("/pane/{d}/body", .{b}), "zero\none\ntwo\nthree\n");
// Alone now, it is no twin; Del asks about it as about any pane.
try std.testing.expect(try s.refused(s.at("/pane/{d}/ctl", .{b}), "Del\n") != 0);
try std.testing.expectEqualSlices(u32, &.{b}, try panesNamed(&s, "/z.txt"));
}
test "panes: a twin renamed logs a rename for each twin" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const a = try s.focused();
try s.write(s.at("/pane/{d}/exec", .{a}), "Zerox\n");
const twins = try panesNamed(&s, "/test.txt");
try std.testing.expectEqual(@as(usize, 2), twins.len);
try s.write(s.at("/pane/{d}/name", .{a}), "/tmp/pardes-twin-renamed.txt\n");
for (twins) |n| try std.testing.expect(has(try s.read("/log"), s.at("rename {d} /tmp/pardes-twin-renamed.txt\n", .{n})));
}
test "panes: small refusals: one name an open, small scratches loaded over unasked, twins saved once, and errnos in words a mount reads" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "f\n" });
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "noread.txt", .data = "secret\n" });
try tmp.dir.createDirPath(std.testing.io, "sub");
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const dir = try tmpPath(&s, &tmp);
const n = try s.focused();
const name = s.at("/pane/{d}/name", .{n});
const exec = s.at("/pane/{d}/exec", .{n});
// One name an open: a second line, in the write or after, is refused.
try std.testing.expectEqual(@as(u16, 22), try s.refused(name, "/tmp/pardes-a\nb"));
const f = try s.hold(name, 1);
try std.testing.expectEqual(@as(u16, 0), try s.writeFid(f, "/tmp/pardes-one.txt\n"));
try std.testing.expectEqual(@as(u16, 22), try s.writeFid(f, "b"));
s.clunk(f);
try s.expect(name, "/tmp/pardes-one.txt\n");
// Errnos a mount reads from the words.
try s.write(name, s.at("{s}/f.txt\n", .{dir}));
try s.write(exec, "Get\n");
try std.testing.expectEqual(@as(u16, 21), try s.refused(exec, "Get sub\n")); // EISDIR
const noread = s.at("{s}/noread.txt\x00", .{dir});
_ = std.c.chmod(noread[0 .. noread.len - 1 :0], 0);
try std.testing.expectEqual(@as(u16, 13), try s.refused(exec, "Get noread.txt\n")); // EACCES
try std.testing.expectEqual(@as(u16, 20), try s.refused(exec, s.at("Incl {s}/f.txt\n", .{dir}))); // ENOTDIR
_ = try s.refused(s.at("/pane/{d}/look", .{n}), "/dev/zero\n");
try std.testing.expect(has(try s.read("/log"), "not a regular file"));
// Twins saved once by Putall.
try s.write(s.at("/pane/{d}/body", .{n}), "edited\n");
try s.write(exec, "Zerox\n");
const saved = s.saved;
try s.write("/ctl", "Putall\n");
try std.testing.expectEqual(saved + 1, s.saved);
// A scratch of 2 bytes is loaded over unasked.
try s.write(exec, "New\n");
const scratch = try s.focused();
try s.write(s.at("/pane/{d}/body", .{scratch}), "hi");
try s.write(s.at("/pane/{d}/exec", .{scratch}), s.at("Get {s}/f.txt\n", .{dir}));
try s.expect(s.at("/pane/{d}/body", .{scratch}), "f\n");
}
test "panes: Get of a file not there says so without asking about unsaved edits" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "f\n" });
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const dir = try tmpPath(&s, &tmp);
const n = try s.focused();
try s.write(s.at("/pane/{d}/body", .{n}), "unsaved\n");
_ = try s.refused(s.at("/pane/{d}/exec", .{n}), "Get /tmp/pardes-surely-no-such-file.txt\n");
const log = try s.read("/log");
try std.testing.expect(has(log, "no such file"));
try std.testing.expect(!has(log, "again to discard"));
// Not asked, so the next Get of a file there is asked about, not run.
try std.testing.expect(try s.refused(s.at("/pane/{d}/exec", .{n}), s.at("Get {s}/f.txt\n", .{dir})) != 0);
try s.expect(s.at("/pane/{d}/body", .{n}), "x\nunsaved\n");
}
test "panes: a pane's ctl takes acme's words: done by the files that replace them, or refused with why" {
var s = try Session.initFile(std.testing.allocator, "one two\n");
defer s.deinit();
const n = try s.focused();
const ctl = s.at("/pane/{d}/ctl", .{n});
const dirty = s.at("/pane/{d}/dirty", .{n});
try s.write(ctl, "name /tmp/pardes-acme-ctl.txt\n");
try s.expect(s.at("/pane/{d}/name", .{n}), "/tmp/pardes-acme-ctl.txt\n");
try s.write(ctl, "dirty\n");
try s.expect(dirty, "1\n");
try s.write(ctl, "clean\n");
try s.expect(dirty, "0\n");
try s.write(s.at("/pane/{d}/addr", .{n}), "/two/");
try s.write(ctl, "dot=addr\nshow\nnomark\nmark\nlimit=addr\n");
try std.testing.expectEqual(@as(u32, 4), (try s.range(s.at("/pane/{d}/dot", .{n})))[0]);
try std.testing.expectEqual(.{ @as(u32, 4), @as(u32, 7) }, try s.range(s.at("/pane/{d}/limit", .{n})));
try s.write(ctl, "cleartag\n");
for ([_][]const u8{ "menu\n", "nomenu\n", "dump echo hi\n", "dumpdir /tmp\n", "font Go Mono\n" }) |line| {
try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, line));
try std.testing.expect(std.mem.startsWith(u8, s.said, "invalid: acme's "));
}
// Checked whole: a refused word anywhere and nothing ran.
try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "dirty\nmenu\n"));
try s.expect(dirty, "0\n");
}
test "panes: +Unsaved drops a pane once it has closed" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const a = try s.newPane();
try s.write(s.at("/pane/{d}/name", .{a}), "/tmp/pardes-unsaved-a.txt\n");
try s.write(s.at("/pane/{d}/body", .{a}), "edited\n");
const b = try s.newPane();
try s.write(s.at("/pane/{d}/name", .{b}), "/tmp/pardes-unsaved-b.txt\n");
try s.write(s.at("/pane/{d}/body", .{b}), "edited\n");
try std.testing.expect(try s.refused("/ctl", "Exit\n") != 0);
const list = s.at("/pane/{d}/body", .{(try panesNamed(&s, "/+Unsaved"))[0]});
try std.testing.expect(has(try s.read(list), "/tmp/pardes-unsaved-a.txt: Modified"));
// a closes (discarded): its row goes.
_ = try s.refused(s.at("/pane/{d}/ctl", .{a}), "Del\n");
try s.write(s.at("/pane/{d}/ctl", .{a}), "Del\n");
try std.testing.expectEqual(@as(usize, 0), (try panesNamed(&s, "/pardes-unsaved-a.txt")).len);
try std.testing.expect(!has(try s.read(list), "/tmp/pardes-unsaved-a.txt: Modified"));
}
test "panes: name refuses a directory, EISDIR, and leaves the name as it was" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const name = s.at("/pane/{d}/name", .{try s.focused()});
for ([_][]const u8{ "/\n", "/tmp/\n", "sub/\n", "/tmp\n" }) |dir| {
try std.testing.expectEqual(@as(u16, 21), try s.refused(name, dir));
try std.testing.expect(std.mem.indexOf(u8, s.said, "is a directory") != null);
}
try s.expect(name, "/test.txt\n");
}
test "panes: a write of two lines to name is refused EINVAL, on a held open or not" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const name = s.at("/pane/{d}/name", .{try s.focused()});
try std.testing.expectEqual(@as(u16, 22), try s.refused(name, "/tmp/a\n/tmp/b\n"));
const h = try s.hold(name, 1);
try std.testing.expectEqual(@as(u16, 22), try s.writeFid(h, "/tmp/a\n/tmp/b\n"));
s.clunk(h);
try s.expect(name, "/test.txt\n");
// Line by line, as bash writes it: the second line is refused.
const h2 = try s.hold(name, 1);
try std.testing.expectEqual(@as(u16, 0), try s.writeFid(h2, "/tmp/pardes-a\n"));
try std.testing.expectEqual(@as(u16, 22), try s.writeFid(h2, "/tmp/pardes-b\n"));
s.clunk(h2);
try s.expect(name, "/tmp/pardes-a\n");
}
test "panes: a name with a component over 255 bytes is refused by the write that holds it" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const name = s.at("/pane/{d}/name", .{try s.focused()});
try std.testing.expectEqual(@as(u16, 22), try s.refused(name, "/tmp/" ++ &@as([256]u8, @splat('c')) ++ "/f.txt\n"));
try std.testing.expectEqualStrings("invalid file name: a component over 255 bytes", s.said);
const longest = "/tmp/" ++ &@as([255]u8, @splat('c'));
try s.write(name, longest ++ "\n");
// Held with no newline, it is refused by that write, and the close
// has nothing left to apply.
const held = try s.hold(name, 1);
try std.testing.expectEqual(@as(u16, 22), try s.writeFid(held, "/tmp/" ++ &@as([256]u8, @splat('e'))));
s.clunk(held);
try s.expect(name, longest ++ "\n");
}
test "panes: a name cut across writes is one name, applied once at its newline or its close" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const name = s.at("/pane/{d}/name", .{try s.focused()});
const h = try s.hold(name, 1);
_ = try s.writeFid(h, "/tmp/pardes-na");
try s.expect(name, "/test.txt\n");
_ = try s.writeFid(h, "me.txt\n");
try s.expect(name, "/tmp/pardes-name.txt\n");
s.clunk(h);
// No newline: held, and applied at the close.
const h3 = try s.hold(name, 1);
_ = try s.writeFid(h3, "/tmp/pardes-closed.txt");
try s.expect(name, "/tmp/pardes-name.txt\n");
s.clunk(h3);
try s.expect(name, "/tmp/pardes-closed.txt\n");
}
test "panes: name reads the file name, and a write promotes a scratch without touching its body or its file" {
const gpa = std.testing.allocator;
var s = try Session.initFile(gpa, "opener\n");
defer s.deinit();
const n = try s.newPane();
const name = s.at("/pane/{d}/name", .{n});
const body = s.at("/pane/{d}/body", .{n});
const scratch = try s.read(name);
try std.testing.expect(std.mem.endsWith(u8, scratch, "/+New\n"));
try std.testing.expectEqual(@as(u64, scratch.len), (try s.stat(name)).size);
try s.write(body, "scratch ");
try s.write(body, "work\n");
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "renamed.zig", .data = "existing target\n" });
var dir_buf: [4096]u8 = undefined;
const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)];
const path = s.at("{s}/renamed.zig", .{dir});
try s.write(name, s.at("{s}\n", .{path}));
try s.expect(body, "scratch work\n");
try s.expect(s.at("/pane/{d}/dirty", .{n}), "1\n");
try s.expect(name, s.at("{s}\n", .{path}));
var target_buf: [64]u8 = undefined;
try std.testing.expectEqualStrings("existing target\n", try tmp.dir.readFile(std.testing.io, "renamed.zig", &target_buf));
try std.testing.expectEqual(@as(u16, 22), try s.refused(name, "\n"));
// One name, and what no file name holds: a second line, a NUL.
for ([_][2][]const u8{
.{ "two\nlines\n", "invalid file name: one name a write, on one line" },
.{ "nul\x00.zig\n", "bad character in file name: a NUL" },
}) |c| {
try std.testing.expectEqual(@as(u16, 22), try s.refused(name, c[0]));
try std.testing.expectEqualStrings(c[1], s.said);
}
// Any other byte a file name holds is taken, and reads back as written:
// blanks at its ends, a tab, a control byte, DEL, C1, bytes not UTF-8.
for ([_][]const u8{ " lead.zig ", "tab\t.zig", "c\x01.zig", "del\x7f.zig", "c1\xc2\x85.zig", "bad\xff.zig" }) |odd| {
try s.write(name, s.at("{s}\n", .{odd}));
const read_back = std.mem.trimEnd(u8, try s.read(name), "\n");
try std.testing.expect(std.mem.endsWith(u8, read_back, odd));
try s.write(name, read_back);
try s.expect(name, s.at("{s}\n", .{read_back}));
}
// A name that is one line and UTF-8 is taken, é and all.
try s.write(name, "caf\xc3\xa9.zig\n");
try std.testing.expect(std.mem.endsWith(u8, try s.read(name), "/caf\xc3\xa9.zig\n"));
try s.write(name, "two words.zig\n");
try s.expect(name, s.at("{s}/two words.zig\n", .{dir}));
// A rename is no edit: Undo takes back the last write.
try s.write(s.at("/pane/{d}/ctl", .{n}), "Undo\n");
try s.expect(body, "scratch ");
}
test "panes: relative names go from the file's directory and can name a new target" {
var s = try Session.initFile(std.testing.allocator, "retained body\n");
defer s.deinit();
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "note.txt", .data = "from pane directory\n" });
var dir_buf: [4096]u8 = undefined;
const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)];
const n = try s.focused();
const name = s.at("/pane/{d}/name", .{n});
try s.write(name, s.at("{s}/old.txt\n", .{dir}));
try s.write(name, "child/../note.txt\n");
try s.write(s.at("/pane/{d}/ctl", .{n}), "get\n");
try s.expect(s.at("/pane/{d}/body", .{n}), "from pane directory\n");
try s.write(name, "./created.txt\n");
try s.expect(name, s.at("{s}/created.txt\n", .{dir}));
// A name alone is no edit.
try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n");
}
test "panes: relative names follow a pane/new scratch's session directory, and a virtual one" {
var s = try Session.initFile(std.testing.allocator, "source body\n");
defer s.deinit();
const source = try s.focused();
try s.write(s.at("/pane/{d}/name", .{source}), "/elsewhere/source.zig\n");
// pane/new's scratch is the session's, not the active pane's.
const n = try s.newPane();
const name = s.at("/pane/{d}/name", .{n});
const session = std.fs.path.dirname(std.mem.trimEnd(u8, try s.read(name), "\n")).?;
try std.testing.expect(!std.mem.eql(u8, session, "/elsewhere"));
const up = std.fs.path.dirname(session) orelse "";
try s.write(name, "../out/./notes.txt\n");
try s.expect(name, s.at("{s}/out/notes.txt\n", .{up}));
try s.write(name, "again.txt\n");
try s.expect(name, s.at("{s}/out/again.txt\n", .{up}));
try s.write(name, s.at("/virtual/pane/{d}/./body\n", .{source}));
try s.write(s.at("/pane/{d}/ctl", .{n}), "get\n");
try s.expect(s.at("/pane/{d}/body", .{n}), "source body\n");
try s.write(name, "./body\n");
try s.expect(name, s.at("/virtual/pane/{d}/body\n", .{source}));
try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n");
}
test "panes: errors appends to one +Errors pane for the pane's directory" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const n = try s.focused();
try std.testing.expect(std.mem.indexOf(u8, try s.read("/index"), "+Errors") == null);
try s.write(s.at("/pane/{d}/errors", .{n}), "boom\n");
try s.write(s.at("/pane/{d}/errors", .{n}), "again\n");
const index = try s.read("/index");
try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, index, " /+Errors "));
const at = std.mem.indexOf(u8, index, " /+Errors ").?;
const line = index[(if (std.mem.lastIndexOfScalar(u8, index[0..at], '\n')) |i| i + 1 else 0)..];
const serial = line[0..std.mem.indexOfScalar(u8, line, ' ').?];
try s.expect(s.at("/pane/{s}/body", .{serial}), "boom\nagain\n");
}
test "panes: truncating body empties it, and a stat says the length after" {
var s = try Session.initFile(std.testing.allocator, "one\ntwo\n");
defer s.deinit();
const body = s.at("/pane/{d}/body", .{try s.focused()});
const h = try s.hold(body, 1);
try s.truncate(h);
try std.testing.expectEqual(@as(u64, 0), (try s.stat(body)).size);
try s.expect(body, "");
s.clunk(h);
try s.write(body, "new text\n");
try std.testing.expectEqual(@as(u64, 9), (try s.stat(body)).size);
}
test "panes: a rename colours a source its new name's language names, its text unchanged" {
if (!@import("pardes_config").syntax_highlighting) return error.SkipZigTest;
var s = try Session.initFile(std.testing.allocator, "fn check() void {}\n");
defer s.deinit();
const n = try s.focused();
const x, const y = try s.where("fn check");
const screen = try s.screen();
const cols = try std.unicode.utf8CountCodepoints(screen[0..std.mem.indexOfScalar(u8, screen, '\n').?]);
const plain = try s.styles();
try std.testing.expectEqualStrings(plain[y * cols + x], plain[y * cols + x + 3]);
try s.write(s.at("/pane/{d}/name", .{n}), "renamed.zig\n");
const coloured = try s.styles();
try std.testing.expectEqualStrings(coloured[y * cols + x], coloured[y * cols + x + 1]);
try std.testing.expect(!std.mem.eql(u8, coloured[y * cols + x], plain[y * cols + x]));
try s.expect(s.at("/pane/{d}/body", .{n}), "fn check() void {}\n");
}
|