summaryrefslogtreecommitdiff
path: root/test/behaviour/keys.zig
blob: 81087297211c7f09f7f0470a135f250d6913bc89 (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
//! docs/typ/guide.typ, Keys <keys>.
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 => {
                    try s.click("/test.txt Save", "/test.txt ".len + 3, 1);
                    try s.keys("\x1b:");
                },
                .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");
        }
    }
}