summaryrefslogtreecommitdiff
path: root/test/behaviour/panes.zig
blob: 149c8da1399eaf568b4e591729675b53b31d35c5 (plain) (blame)
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
//! 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);
}

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();
    try s.write("/ctl", "Placement pardes\n");
    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]);
}