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
|
//! docs/typ/reference.typ, Placement <placement>.
const std = @import("std");
const Session = @import("session").Session;
test "placement: an empty active column takes the new pane whole, a look from another column's text too" {
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, "x\n");
defer s.deinit();
const first = try s.focused();
// Its tag has the keyboard: it is the active column, and stays so
// when Esc gives the keyboard back.
try s.write("/ctl", "Newcol\n");
try s.keys("\x1b");
try std.testing.expectEqual(first, try s.focused());
// A look from the first pane's text still lands in the active column,
// as acme's makenewwindow puts it in activecol first (util.c:456).
try s.write(s.at("/pane/{d}/look", .{first}), s.at("{s}/f.txt\n", .{at}));
const made = try s.focused();
try std.testing.expectEqualStrings(s.at("{d}:{d} {d}:{d}", .{ try s.column(0), first, try s.column(1), made }), try s.columns());
}
/// The screen row the tag starting `name` is drawn on.
fn tagRow(s: *Session, name: []const u8) !usize {
return (try s.where(s.at("{s} Save", .{name})))[1];
}
test "placement: New and 9P's new take the bottom half of the column's last pane" {
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}/exec", .{first}), "New\n");
const last = try s.focused();
try s.write(s.at("/pane/{d}/name", .{last}), "/last\n");
const was = try tagRow(&s, "/last");
const made = try s.newPane();
try s.write(s.at("/pane/{d}/name", .{made}), "/made\n");
try std.testing.expectEqualStrings(s.at("{d}:{d},{d},{d}", .{ try s.column(0), first, last, made }), try s.columns());
// The last pane keeps its top and gives up its bottom half.
try std.testing.expectEqual(was, try tagRow(&s, "/last"));
const rows = std.mem.count(u8, try s.screen(), "\n") + 1;
try std.testing.expect(try tagRow(&s, "/made") >= was + (rows - was) / 2);
}
test "placement: Placement pardes puts a new pane right under the asking one, and bare it steps back to acme" {
var s = try Session.initFile(std.testing.allocator, "x\n");
defer s.deinit();
try s.write("/ctl", "Placement pardes\n");
try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "\nPlacement pardes\n") != null);
const top = try s.focused();
try s.write(s.at("/pane/{d}/exec", .{top}), "New\n");
const below = try s.focused();
// Directly under the asking pane, as New always put it.
try s.write(s.at("/pane/{d}/exec", .{top}), "New\n");
try std.testing.expectEqualStrings(s.at("{d}:{d},{d},{d}", .{ try s.column(0), top, try s.focused(), below }), try s.columns());
try s.write("/ctl", "Placement\n");
try std.testing.expect(std.mem.indexOf(u8, try s.read("/ctl"), "\nPlacement acme\n") != null);
}
test "placement: a look under pardes's placement, from a pane pane/new made after Delcol, opens its file in a column" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "file\n" });
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 first = try s.focused();
try s.write(s.at("/pane/{d}/exec", .{first}), "Placement\n");
try s.write(s.at("/pane/{d}/exec", .{first}), "Delcol\n");
const made = try s.newPane();
try s.write(s.at("/pane/{d}/look", .{made}), s.at("{s}/f.txt:#4\n", .{dir}));
const opened = try s.focused();
try std.testing.expect(opened != made);
// The pane pane/new made is placed, and so is the one its look opened.
const cols = try s.columns();
for ([_]u32{ made, opened }) |n| try std.testing.expect(std.mem.indexOf(u8, cols, s.at(":{d}", .{n})) != null or std.mem.indexOf(u8, cols, s.at(",{d}", .{n})) != null);
}
/// Each pane's tag row and its rows down to the next tag, or the screen's
/// end, as /screen draws them; `names` top to bottom in one column.
fn spans(s: *Session, comptime n: usize, names: [n][]const u8) ![n][2]usize {
var out: [n][2]usize = undefined;
for (names, 0..) |name, i| out[i][0] = try tagRow(s, name);
const rows = std.mem.count(u8, try s.screen(), "\n") + 1;
for (0..n) |i| out[i][1] = (if (i + 1 < n) out[i + 1][0] else rows) - out[i][0];
return out;
}
test "placement: a look from a pane's text goes under its text when it has many blank rows, else halves the biggest pane" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
for ([_][]const u8{ "second.txt", "third.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, "one\ntwo\nthree\n");
defer s.deinit();
s.detach();
// Wide enough that a tag with the test's temporary path stays one row,
// wherever the checkout is.
try s.write("/ctl", "size 200 60\n");
const top = try s.focused();
// Most of the pane is blank: the look starts under its three lines
// (makenewwindow, util.c:482-486).
try s.write(s.at("/pane/{d}/look", .{top}), s.at("{s}/second.txt\n", .{at}));
const second = try s.focused();
const shown = s.at("{s}/second.txt", .{at});
try std.testing.expectEqual((try tagRow(&s, "/test.txt")) + 1 + 3, try tagRow(&s, shown));
// Both full of text: no blank space is big, so the biggest is halved.
const full = std.mem.asBytes(&@as([80][2]u8, @splat("x\n".*)));
for ([_]u32{ top, second }) |n| try s.replace(s.at("/pane/{d}/body", .{n}), full);
const was = try spans(&s, 2, .{ "/test.txt", shown });
const big: usize = if (was[1][1] >= was[0][1]) 1 else 0;
try s.write(s.at("/pane/{d}/look", .{second}), s.at("{s}/third.txt\n", .{at}));
const third = try s.focused();
const ids = [_]u32{ top, second };
try std.testing.expect(std.mem.indexOf(u8, try s.columns(), s.at("{d},{d}", .{ ids[big], third })) != null);
const made = s.at("{s}/third.txt", .{at});
const now = if (big == 1) try spans(&s, 3, .{ "/test.txt", shown, made }) else try spans(&s, 3, .{ "/test.txt", made, shown });
try std.testing.expectEqual(was[big][0] + was[big][1] / 2, now[big + 1][0]);
try std.testing.expectEqual(was[big][0] + was[big][1], now[big + 1][0] + now[big + 1][1]);
}
test "placement: a look from the middle of a column splits only the pane it came from, short ones round it kept short, through a Newcol too" {
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
for ([_][]const u8{ "mid", "low", "made" }) |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, "one\n");
defer s.deinit();
s.detach();
try s.write("/ctl", "size 100 60\n");
const top = try s.focused();
try s.write(s.at("/pane/{d}/look", .{top}), s.at("{s}/mid\n", .{at}));
const mid = try s.focused();
try s.write(s.at("/pane/{d}/look", .{mid}), s.at("{s}/low\n", .{at}));
const low = try s.focused();
const full = std.mem.asBytes(&@as([80][2]u8, @splat("x\n".*)));
for ([_]u32{ top, mid, low }) |n| try s.replace(s.at("/pane/{d}/body", .{n}), full);
// Short names, so a narrowed column draws each tag on one row.
try s.write(s.at("/pane/{d}/name", .{mid}), "/mid\n");
try s.write(s.at("/pane/{d}/name", .{low}), "/low\n");
const names = [_][]const u8{ "/test.txt", "/mid", "/low", "/made" };
// The top and bottom panes dragged by their boxes to two and three
// rows, under a placement's minimum; the middle one full of text, so
// the one halved.
const rows = std.mem.count(u8, try s.screen(), "\n") + 1;
const r = try spans(&s, 3, names[0..3].*);
try s.write("/mousein", s.at("m 0 {d} 1\nm 0 {d} 1\nm 0 {d} 0\n", .{ r[1][0], r[0][0] + 2, r[0][0] + 2 }));
try s.write("/mousein", s.at("m 0 {d} 1\nm 0 {d} 1\nm 0 {d} 0\n", .{ r[2][0], rows - 3, rows - 3 }));
const was = try spans(&s, 3, names[0..3].*);
try std.testing.expectEqual(@as(usize, 2), was[0][1]);
try std.testing.expectEqual(@as(usize, 3), was[2][1]);
try s.write(s.at("/pane/{d}/look", .{mid}), s.at("{s}/made\n", .{at}));
const made = try s.focused();
try s.write(s.at("/pane/{d}/name", .{made}), "/made\n");
try std.testing.expectEqualStrings(s.at("{d}:{d},{d},{d},{d}", .{ try s.column(0), top, mid, made, low }), try s.columns());
const now = try spans(&s, 4, .{ names[0], names[1], names[3], names[2] });
try std.testing.expectEqual(was[0], now[0]);
try std.testing.expectEqual(was[2], now[3]);
// A new column narrows this one, and the short panes keep their rows.
try s.write(s.at("/pane/{d}/exec", .{made}), "Newcol\n");
try std.testing.expectEqual(@as(usize, 2), std.mem.count(u8, try s.columns(), ":"));
const narrowed = try spans(&s, 4, .{ names[0], names[1], names[3], names[2] });
try std.testing.expectEqual(was[0][1], narrowed[0][1]);
try std.testing.expectEqual(was[2][1], narrowed[3][1]);
}
|