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
|
const std = @import("std");
const Session = @import("session").Session;
const alt = "\u{F015}";
const window = "\x17"; // Ctrl-w
test "keys: a prompt's entry and cancellation leave the tag, the body and its cursor as they were" {
var s = try Session.initFile(std.testing.allocator, "one\ntwo\n");
defer s.deinit();
const n = try s.focused();
const tag = s.at("/pane/{d}/tag", .{n});
const dot = s.at("/pane/{d}/dot", .{n});
try s.replace(tag, " keep");
for ([_]struct { keys: []const u8, shown: []const u8 }{
.{ .keys = "s", .shown = " /zq" },
.{ .keys = "|", .shown = " |zq" },
.{ .keys = alt ++ "|", .shown = " |-zq" },
.{ .keys = "!", .shown = " !zq" },
.{ .keys = alt ++ "!", .shown = " !+zq" },
}) |case| {
try s.write(dot, "6 6");
try s.keys(case.keys);
// The answer is typed into the prompt's own line, never the tag.
try s.keys("zq");
_ = try s.where(case.shown);
try std.testing.expectEqualStrings("/test.txt keep", try s.read(tag));
try s.keys("\x1b");
try std.testing.expectError(error.NotOnScreen, s.where(case.shown));
try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/ctl", .{n})), " mode:normal "));
try std.testing.expectEqualStrings("/test.txt keep", try s.read(tag));
try std.testing.expectEqual(.{ 6, 6 }, try s.range(dot));
try s.expect(s.at("/pane/{d}/body", .{n}), "one\ntwo\n");
}
// Save on a scratch asks for a path, starting from its directory.
const scratch = try s.newPane();
const scratch_tag = try s.read(s.at("/pane/{d}/tag", .{scratch}));
try s.keys(":\tzq");
_ = try s.where("/zq");
try s.expect(s.at("/pane/{d}/tag", .{scratch}), scratch_tag);
try s.keys("\x1b");
try std.testing.expectError(error.NotOnScreen, s.where("/zq"));
try s.expect(s.at("/pane/{d}/tag", .{scratch}), scratch_tag);
try std.testing.expectEqual(@as(usize, 0), s.saved);
}
test "keys: a prompt opens whatever the tag already holds" {
var s = try Session.initFile(std.testing.allocator, "one\n");
defer s.deinit();
const n = try s.focused();
const tag = s.at("/pane/{d}/tag", .{n});
try s.replace(tag, &@as([4096]u8, @splat('x')));
const full = try s.read(tag);
for ([_][]const u8{ "s", "|" }) |key| {
try s.keys(key);
try s.keys("zq");
_ = try s.where("zq");
try std.testing.expectEqualStrings(full, try s.read(tag));
try s.keys("\x1b");
}
}
test "keys: generated output refuses every pipe prompt before seeding the tag" {
var s = try Session.initFile(std.testing.allocator, "one\n");
defer s.deinit();
try s.write("/ctl", "Help\n");
const n = try s.focused();
const tag = try s.read(s.at("/pane/{d}/tag", .{n}));
const body = try s.read(s.at("/pane/{d}/body", .{n}));
for ([_][]const u8{ "|", alt ++ "|", "!", alt ++ "!" }) |key| {
try s.keys(key);
try std.testing.expectError(error.NotOnScreen, s.where(" |"));
try std.testing.expectError(error.NotOnScreen, s.where(" !"));
try s.expect(s.at("/pane/{d}/tag", .{n}), tag);
try s.expect(s.at("/pane/{d}/body", .{n}), body);
}
}
test "keys: a cancelled prompt restores the selection its regex previews changed" {
var s = try Session.initFile(std.testing.allocator, "one two one");
defer s.deinit();
const n = try s.focused();
const dot = s.at("/pane/{d}/dot", .{n});
for ([_][]const u8{ "sone", "S " }) |typed| {
try s.write(dot, "0 11");
try s.keys(typed);
try s.keys("\x1b");
try std.testing.expectEqual(.{ 0, 11 }, try s.range(dot));
try s.expect(s.at("/pane/{d}/sel", .{n}), "one two one");
try s.expect(s.at("/pane/{d}/body", .{n}), "one two one");
}
}
test "keys: a counted paste of nothing leaves no count for the next motion" {
var s = try Session.initFile(std.testing.allocator, "abcdefgh\n");
defer s.deinit();
const n = try s.focused();
try s.keys("3pl");
try std.testing.expectEqual(.{ 1, 1 }, try s.range(s.at("/pane/{d}/dot", .{n})));
try s.expect(s.at("/pane/{d}/body", .{n}), "abcdefgh\n");
}
test "keys: a count or a prefix a click or a change of pane cancels leaves nothing for the next motion" {
const Gesture = enum { body, tag, focus, last };
for ([_][]const u8{ "3", "3g", "mr(" }) |typed| {
for ([_]Gesture{ .body, .tag, .focus, .last }) |gesture| {
errdefer std.debug.print("\nkeys={s} gesture={s}\n", .{ typed, @tagName(gesture) });
var s = try Session.initFile(std.testing.allocator, "abcdefghij\n");
defer s.deinit();
const n = try s.focused();
try s.write("/ctl", "Newcol\n");
const other = try s.newPane();
try s.write("/focus", s.at("{d}", .{n}));
try s.keys(typed);
switch (gesture) {
.body => try s.click("abcdefghij", 3, 1),
.tag => {
// A clicked tag is in normal mode: `:` goes back.
try s.click("/test.txt Save", "/test.txt ".len + 3, 1);
try s.keys(":");
},
.focus, .last => {
try s.keys(window ++ "l\n");
try std.testing.expectEqual(other, try s.focused());
try s.keys(if (gesture == .last) "\x1b" else window ++ "h\n");
try std.testing.expectEqual(n, try s.focused());
},
}
const dot = s.at("/pane/{d}/dot", .{n});
const at = (try s.range(dot))[0];
try s.keys("l");
try std.testing.expectEqual(at + 1, (try s.range(dot))[0]);
try s.expect(s.at("/pane/{d}/body", .{n}), "abcdefghij\n");
}
}
}
test "keys: a PDF's moving keys scroll its strip, counted, gg G and ge go to pages, and the editing keys do nothing" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const n = try s.lookFile("docs/design.pdf");
const pages = (try s.pdfPage(n))[1];
try std.testing.expect(pages > 3);
for ([_]struct { []const u8, u32 }{ .{ "ge", pages }, .{ "gg", 1 }, .{ "3gg", 3 }, .{ "2G", 2 }, .{ "G", pages }, .{ "gg", 1 } }) |step| {
try s.keys(step[0]);
try std.testing.expectEqual(step[1], (try s.pdfPage(n))[0]);
}
// j a cell's pixels, Ctrl-f a window's: a count moves that many, so
// thirty windows down is past the first page (a window is a few rows
// of a page here), thirty back at the first, and k at the top stays.
try s.keys("k");
try std.testing.expectEqual(@as(u32, 1), (try s.pdfPage(n))[0]);
try s.keys("30\x06");
try std.testing.expect((try s.pdfPage(n))[0] > 1);
try s.keys("30\x02");
try std.testing.expectEqual(@as(u32, 1), (try s.pdfPage(n))[0]);
try s.keys("200j");
try std.testing.expect((try s.pdfPage(n))[0] > 1);
try s.keys("2G");
// Consumed, with their count: the page stays, no selection begins, and
// no mode changes.
try s.keys("4dv|i");
try std.testing.expectEqual(@as(u32, 2), (try s.pdfPage(n))[0]);
try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/ctl", .{n})), " mode:normal "));
}
test "keys: SPC j n and SPC j p in plain text select its places, never a comment's slashes, round the end, opening nothing" {
var s = try Session.initFile(std.testing.allocator, "// ordinary comment\n/// documentation comment\n(//) [///] punctuation\n/\n// /tmp\n//tmp\n");
defer s.deinit();
const n = try s.focused();
const index = try s.read("/index");
for ([_]struct { []const u8, []const u8, u32 }{
.{ " jn", "/", 69 }, .{ " jn", "/tmp", 74 }, .{ " jn", "//tmp", 79 }, .{ " jn", "/", 69 },
.{ " jp", "//tmp", 79 }, .{ " jp", "/tmp", 74 }, .{ " jp", "/", 69 },
}) |step| {
try s.keys(step[0]);
try std.testing.expectEqual(n, try s.focused());
try s.expect(s.at("/pane/{d}/sel", .{n}), step[1]);
try std.testing.expectEqual(step[2], (try s.range(s.at("/pane/{d}/dot", .{n})))[0]);
try s.expect("/index", index);
}
}
/// The value the root ctl's read gives `word`.
fn setting(s: *Session, word: []const u8) ![]const u8 {
var lines = std.mem.splitScalar(u8, try s.read("/ctl"), '\n');
while (lines.next()) |line| if (std.mem.startsWith(u8, line, word) and line.len > word.len and line[word.len] == ' ') return line[word.len + 1 ..];
return error.NoSuchSetting;
}
test "keys: SPC j n and SPC j p walk Themes' rows, whole, never its section headings, and Tab sets the one selected" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const text = try s.focused();
// An imported theme's, the list still begins with the natives.
try s.write("/ctl", "Theme dark\n");
try s.write(s.at("/pane/{d}/exec", .{text}), "Themes\n");
// Listed beside the text, which keeps the keyboard.
try std.testing.expectEqual(text, try s.focused());
const list = text + 1;
try std.testing.expect(std.mem.indexOf(u8, try s.read("/index"), s.at("{d} text 0 /+Themes ", .{list})) != null);
const body = try s.read(s.at("/pane/{d}/body", .{list}));
// A heading, then a row a theme; each section after the first a blank
// line and its heading.
var lines = std.mem.splitScalar(u8, body, '\n');
try std.testing.expectEqualStrings("# Pardes themes", lines.next().?);
var names: std.ArrayList([]const u8) = .empty;
var first_break: ?usize = null;
while (lines.next()) |line| {
if (line.len == 0) {
const heading = lines.next() orelse break; // the newline ending the last row
try std.testing.expect(std.mem.startsWith(u8, heading, "# "));
first_break = first_break orelse names.items.len;
continue;
}
try std.testing.expect(std.mem.startsWith(u8, line, "Theme "));
try names.append(s.arena.allocator(), line);
}
const brk = first_break orelse return error.NoSections;
try std.testing.expect(std.mem.indexOf(u8, body[0..std.mem.indexOf(u8, body, "\n\n").?], "\nTheme acme\n") != null);
// Across the first section's end and back, the theme as it was.
for (names.items[0 .. brk + 1]) |row| {
try s.keys(" jn");
try std.testing.expectEqual(list, try s.focused());
try s.expect(s.at("/pane/{d}/sel", .{list}), row);
try std.testing.expectEqualStrings("dark", try setting(&s, "Theme"));
}
try s.keys(" jp");
try s.expect(s.at("/pane/{d}/sel", .{list}), names.items[brk - 1]);
try s.keys("\t");
try std.testing.expectEqualStrings(names.items[brk - 1]["Theme ".len..], try setting(&s, "Theme"));
}
test "keys: Save's prompt on a scratch starts from the scratch's directory, one slash after it" {
for ([_][2][]const u8{ .{ "/dír space/f.txt", " Save /dír space/zq" }, .{ "/f.txt", " Save /zq" } }) |c| {
var s = try Session.initFile(std.testing.allocator, "one\n");
defer s.deinit();
const n = try s.focused();
try s.write(s.at("/pane/{d}/name", .{n}), s.at("{s}\n", .{c[0]}));
// New from the file's pane: a scratch in its directory.
try s.write(s.at("/pane/{d}/exec", .{n}), "New\n");
try std.testing.expect(try s.focused() != n);
try s.keys(":\tzq");
_ = try s.where(c[1]);
try s.keys("\x1b");
}
}
/// A B1 sweep from cell (x0, y0) to (x1, y1).
fn sweep(s: *Session, x0: usize, y0: usize, x1: usize, y1: usize) !void {
try s.write("/mousein", s.at("m {d} {d} 1\nm {d} {d} 1\nm {d} {d} 0\n", .{ x0, y0, x1, y1, x1, y1 }));
}
/// Whether any cell /screen draws is in `style`.
fn drawsIn(s: *Session, style: []const u8) !bool {
for (try s.styles()) |cell| if (std.mem.eql(u8, cell, style)) return true;
return false;
}
test "keys: Ctrl-o and Ctrl-i jump back and forth, leaving no sweep drawn, a wrapped Unicode and tab one included" {
const ctl = "\u{F017}";
for ([_]struct { []const u8, []const u8, bool }{
.{ "alpha\nbravo\n", "alpha", false },
.{ "界\tabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\n", "界", true },
}) |c| for ([_]bool{ false, true }) |reverse| {
const prefix, const first, const wrap = c;
var buf: [4096]u8 = undefined;
@memcpy(buf[0..prefix.len], prefix);
for (0..80) |i| @memcpy(buf[prefix.len + i * 27 ..][0..27], "unrelated destination text\n");
var s = try Session.initFile(std.testing.allocator, buf[0 .. prefix.len + 80 * 27]);
defer s.deinit();
const n = try s.focused();
s.detach();
try s.write("/ctl", "size 34 10\n");
try s.write("/ctl", if (wrap) "Wrap on\n" else "Wrap off\n");
const dot = s.at("/pane/{d}/dot", .{n});
const addr = s.at("/pane/{d}/addr", .{n});
// Two places in the jump list: line 60, then line 1.
try s.keys("60G1G");
const x, const y = try s.where(first);
const plain = (try s.styles())[0];
if (reverse) try sweep(&s, x + 3, y + 1, x + 1, y) else try sweep(&s, x + 1, y, x + 3, y + 1);
const screen = try s.screen();
const cols = try std.unicode.utf8CountCodepoints(screen[0..std.mem.indexOfScalar(u8, screen, '\n').?]);
const swept = (try s.styles())[(y + 1) * cols + x];
try std.testing.expect(!std.mem.eql(u8, swept, plain));
try s.keys(ctl ++ "o");
try s.write(addr, "60");
const line60 = (try s.range(addr))[0];
try std.testing.expectEqual(.{ line60, line60 }, try s.range(dot));
try std.testing.expectError(error.NotOnScreen, s.where(first));
try std.testing.expect(!try drawsIn(&s, swept));
// A sweep made after Back is gone after Forward.
const x2, const y2 = try s.where("unrelated");
try sweep(&s, x2 + 1, y2, x2 + 3, y2 + 1);
try std.testing.expect(try drawsIn(&s, swept));
try s.keys(ctl ++ "i");
// Back where the sweep left the caret, nothing selected.
const q0, const q1 = try s.range(dot);
try std.testing.expect(q0 == q1 and q1 < line60);
_ = try s.where(first);
try std.testing.expect(!try drawsIn(&s, swept));
};
}
// Its rows whole and Tab running one are the Themes walk's test above.
test "keys: SPC j n from the pane a Look gave the keyboard walks back into the command output" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
for ([_][]const u8{ "a.txt", "b.txt" }) |name| try tmp.dir.writeFile(std.testing.io, .{ .sub_path = name, .data = "x\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, "x\n");
defer s.deinit();
try s.write("/look", s.at("{s}/a.txt\n", .{at}));
const a = try s.focused();
try s.write(s.at("/pane/{d}/exec", .{a}), "Themes\n");
try s.keys(" jn");
const themes = try s.focused();
try std.testing.expect(themes != a);
const sel = s.at("/pane/{d}/sel", .{themes});
try s.expect(sel, "Theme orchard");
try s.write(s.at("/pane/{d}/exec", .{a}), "Look b.txt\n");
try std.testing.expect(try s.focused() != themes);
try s.keys(" jn");
try std.testing.expectEqual(themes, try s.focused());
try std.testing.expect(std.mem.startsWith(u8, try s.read(sel), "Theme "));
}
/// Screen row `row` as drawn.
fn screenRow(s: *Session, row: usize) ![]const u8 {
var rows = std.mem.splitScalar(u8, try s.screen(), '\n');
for (0..row) |_| _ = rows.next();
return rows.next().?;
}
test "keys: a PDF in normal mode keeps : (its tag and back), SPC (a leader, cancelled by a key it has not) and / (a search prompt, Esc closing it)" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
const n = try s.lookFile("docs/design.pdf");
// A page has no text cursor: the body shows none.
try std.testing.expect(try s.cursor() == null);
_ = try s.pdfPage(n);
const tag = (try s.where("[1/"))[1];
try s.keys(":");
// On its tag, which runs to three rows (its path, the core's art words).
const on_tag = (try s.cursor()).?[1];
try std.testing.expect(on_tag >= tag and on_tag <= tag + 2);
try std.testing.expect(std.mem.endsWith(u8, try s.read(s.at("/pane/{d}/ctl", .{n})), " mode:normal "));
try s.keys(":");
try std.testing.expect(try s.cursor() == null);
// The leader shows its keys until one it has not.
try s.keys(" ");
try std.testing.expect(std.mem.indexOf(u8, try s.screen(), " SPC") != null);
try s.keys("!");
try std.testing.expect(std.mem.indexOf(u8, try s.screen(), " SPC") == null);
try s.keys("/");
try std.testing.expect(try s.cursor() != null);
try std.testing.expectEqual(n, try s.focused());
try s.keys("\x1b");
try std.testing.expect(try s.cursor() == null);
try std.testing.expectEqual(n, try s.focused());
}
|