summaryrefslogtreecommitdiff
path: root/src/vaxis_input.zig
blob: dcad9ef19a6931f21ed7b64f7d845f3c54a10fdc (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
//! A key as vaxis parses it from a terminal's bytes, as the core's Event:
//! the one map for every shell that reads a terminal (tty, gui's stdin).
//! Three copies had drifted; the gui's had lost the F-keys,
//! Insert and the keypad. And the other way, a cell's style as vaxis draws
//! it.
const std = @import("std");
const vaxis = @import("vaxis");
const pardes = @import("pardes.zig");

/// The effective codepoint the way vaxis's own `Key.matches` sees it: a
/// single-character `text` wins, the terminal having resolved shift;
/// otherwise the shifted codepoint.
pub fn effCp(key: vaxis.Key) u21 {
    if (key.text) |t| {
        const view = std.unicode.Utf8View.init(t) catch return key.codepoint;
        var it = view.iterator();
        if (it.nextCodepoint()) |c| {
            if (it.nextCodepoint() == null) return c;
        }
    }
    return key.shifted_codepoint orelse key.codepoint;
}

/// vaxis's functional-key codepoints as the core's; the ASCII ones
/// (enter, tab, escape, backspace) already coincide.
pub fn mapKey(cp: u21) u21 {
    return switch (cp) {
        vaxis.Key.kp_enter => pardes.Key.enter,
        vaxis.Key.up => pardes.Key.up,
        vaxis.Key.down => pardes.Key.down,
        vaxis.Key.left => pardes.Key.left,
        vaxis.Key.right => pardes.Key.right,
        vaxis.Key.home => pardes.Key.home,
        vaxis.Key.end => pardes.Key.end,
        vaxis.Key.page_up => pardes.Key.page_up,
        vaxis.Key.page_down => pardes.Key.page_down,
        vaxis.Key.delete => pardes.Key.delete,
        vaxis.Key.insert => pardes.Key.insert,
        vaxis.Key.print_screen => pardes.Key.print_screen,
        vaxis.Key.scroll_lock => pardes.Key.scroll_lock,
        vaxis.Key.pause => pardes.Key.pause,
        vaxis.Key.menu => pardes.Key.menu,
        vaxis.Key.f1...vaxis.Key.f25 => pardes.Key.f1 + (cp - vaxis.Key.f1),
        // The keypad's own keys, which the kitty keyboard protocol tells
        // apart, do what the keys they are named for do.
        vaxis.Key.kp_left => pardes.Key.left,
        vaxis.Key.kp_right => pardes.Key.right,
        vaxis.Key.kp_up => pardes.Key.up,
        vaxis.Key.kp_down => pardes.Key.down,
        vaxis.Key.kp_page_up => pardes.Key.page_up,
        vaxis.Key.kp_page_down => pardes.Key.page_down,
        vaxis.Key.kp_home => pardes.Key.home,
        vaxis.Key.kp_end => pardes.Key.end,
        vaxis.Key.kp_insert => pardes.Key.insert,
        vaxis.Key.kp_delete => pardes.Key.delete,
        else => cp,
    };
}

/// The key as the core's event, its text the key's own.
pub fn keyEvent(key: vaxis.Key) pardes.Event {
    return .{ .key = .{
        .cp = mapKey(effCp(key)),
        .text = key.text orelse "",
        .ctrl = key.mods.ctrl,
        .alt = key.mods.alt,
        .shift = key.mods.shift,
    } };
}

/// What a key typed inside a bracketed paste adds to it: its text, else a
/// tab, or a newline for Enter (the keypad's too) and Ctrl-J.
pub fn pasteBytes(key: vaxis.Key) []const u8 {
    const text = key.text orelse "";
    if (text.len > 0) return text;
    const cp = mapKey(effCp(key));
    if (cp == pardes.Key.tab) return "\t";
    if (cp == pardes.Key.enter or (key.mods.ctrl and cp == 'j')) return "\n";
    return "";
}

/// A cell's style as vaxis draws it.
pub fn style(s: pardes.CellStyle) vaxis.Style {
    return .{
        .fg = color(s.fg),
        .bg = color(s.bg),
        .bold = s.bold,
        .dim = s.dim,
        .italic = s.italic,
        .blink = s.blink,
        .reverse = s.reverse,
        .invisible = s.invisible,
        .strikethrough = s.strikethrough,
        // The two underline enums name the same six styles.
        .ul_style = switch (s.ul) {
            inline else => |ul| @field(@FieldType(vaxis.Style, "ul_style"), @tagName(ul)),
        },
    };
}

pub fn color(c: pardes.Color) vaxis.Color {
    return switch (c) {
        .default => .default,
        .index => |i| .{ .index = i },
        .rgb => |rgb| .{ .rgb = rgb },
    };
}

test "F-keys, Insert and the keypad map to the core's keys" {
    try std.testing.expectEqual(pardes.Key.f1 + 4, mapKey(vaxis.Key.f5));
    try std.testing.expectEqual(pardes.Key.f1 + 24, mapKey(vaxis.Key.f25));
    try std.testing.expectEqual(pardes.Key.insert, mapKey(vaxis.Key.insert));
    try std.testing.expectEqual(pardes.Key.up, mapKey(vaxis.Key.kp_up));
    try std.testing.expectEqual(pardes.Key.enter, keyEvent(.{ .codepoint = vaxis.Key.kp_enter }).key.cp);
    try std.testing.expectEqual(@as(u21, 'a'), mapKey('a'));
}

test "an event queue emptied in one batch lets every writer it held go on (build.zig's patch)" {
    const io = std.testing.io;
    const Q = vaxis.Queue(u8, 2);
    var q: Q = .init(io);
    try q.push(1);
    try q.push(2);
    const Writer = struct {
        fn run(queue: *Q, byte: u8) !void {
            try queue.push(byte);
        }
    };
    // Two writers waiting on the full queue, as a pane's pty reader and
    // the frame ack's wake do under `yes`.
    var a = try io.concurrent(Writer.run, .{ &q, 3 });
    defer a.cancel(io) catch {};
    var b = try io.concurrent(Writer.run, .{ &q, 4 });
    defer b.cancel(io) catch {};
    try io.sleep(.fromMilliseconds(20), .awake);
    // The loop takes both at once: one pop from full, then one more.
    try q.lock();
    _ = q.drain().?;
    _ = q.drain().?;
    q.unlock();
    var got: usize = 0;
    var waited: usize = 0;
    while (got < 2 and waited < 200) : (waited += 1) {
        if (try q.tryPop() != null) got += 1 else try io.sleep(.fromMilliseconds(5), .awake);
    }
    try std.testing.expectEqual(@as(usize, 2), got);
}