summaryrefslogtreecommitdiff
path: root/src/key.zig
blob: d2748e7ea595e6177076c21b71e5d558255149fe (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
164
165
166
167
168
169
const std = @import("std");

pub const Key = enum(u16) {
    tab = 0x09,
    return_ = 0x0d,
    escape = 0x1b,
    space = 0x20,
    backspace = 0x7f,
    max_char = 0xff,

    up,
    down,
    right,
    left,
    end,
    home,
    untab,
    insert,
    delete,
    page_up,
    page_down,

    mod_shft = 0x1000,
    mod_meta = 0x2000,
    mod_ctrl = 0x4000,

    _,

    pub fn char(ch: u8) Key {
        return @enumFromInt(ch);
    }

    pub fn shift(k: anytype) Key {
        return Key.mod(.mod_shft, Key.ify(k));
    }

    pub fn meta(k: anytype) Key {
        return Key.mod(.mod_meta, Key.ify(k));
    }

    pub fn ctrl(k: anytype) Key {
        return Key.mod(.mod_ctrl, Key.ify(k));
    }

    /// Key.ify == Keyify :)
    fn ify(k: anytype) Key {
        return switch (@TypeOf(k)) {
            comptime_int, u8 => Key.char(k),
            Key => k,
            else => unreachable,
        };
    }

    fn mod(comptime modifier: Key, k: Key) Key {
        comptime std.debug.assert(modifier == .mod_shft or modifier == .mod_meta or modifier == .mod_ctrl);

        const shft_int = @intFromEnum(Key.mod_shft);
        const meta_int = @intFromEnum(Key.mod_meta);
        const ctrl_int = @intFromEnum(Key.mod_ctrl);

        const max_char_int = @intFromEnum(Key.max_char);

        const mod_int = @intFromEnum(modifier);
        const k_int = @intFromEnum(k);
        if (k_int & mod_int == mod_int) {
            return k;
        }

        const k_origmod = k_int & (shft_int | meta_int | ctrl_int);
        const k_nomod = k_int & ~k_origmod;
        if (k_nomod <= max_char_int) {
            // Appending S- to a character is not smart
            std.debug.assert(modifier != .mod_shft);
            return switch (modifier) {
                .mod_meta => @enumFromInt(k_int | meta_int),
                .mod_ctrl => @enumFromInt(k_origmod | (k_nomod & 0x1f)),
                else => unreachable,
            };
        } else {
            return @enumFromInt(k_int | mod_int);
        }
    }

    pub fn format(
        key: Key,
        comptime fmt: []const u8,
        options: std.fmt.FormatOptions,
        writer: anytype,
    ) @TypeOf(writer).Error!void {
        comptime if (fmt.len != 0) {
            @compileError("Key doesn't support {" ++ fmt ++ "} format");
        };

        return switch (key) {
            .tab => std.fmt.formatBuf("<tab>", options, writer),
            .return_ => std.fmt.formatBuf("<return>", options, writer),
            .escape => std.fmt.formatBuf("<escape>", options, writer),
            .space => std.fmt.formatBuf("<space>", options, writer),
            .backspace => std.fmt.formatBuf("<backspace>", options, writer),
            .max_char => key.formatGeneric(options, writer),

            .up => std.fmt.formatBuf("<up>", options, writer),
            .down => std.fmt.formatBuf("<down>", options, writer),
            .right => std.fmt.formatBuf("<right>", options, writer),
            .left => std.fmt.formatBuf("<left>", options, writer),
            .end => std.fmt.formatBuf("<end>", options, writer),
            .home => std.fmt.formatBuf("<home>", options, writer),
            .untab => std.fmt.formatBuf("<untab>", options, writer),
            .insert => std.fmt.formatBuf("<insert>", options, writer),
            .delete => std.fmt.formatBuf("<delete>", options, writer),
            .page_up => std.fmt.formatBuf("<page-up>", options, writer),
            .page_down => std.fmt.formatBuf("<page-down>", options, writer),

            .mod_shft, .mod_meta, .mod_ctrl => key.formatGeneric(options, writer),
            _ => key.formatGeneric(options, writer),
        };
    }

    fn formatGeneric(
        key: Key,
        options: std.fmt.FormatOptions,
        writer: anytype,
    ) @TypeOf(writer).Error!void {
        const shft_int = @intFromEnum(Key.mod_shft);
        const meta_int = @intFromEnum(Key.mod_meta);
        const ctrl_int = @intFromEnum(Key.mod_ctrl);

        const key_int = @intFromEnum(key);
        if (key_int & shft_int == shft_int) {
            try std.fmt.formatBuf("S-", options, writer);
            return Key.format(
                @enumFromInt(key_int & ~shft_int),
                "",
                options,
                writer,
            );
        } else if (key_int & meta_int == meta_int) {
            try std.fmt.formatBuf("M-", options, writer);
            return Key.format(
                @enumFromInt(key_int & ~meta_int),
                "",
                options,
                writer,
            );
        } else if (key_int & ctrl_int == ctrl_int) {
            try std.fmt.formatBuf("C-", options, writer);
            return Key.format(
                @enumFromInt(key_int & ~ctrl_int),
                "",
                options,
                writer,
            );
        } else if (key_int < 0x20) {
            try std.fmt.formatBuf("C-", options, writer);
            return Key.format(Key.char(@intCast(key_int + 0x40)), "", options, writer);
        } else if (key_int < 0x100) {
            const ch: u8 = @intCast(key_int);
            if (std.ascii.isPrint(ch)) {
                return writer.writeByte(ch);
            } else {
                try writer.writeAll("<\\x");
                try std.fmt.formatIntValue(ch, "X", options, writer);
                return writer.writeAll(">");
            }
        } else {
            unreachable;
        }
    }
};