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
|
//! docs/typ/reference.typ, look and exec <look-and-exec>.
const std = @import("std");
const Session = @import("session").Session;
const expectEqual = std.testing.expectEqual;
test "look-and-exec: a 9P look keeps its leading blanks, a blank-only look included, and exec still trims" {
var s = try Session.initFile(std.testing.allocator, "a indented\nb\n indented\n \n");
defer s.deinit();
const n = try s.focused();
const dot = s.at("/pane/{d}/dot", .{n});
try s.write(dot, "0 0");
// The indented line, not the `indented` after `a `.
try s.write(s.at("/pane/{d}/look", .{n}), " indented\n");
try expectEqual(.{ @as(u32, 13), @as(u32, 25) }, try s.range(dot));
// Blanks alone are text to find too.
try s.write(dot, "26 26");
try s.write(s.at("/pane/{d}/look", .{n}), " \n");
try expectEqual(.{ @as(u32, 26), @as(u32, 30) }, try s.range(dot));
// exec's line is still trimmed: ` Msg hi` runs Msg.
try s.write(s.at("/pane/{d}/exec", .{n}), " Msg blanks trimmed\n");
try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "blanks trimmed") != null);
}
test "look-and-exec: a bare :N or :N:M look addresses the pane itself, from its look, the root's and event write-back" {
var s = try Session.initFile(std.testing.allocator, "alpha\nbeta\ngamma\n");
defer s.deinit();
const n = try s.focused();
const dot = s.at("/pane/{d}/dot", .{n});
try s.write(s.at("/pane/{d}/look", .{n}), ":2\n");
try expectEqual(.{ @as(u32, 6), @as(u32, 11) }, try s.range(dot));
try s.write("/look", ":3\n");
try expectEqual(.{ @as(u32, 11), @as(u32, 17) }, try s.range(dot));
try s.write(s.at("/pane/{d}/look", .{n}), ":3:2\n");
try expectEqual(@as(u32, 12), (try s.range(dot))[0]);
// Written back by an event reader, the same.
try s.write(s.at("/pane/{d}/event", .{n}), "ML0 0 0 2 :1\n");
try expectEqual(.{ @as(u32, 0), @as(u32, 6) }, try s.range(dot));
}
fn has(hay: []const u8, needle: []const u8) bool {
return std.mem.indexOf(u8, hay, needle) != null;
}
fn lines(text: []const u8) usize {
return std.mem.count(u8, text, "\n");
}
/// Whether /index lists the pane `serial`.
fn listed(s: *Session, serial: u32) !bool {
var it = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n');
while (it.next()) |line| {
var words = std.mem.tokenizeScalar(u8, line, ' ');
if (std.mem.eql(u8, words.next() orelse "", s.at("{d}", .{serial}))) return true;
}
return false;
}
test "look-and-exec: a look takes acme's :addr and file:addr, and a miss changes nothing" {
var s = try Session.initFile(std.testing.allocator, "foo x foo y\nbar\n");
defer s.deinit();
const n = try s.focused();
const look = s.at("/pane/{d}/look", .{n});
const dot = s.at("/pane/{d}/dot", .{n});
const path = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n");
// :addr is an address in this pane, evaluated from its dot.
try s.write(look, ":/y/\n");
try expectEqual(.{ @as(u32, 10), @as(u32, 11) }, try s.range(dot));
try s.write(look, ":#4\n");
try expectEqual(.{ @as(u32, 4), @as(u32, 4) }, try s.range(dot));
try s.write(look, ":/foo/\n"); // from #4: the second foo
try expectEqual(.{ @as(u32, 6), @as(u32, 9) }, try s.range(dot));
// file:addr names the pane's own file here.
try s.write(look, s.at("{s}:/bar/\n", .{path}));
try expectEqual(.{ @as(u32, 12), @as(u32, 15) }, try s.range(dot));
// A miss: no pane made, the selection kept, and it says so.
const panes = lines(try s.read("/index"));
try s.write(look, "zzqq\n");
try expectEqual(panes, lines(try s.read("/index")));
try expectEqual(.{ @as(u32, 12), @as(u32, 15) }, try s.range(dot));
try std.testing.expect(has(try s.read("/log"), "no match for \"zzqq\""));
// A hit answers the pane the text is selected in, not a hits pane.
try s.write(look, "foo\n");
try s.expect(look, s.at("{d}\n", .{n}));
// A pattern with a blank in it; 0/re/ searches from the top, whatever dot is.
try s.write(look, ":/foo y/\n");
try expectEqual(.{ @as(u32, 6), @as(u32, 11) }, try s.range(dot));
try s.write(look, s.at("{s}:0/foo/\n", .{path}));
try expectEqual(.{ @as(u32, 0), @as(u32, 3) }, try s.range(dot));
// file:N selects the line; a line past the end moves nothing and reads back empty.
try s.write(look, s.at("{s}:2\n", .{path}));
try expectEqual(.{ @as(u32, 12), @as(u32, 16) }, try s.range(dot));
try s.write(look, s.at("{s}:99\n", .{path}));
try expectEqual(.{ @as(u32, 12), @as(u32, 16) }, try s.range(dot));
try s.expect(look, "");
try std.testing.expect(has(try s.read("/log"), s.at("look: {s}:99 has no line 99\n", .{path})));
}
test "look-and-exec: the root look's file:/re/ goes on from the dot, and a miss leaves the dot as it was" {
var s = try Session.initFile(std.testing.allocator, "foo x foo y\nfoo\n");
defer s.deinit();
const n = try s.focused();
const dot = s.at("/pane/{d}/dot", .{n});
const path = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n");
for ([_]u32{ 0, 6, 12, 0 }) |at| {
try s.write("/look", s.at("{s}:/foo/\n", .{path}));
try expectEqual(.{ at, at + 3 }, try s.range(dot));
}
try s.write("/look", s.at("{s}:/zzqq/\n", .{path}));
try expectEqual(.{ @as(u32, 0), @as(u32, 3) }, try s.range(dot));
}
test "look-and-exec: exec runs a builtin at the pane, and look and exec read back the pane it acted on" {
var s = try Session.initFile(std.testing.allocator, "Msg fs-ran\n");
defer s.deinit();
const n = try s.focused();
const own = s.at("{d}\n", .{n});
try s.write(s.at("/pane/{d}/exec", .{n}), "Msg from exec\n");
try std.testing.expect(has(try s.read("/log"), s.at("msg {d} from exec\n", .{n})));
try s.expect("/exec", own);
try std.testing.expectEqual(@as(u64, own.len), (try s.stat("/exec")).size);
try s.expect(s.at("/pane/{d}/look", .{n}), own);
try s.write(s.at("/pane/{d}/exec", .{n}), "New\n");
const made = try s.focused();
try std.testing.expect(made != n);
try s.expect("/exec", s.at("{d}\n", .{made}));
try s.write(s.at("/pane/{d}/exec", .{made}), "Del\n");
try std.testing.expect(try listed(&s, n) and !try listed(&s, made));
try s.expect("/exec", s.at("{d}\n", .{made}));
}
test "look-and-exec: look and exec read back what the last command touched, a ctl write's too" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const n = try s.focused();
try s.write("/exec", "Msg first\n");
try s.expect("/exec", s.at("{d}\n", .{n}));
// A pane made through ctl is what both read next, not the Msg's pane.
try s.write(s.at("/pane/{d}/ctl", .{n}), "Term\n");
const made = try s.focused();
try std.testing.expect(made != n);
try s.expect("/exec", s.at("{d}\n", .{made}));
try s.expect("/look", s.at("{d}\n", .{made}));
// A ctl write that makes nothing names the pane it ran at.
try s.write("/ctl", "Verbose off\n");
try s.expect("/exec", s.at("{d}\n", .{made}));
// Closed, it is no pane to go to: a fresh read of look is empty.
try s.write(s.at("/pane/{d}/ctl", .{made}), "Del\n");
try std.testing.expect(!try listed(&s, made));
try s.expect("/look", "");
}
test "look-and-exec: a line starting with # runs as nothing at an exec or a ctl, silently" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const n = try s.focused();
// The setup's own announcements are made by the first write.
try s.write("/ctl", "# settle\n");
const index = try s.read("/index");
const log = try s.read("/log");
const col = std.mem.sliceTo(try s.read("/layout"), ' ');
for ([_][]const u8{ "/exec", s.at("/pane/{d}/exec", .{n}), "/ctl", s.at("/pane/{d}/ctl", .{n}), s.at("/col/{s}/ctl", .{col}) }) |path|
try s.write(path, "# Platform: tui\n # indented too\n");
try s.expect("/index", index);
try s.expect("/log", log);
}
test "look-and-exec: a look replaces the selection it was made from, for an address, a range and text found" {
var s = try Session.initFile(std.testing.allocator, "alpha\nbravo\ncharlie\nneedle\n");
defer s.deinit();
const n = try s.focused();
const dot = s.at("/pane/{d}/dot", .{n});
const look = s.at("/pane/{d}/look", .{n});
const ctl = s.at("/pane/{d}/ctl", .{n});
const body = s.at("/pane/{d}/body", .{n});
try s.write(dot, "0 5");
// A second cursor on the line below, and select mode.
try s.keys("Cv");
try std.testing.expect(std.mem.endsWith(u8, try s.read(ctl), " mode:select "));
try s.write(look, "/test.txt:3:2\n");
try expectEqual(.{ @as(u32, 13), @as(u32, 13) }, try s.range(dot));
// Both are gone with it: normal mode, and what is typed goes in once.
try std.testing.expect(std.mem.endsWith(u8, try s.read(ctl), " mode:normal "));
try s.keys("iZ\x1b");
try s.expect(body, "alpha\nbravo\ncZharlie\nneedle\n");
try s.write(ctl, "Undo\n");
try s.expect(body, "alpha\nbravo\ncharlie\nneedle\n");
try s.write(dot, "0 5");
try s.write(look, "/test.txt:2:2-4\n");
try s.expect(s.at("/pane/{d}/sel", .{n}), "rav");
try s.write(s.at("/pane/{d}/name", .{n}), "/tmp/look-selection-source.txt");
try s.write(dot, "0 5");
// A count typed before it is gone after it.
try s.keys("5");
try s.write(look, "needle\n");
try s.expect(s.at("/pane/{d}/sel", .{n}), "needle");
try s.keys("l");
try expectEqual(.{ @as(u32, 26), @as(u32, 26) }, try s.range(dot));
try expectEqual(n, try s.focused());
}
test "look-and-exec: a look into another pane leaves the source's dot and replaces the destination's selection" {
var s = try Session.initFile(std.testing.allocator, "source\nlines\n");
defer s.deinit();
const source = try s.focused();
const dest = try s.newPane();
try s.write(s.at("/pane/{d}/name", .{dest}), "/look-destination.txt");
try s.write(s.at("/pane/{d}/body", .{dest}), "alpha\nbravo\ncharlie\n");
const dest_dot = s.at("/pane/{d}/dot", .{dest});
for ([_]bool{ false, true }) |range| {
try s.write("/focus", s.at("{d}", .{source}));
try s.write(s.at("/pane/{d}/dot", .{source}), "0 6");
try s.write(dest_dot, "0 5");
try s.write(s.at("/pane/{d}/look", .{source}), if (range) "/look-destination.txt:2:2-4\n" else "/look-destination.txt:3:2\n");
try expectEqual(dest, try s.focused());
// The source keeps its dot, as acme's look leaves it.
try s.expect(s.at("/pane/{d}/sel", .{source}), "source");
if (range)
try s.expect(s.at("/pane/{d}/sel", .{dest}), "rav")
else
try expectEqual(.{ @as(u32, 13), @as(u32, 13) }, try s.range(dest_dot));
}
}
test "look-and-exec: a look that finds nothing says so" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const n = try s.newPane();
try s.write(s.at("/pane/{d}/look", .{n}), "/no/such/zzqq\n");
try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), s.at("err {d} look: no match for \"/no/such/zzqq\"\n", .{n})) != null);
}
/// The log's `err` records, counted.
fn errs(s: *Session) !usize {
return std.mem.count(u8, try s.read("/log"), "\nerr ");
}
/// The serial of the pane /index names with `suffix`.
fn paneNamed(s: *Session, suffix: []const u8) !u32 {
var rows = std.mem.splitScalar(u8, try s.read("/index"), '\n');
while (rows.next()) |line| {
// serial kind dirty name column
const name = line[0 .. std.mem.lastIndexOfScalar(u8, line, ' ') orelse continue];
if (std.mem.endsWith(u8, name, suffix)) return std.fmt.parseInt(u32, line[0..std.mem.indexOfScalar(u8, line, ' ').?], 10);
}
return error.NoSuchPane;
}
test "look-and-exec: `:N` on a PDF turns to page N, from the pane and from the root's look, and `@pN:N` logs no err" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const n = try s.lookFile("docs/design.pdf");
const look = s.at("/pane/{d}/look", .{n});
const pages = (try s.pdfPage(n))[1];
try std.testing.expect(pages >= 3);
try s.write(look, ":2\n");
try expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]);
try s.write(look, s.at("@p{d}:1\n", .{n}));
try expectEqual(@as(u32, 1), (try s.pdfPage(n))[0]);
try s.write("/look", s.at("@p{d}:2\n", .{n}));
try expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]);
try expectEqual(@as(usize, 0), try errs(&s));
// `:N:H` is hit H of its search on page N, as a search row's
// `file.pdf:N:H` is: with no search there is no hit 3, a miss that turns
// no page.
try s.write(look, ":1\n");
_ = try s.refused(look, ":2:3\n");
try expectEqual(@as(u32, 1), (try s.pdfPage(n))[0]);
try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "has no search hit 3 on page 2") != null);
try s.write(look, ":2\n");
_ = try s.refused(look, ":0\n");
try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "has no page 0") != null);
// A byte offset or a pattern is no PDF address: refused with words.
_ = try s.refused(look, ":#3\n");
try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "its address is a page, :N") != null);
const before = try errs(&s);
_ = try s.refused(look, s.at(":{d}\n", .{pages + 1}));
try expectEqual(before + 1, try errs(&s));
try expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]);
}
test "look-and-exec: a look at a PDF's page past its last is a miss, never the last page, and opens nothing" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
var buf: [4096]u8 = undefined;
const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
try std.Io.Dir.cwd().copyFile("docs/design.pdf", tmp.dir, "other.pdf", std.testing.io, .{});
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const n = try s.lookFile("docs/design.pdf");
const look = s.at("/pane/{d}/look", .{n});
const name = std.mem.trimEnd(u8, try s.read(s.at("/pane/{d}/name", .{n})), "\n");
// Open: it stays on its page.
try s.write(look, ":2\n");
_ = try s.refused(look, s.at("{s}:999\n", .{name}));
try expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]);
try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "has no page 999") != null);
// Not open: nothing opens.
const index = try s.read("/index");
_ = try s.refused(look, s.at("{s}/other.pdf:999\n", .{dir}));
try s.expect("/index", index);
try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "other.pdf has no page 999") != null);
}
test "look-and-exec: a +PdfSections row's first number is the page: a section not there, or on another page, is a miss said" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const n = try s.lookFile("docs/design.pdf");
try s.write(s.at("/pane/{d}/exec", .{n}), "PdfSections\n");
const out = try paneNamed(&s, "/+PdfSections");
const body = try s.read(s.at("/pane/{d}/body", .{out}));
const row = body[0..std.mem.indexOfScalar(u8, body, '\n').?];
const place = row[0..std.mem.indexOfScalar(u8, row, ' ').?]; // design.pdf:P:S
var fields = std.mem.splitScalar(u8, place, ':');
_ = fields.next();
const page = try std.fmt.parseInt(u32, fields.next().?, 10);
const look = s.at("/pane/{d}/look", .{out});
// As listed: found, no miss.
try s.write(look, s.at("{s}\n", .{place}));
try expectEqual(@as(usize, 0), try errs(&s));
// The section on another page than the row says, and one not there.
_ = try s.refused(look, s.at("design.pdf:{d}:1\n", .{page + 1}));
try expectEqual(@as(usize, 1), try errs(&s));
_ = try s.refused(look, "design.pdf:1:999\n");
try expectEqual(@as(usize, 2), try errs(&s));
}
test "look-and-exec: a look's miss repeats the path whole up to 256 bytes, then gives up its middle to an ellipsis" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const long = "some/deeply/nested/directory/that/goes/on/and/on/with/a_rather_long_file_name.txt";
try s.write("/look", long ++ "\n");
try std.testing.expect(has(try s.read("/log"), "look: no match for \"" ++ long ++ "\"\n"));
const huge = std.mem.asBytes(&@as([150][2]u8, @splat("é".*))); // 300 bytes: its middle cut to fit 256, between characters
const kept = std.mem.asBytes(&@as([63][2]u8, @splat("é".*)));
try s.write("/look", huge ++ "\n");
try std.testing.expect(has(try s.read("/log"), "look: no match for \"" ++ kept ++ "…" ++ kept ++ "\"\n"));
}
test "look-and-exec: @p<serial>:<address> takes any sam address; a miss and an unknown serial are errs" {
var s = try Session.initFile(std.testing.allocator, "foo x foo y\nbar\n");
defer s.deinit();
const n = try s.focused();
const dot = s.at("/pane/{d}/dot", .{n});
const Case = struct { []const u8, u32, u32 };
for ([_]Case{
.{ "0/foo/", 0, 3 },
.{ "/foo/", 6, 9 },
.{ "#12,#15", 12, 15 },
.{ "$", 16, 16 },
.{ "1,2", 0, 16 },
// Backward, as in addr: from the dot's start, wrapping to the last.
.{ "?foo?", 6, 9 },
.{ "$-?y?", 10, 11 },
}) |c| {
try s.write("/look", s.at("@p{d}:{s}\n", .{ n, c[0] }));
try expectEqual(.{ c[1], c[2] }, try s.range(dot));
}
try s.write("/look", s.at("@p{d}:/zzqq/\n", .{n}));
try std.testing.expect(has(try s.read("/log"), ": no match for regexp"));
try s.expect("/look", "");
try s.write("/look", "@p999:/foo/\n");
try std.testing.expect(has(try s.read("/log"), "look: no pane @p999"));
try s.expect("/look", "");
// A line address to no pane is a miss too, not the active pane.
try s.write("/look", "@p77:1\n");
try std.testing.expect(has(try s.read("/log"), "look: no pane @p77"));
try s.expect("/look", "");
}
test "look-and-exec: a look at file:3:0 is refused as addr 3:0 is, a column counting from 1" {
var s = try Session.initFile(std.testing.allocator, "one\ntwo\nthree\n");
defer s.deinit();
const n = try s.focused();
try expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/addr", .{n}), "3:0"));
const by_addr = s.said;
try expectEqual(@as(u16, 22), try s.refused(s.at("/pane/{d}/look", .{n}), "/test.txt:3:0\n"));
try std.testing.expectEqualStrings(by_addr, s.said);
try s.write(s.at("/pane/{d}/look", .{n}), "/test.txt:3:1\n");
}
test "look-and-exec: a look of ./x, x or /abs/x that is not there is a miss: answered, the err logged" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const look = s.at("/pane/{d}/look", .{try s.focused()});
for ([_][]const u8{ "./zzq-nosuch.txt\n", "zzq-nosuch.txt\n", "/zzq-nosuch.txt\n" }) |line| try s.write(look, line);
try std.testing.expect(has(try s.read("/log"), "look: ./zzq-nosuch.txt: no such file"));
}
test "look-and-exec: a look at a ./ or ../ name that is not there is a miss naming the file; a plain one is looked for as text" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.createDirPath(std.testing.io, "many");
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.focused();
try s.write(s.at("/pane/{d}/name", .{n}), s.at("{s}/many/a.txt\n", .{dir}));
for ([_][]const u8{ "./zz.txt", "../zz.txt" }) |name| {
try s.write(s.at("/pane/{d}/look", .{n}), s.at("{s}\n", .{name}));
try std.testing.expect(has(try s.read("/log"), s.at("look: {s}: no such file", .{name})));
}
try s.write(s.at("/pane/{d}/look", .{n}), "zz.txt\n");
try std.testing.expect(has(try s.read("/log"), "look: no match for \"zz.txt\""));
}
test "look-and-exec: a look at file:addr whose file is not there is one miss, quoting what was written, in a terminal too" {
var s = try Session.initTerm(std.testing.allocator);
defer s.deinit();
const look = s.at("/pane/{d}/look", .{try s.focused()});
const before = try errs(&s);
for ([_][]const u8{ "zzq:#3", "zzq:2", "zzq:/re/" }) |written| {
try s.write(look, s.at("{s}\n", .{written}));
try expectEqual(@as(usize, 1), std.mem.count(u8, try s.read("/log"), s.at("look: no match for \"{s}\"\n", .{written})));
}
try expectEqual(before + 3, try errs(&s));
}
test "look-and-exec: a builtin that needs its argument, written bare to an exec, is EINVAL as through ctl" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
for ([_][]const u8{ "/exec", s.at("/pane/{d}/exec", .{try s.focused()}), "/tagexec" }) |path| {
try expectEqual(@as(u16, 22), try s.refused(path, "Mount\n"));
try std.testing.expectEqualStrings("wrong #args in control message \"Mount\"", s.said);
}
}
test "look-and-exec: a builtin that takes no argument, written with one to an exec, is refused wrong #args, as at a ctl" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
for ([_][]const u8{ "/exec", s.at("/pane/{d}/exec", .{try s.focused()}) }) |path| for ([_][]const u8{ "Config extra\n", "DumpConfig extra\n" }) |line| {
try expectEqual(@as(u16, 22), try s.refused(path, line));
try std.testing.expect(std.mem.startsWith(u8, s.said, "wrong #args in control message"));
};
}
/// 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)]});
}
test "look-and-exec: a look opens spaced paths and locations without editing the source pane" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "two words.zig", .data = "first\nsecond\nthird\n" });
var s = try Session.initFile(std.testing.allocator, "source stays intact\n");
defer s.deinit();
const path = s.at("{s}/two words.zig", .{try tmpPath(&s, &tmp)});
const source = try s.focused();
const ctl = try s.read(s.at("/pane/{d}/ctl", .{source}));
try s.write(s.at("/pane/{d}/dot", .{source}), "3 3");
try s.write(s.at("/pane/{d}/look", .{source}), s.at("{s}:2:3\n", .{path}));
const opened = try s.focused();
try std.testing.expect(opened != source);
try s.expect(s.at("/pane/{d}/name", .{opened}), s.at("{s}\n", .{path}));
try s.expect(s.at("/pane/{d}/body", .{opened}), "first\nsecond\nthird\n");
try expectEqual(@as(u32, 8), (try s.range(s.at("/pane/{d}/dot", .{opened})))[0]); // line 2, column 3
// The source: its text, its undo steps and its cursor as they were.
try s.expect(s.at("/pane/{d}/body", .{source}), "source stays intact\n");
const after = try s.read(s.at("/pane/{d}/ctl", .{source}));
try std.testing.expectEqualStrings(ctl[0..60], after[0..60]);
var fields = std.mem.tokenizeScalar(u8, after, ' ');
for (0..8) |_| _ = fields.next();
try std.testing.expectEqualStrings("0", fields.next().?); // still no undo step
try expectEqual(.{ @as(u32, 3), @as(u32, 3) }, try s.range(s.at("/pane/{d}/dot", .{source})));
try s.expect("/look", s.at("{d}\n", .{opened}));
// From the root, with the source focused: the same pane, moved.
try s.write("/focus", s.at("{d}\n", .{source}));
try s.write("/look", s.at("{s}:3:2\n", .{path}));
try expectEqual(opened, try s.focused());
try expectEqual(@as(u32, 14), (try s.range(s.at("/pane/{d}/dot", .{opened})))[0]); // line 3, column 2
try s.expect("/look", s.at("{d}\n", .{opened}));
}
test "look-and-exec: a look validates the whole batch before opening virtual files" {
var s = try Session.initFile(std.testing.allocator, "source\n");
defer s.deinit();
const source = try s.focused();
const look = s.at("/pane/{d}/look", .{source});
const index = try s.read("/index");
for ([_][]const u8{ "/virtual/index\x00ignored", "/virtual/index\n/virtual/\x01index\n" }) |invalid| {
try expectEqual(@as(u16, 22), try s.refused(look, invalid));
try s.expect("/index", index);
try expectEqual(source, try s.focused());
try s.expect(s.at("/pane/{d}/body", .{source}), "source\n");
}
try s.write(look, "/n/self/index\n");
const opened = try s.focused();
try s.expect(s.at("/pane/{d}/name", .{opened}), "/virtual/index\n");
try std.testing.expect(has(try s.read(s.at("/pane/{d}/body", .{opened})), "/test.txt"));
try s.expect(s.at("/pane/{d}/body", .{source}), "source\n");
}
test "look-and-exec: status reads the editor, and look and exec validate whole batches" {
var s = try Session.initFile(std.testing.allocator, "Msg root\n");
defer s.deinit();
const status = try s.read("/status");
var rows = std.mem.tokenizeScalar(u8, status, '\n');
try std.testing.expect(std.mem.startsWith(u8, rows.next().?, "pid "));
try std.testing.expect(std.mem.startsWith(u8, rows.next().?, "version "));
try std.testing.expectEqualStrings(s.at("panes {d}", .{lines(try s.read("/index"))}), rows.next().?);
try std.testing.expect(rows.next() == null);
const st = try s.stat("/status");
try expectEqual(@as(u64, status.len), st.size);
try expectEqual(@as(u16, 0o444), st.mode & 0o777);
try expectEqual(@as(u16, 1), try s.refused("/status", "anything\n"));
try s.expect("/exec", "");
const index = try s.read("/index");
for ([_][]const u8{ "Msg a\x00b", "Msg x\nMsg \x01y\n" }) |invalid| {
try expectEqual(@as(u16, 22), try s.refused("/exec", invalid));
try s.expect("/index", index);
}
// An empty write is a no-op, not a command.
try s.write("/exec", "\n \n");
try s.expect("/exec", "");
try s.expect("/status", status);
const n = try s.focused();
try s.write("/exec", "Msg hello there\n");
try std.testing.expect(has(try s.read("/log"), s.at("msg {d} hello there\n", .{n})));
try s.expect("/exec", s.at("{d}\n", .{n}));
try s.write("/look", "/virtual/index\n");
const opened = try s.focused();
try s.expect(s.at("/pane/{d}/name", .{opened}), "/virtual/index\n");
try s.expect("/look", s.at("{d}\n", .{opened}));
try s.write("/exec", "New\n");
const made = try s.focused();
try std.testing.expect(made != opened);
try s.expect("/exec", s.at("{d}\n", .{made}));
}
test "look-and-exec: a look at a file it may not read fails the write naming the file" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "locked.txt", .data = "secret\n", .flags = .{ .permissions = .fromMode(0o000) } });
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const path = s.at("{s}/locked.txt", .{try tmpPath(&s, &tmp)});
if (try s.refused("/look", s.at("{s}\n", .{path})) == 0) return error.SkipZigTest; // root reads anything
try std.testing.expectEqualStrings(s.at("look: {s}: permission denied", .{path}), s.said);
}
test "look-and-exec: a look line's trailing colon is dropped, and a look whose address fails never leaves its file open" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "a.txt", .data = "one\ntwo\nthree\n" });
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const dir = try tmpPath(&s, &tmp);
const before = lines(try s.read("/index"));
// Failing addresses: nothing opened, the write still answered.
for ([_][]const u8{ s.at("{s}/a.txt:/nomatch/\n", .{dir}), s.at("{s}/a.txt:9:\n", .{dir}) }) |line| {
_ = try s.refused("/look", line);
try expectEqual(before, lines(try s.read("/index")));
}
// `a.txt:2:3:` is `a.txt:2:3`: it opens there.
try s.write("/look", s.at("{s}/a.txt:2:3:\n", .{dir}));
try expectEqual(before + 1, lines(try s.read("/index")));
const opened = try s.focused();
try s.expect(s.at("/pane/{d}/name", .{opened}), s.at("{s}/a.txt\n", .{dir}));
try expectEqual(@as(u32, 6), (try s.range(s.at("/pane/{d}/dot", .{opened})))[0]);
}
test "look-and-exec: a look that misses file:/re/ names the file, and is logged as its pane's" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "alpha\n" });
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const dir = try tmpPath(&s, &tmp);
const first = try s.focused();
try s.write("/look", s.at("{s}/f.txt\n", .{dir}));
const f = try s.focused();
_ = try s.refused(s.at("/pane/{d}/look", .{first}), s.at("{s}/f.txt:/zzqq/\n", .{dir}));
try std.testing.expect(has(try s.read("/log"), s.at("err {d} look: {s}/f.txt: no match for regexp", .{ f, dir })));
}
test "look-and-exec: a look that goes to another pane leaves the looking pane's dot alone" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "f one\nf two\n" });
var s = try Session.initFile(std.testing.allocator, "alpha bravo\ncharlie\n");
defer s.deinit();
const dir = try tmpPath(&s, &tmp);
const source = try s.focused();
const other = try s.newPane();
try s.write(s.at("/pane/{d}/body", .{other}), "one\ntwo\nthree\n");
for ([_][]const u8{ s.at("@p{d}:2\n", .{other}), s.at("@p{d}:/three/\n", .{other}), s.at("{s}/f.txt:2\n", .{dir}) }) |line| {
try s.write(s.at("/pane/{d}/dot", .{source}), "6 11");
try s.write(s.at("/pane/{d}/look", .{source}), line);
try std.testing.expect(try s.focused() != source);
try expectEqual(.{ @as(u32, 6), @as(u32, 11) }, try s.range(s.at("/pane/{d}/dot", .{source})));
}
}
test "look-and-exec: a looked word is found in its own pane, next place after the dot, wrapping, and opens no pane" {
var s = try Session.initFile(std.testing.allocator, "foo x foo y\n");
defer s.deinit();
const n = try s.focused();
const look = s.at("/pane/{d}/look", .{n});
const index = try s.read("/index");
for ([_]u32{ 0, 6, 0 }) |at| {
try s.write(look, "foo\n");
try expectEqual(.{ at, at + 3 }, try s.range(s.at("/pane/{d}/dot", .{n})));
}
try s.expect("/index", index);
}
test "look-and-exec: `@pN:` looks at pane N as `a.txt:` looks at a.txt, and a line past the end is said one way, `:N` or `file:N`" {
var s = try Session.initFile(std.testing.allocator, "one\ntwo\n");
defer s.deinit();
const n = try s.focused();
const look = s.at("/pane/{d}/look", .{n});
const before = try errs(&s);
try s.write(look, s.at("@p{d}:\n", .{n}));
try expectEqual(before, try errs(&s));
try s.write(look, ":99999\n");
try s.write(look, "/test.txt:99999\n");
try std.testing.expect(has(try s.read("/log"), "/test.txt:99999 has no line 99999 (x2)\n"));
}
test "look-and-exec: an exec line over the command limit fails the write and says the limit; a builtin's may be longer" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const n = try s.focused();
const long = "echo " ++ &@as([1024]u8, @splat('y')) ++ "\n";
try expectEqual(@as(u16, 22), try s.refused("/exec", long));
try std.testing.expectEqualStrings("invalid command line: a command line is at most 1024 bytes", s.said);
try std.testing.expect(has(try s.read("/log"), s.at("err {d} exec: invalid command line: a command line is at most 1024 bytes", .{n})));
_ = try s.refused("/exec", "echo a\x01b\n");
try std.testing.expectEqualStrings("invalid command line: it holds a control character (or DEL) other than a tab", s.said);
try std.testing.expect(has(try s.read("/log"), s.at("err {d} exec: invalid command line: it holds", .{n})));
try s.write("/exec", "Msg " ++ &@as([1100]u8, @splat('z')) ++ "\n");
}
test "look-and-exec: a builtin that fails through exec, tagexec or a column's exec fails the write with one err and no msg" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const n = try s.focused();
// Another failure first: the pane's `new` is logged after it.
_ = try s.refused("/ctl", "Unmount yyy\n");
for ([_][]const u8{ "/exec", s.at("/pane/{d}/exec", .{n}), "/tagexec", s.at("/col/{d}/exec", .{try s.column(0)}) }) |path| {
try std.testing.expect(try s.refused(path, "Unmount zzz\n") != 0);
try std.testing.expect(has(s.said, "Unmount: no such mount zzz"));
}
const log = try s.read("/log");
try std.testing.expect(!has(log, "msg "));
// The root's exec ran at the pane, so its err and the pane's exec's are
// the same record, counted.
try expectEqual(@as(usize, 3), std.mem.count(u8, log, "Unmount: no such mount zzz"));
try std.testing.expect(has(log, "mount zzz (x2)"));
// A control character is refused with its reason.
try expectEqual(@as(u16, 22), try s.refused("/exec", "Msg a\x01b\n"));
try std.testing.expectEqualStrings("invalid command line: it holds a control character (or DEL) other than a tab", s.said);
// A look miss: one err, no msg; the same miss again is that record counted.
try s.write("/look", "zzqqnotthere\n");
try std.testing.expect(has(try s.read("/log"), "look: no match for \"zzqqnotthere\""));
try std.testing.expect(!has(try s.read("/log"), "msg "));
try s.write("/look", "zzqqnotthere\n");
try std.testing.expect(has(try s.read("/log"), "look: no match for \"zzqqnotthere\" (x2)\n"));
}
test "look-and-exec: a served-tree pane spoils no Grep, and Look . from a gone directory is ENOENT, not /virtual/" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "hit.txt", .data = "needle here\n" });
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}), s.at("{s}/other.txt\n", .{try tmpPath(&s, &tmp)}));
// A pane of the served tree, and one named its root, whose directory is `/`.
try s.write(s.at("/pane/{d}/name", .{try s.newPane()}), "/virtual/index\n");
try s.write(s.at("/pane/{d}/name", .{try s.newPane()}), "/virtual\n");
try s.write(s.at("/pane/{d}/exec", .{first}), "Grep needle\n");
// `.` from a pane whose directory is gone.
const gone = try s.newPane();
try s.write(s.at("/pane/{d}/name", .{gone}), "/tmp/pardes-no-such-dir-zz/f.txt\n");
try expectEqual(@as(u16, 2), try s.refused(s.at("/pane/{d}/look", .{gone}), ".\n"));
try std.testing.expect(std.mem.startsWith(u8, s.said, "look: /tmp/pardes-no-such-dir-zz: no such directory"));
}
test "look-and-exec: a pane's tagexec runs a word as a click in its tag, and reads back what it touched" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const n = try s.focused();
const tagexec = s.at("/pane/{d}/tagexec", .{n});
try s.write(tagexec, "Msg from the tag\n");
try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "from the tag") != null);
try s.expect(tagexec, s.at("{d}\n", .{n}));
}
test "look-and-exec: a look at a directory opens a pane listing it, as acme's directory window: named with a trailing /, its entries, ctl's isdir 1, get reading it again" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.createDirPath(std.testing.io, "listed/sub");
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "listed/.dot", .data = "" });
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const dir = s.at("{s}/listed", .{try tmpPath(&s, &tmp)});
const scratch = try s.newPane();
try s.write(s.at("/pane/{d}/look", .{scratch}), s.at("{s}\n", .{dir}));
const serial = try std.fmt.parseInt(u32, std.mem.trim(u8, try s.read("/look"), "\n"), 10);
try std.testing.expect(serial != scratch);
const words = struct {
fn of(text: []const u8, buf: [][]const u8) []const []const u8 {
var it = std.mem.tokenizeAny(u8, text, " \n");
var n: usize = 0;
while (it.next()) |w| : (n += 1) buf[n] = w;
return buf[0..n];
}
}.of;
var buf: [16][]const u8 = undefined;
var rows = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n');
const row = while (rows.next()) |r| {
if (std.mem.startsWith(u8, r, s.at("{d} ", .{serial}))) break words(r, &buf);
} else return error.NoRow;
try std.testing.expectEqualStrings("text", row[1]);
try std.testing.expectEqualStrings("0", row[2]);
try std.testing.expectEqualStrings(s.at("{s}/", .{dir}), row[3]);
const body = words(try s.read(s.at("/pane/{d}/body", .{serial})), &buf);
try std.testing.expectEqual(@as(usize, 2), body.len);
try std.testing.expectEqualStrings(".dot", body[0]);
try std.testing.expectEqualStrings("sub/", body[1]);
try std.testing.expectEqualStrings("1", words(try s.read(s.at("/pane/{d}/ctl", .{serial})), &buf)[3]);
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "listed/later", .data = "" });
try s.write(s.at("/pane/{d}/ctl", .{serial}), "get\n");
const again = words(try s.read(s.at("/pane/{d}/body", .{serial})), &buf);
try std.testing.expectEqual(@as(usize, 3), again.len);
try std.testing.expectEqualStrings(".dot", again[0]);
try std.testing.expectEqualStrings("later", again[1]);
try std.testing.expectEqualStrings("sub/", again[2]);
}
|