ref(key): make Key struct packed and rename constants
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 2m5s

This commit is contained in:
2025-02-17 21:06:15 +01:00
parent 7891af6c6f
commit a9f48bfb6a
8 changed files with 207 additions and 212 deletions

View File

@@ -6,7 +6,8 @@ const event = @import("event.zig");
const mergeTaggedUnions = event.mergeTaggedUnions;
const isTaggedUnion = event.isTaggedUnion;
const Key = @import("key.zig");
const key = @import("key.zig");
const Key = key.Key;
const Size = @import("size.zig").Size;
const Queue = @import("queue.zig").Queue;
@@ -164,11 +165,6 @@ pub fn App(comptime E: type) type {
}
fn run(this: *@This()) !void {
// send initial terminal size
// changes are handled by the winch signal handler
// see `App.start` and `App.registerWinch` for details
{}
// thread to read user inputs
var buf: [256]u8 = undefined;
while (true) {
@@ -181,22 +177,21 @@ pub fn App(comptime E: type) type {
0x4F => { // ss3
if (read_bytes < 3) continue;
const key: ?Key = switch (buf[2]) {
0x1B => null,
'A' => .{ .cp = Key.up },
'B' => .{ .cp = Key.down },
'C' => .{ .cp = Key.right },
'D' => .{ .cp = Key.left },
'E' => .{ .cp = Key.kp_begin },
'F' => .{ .cp = Key.end },
'H' => .{ .cp = Key.home },
'P' => .{ .cp = Key.f1 },
'Q' => .{ .cp = Key.f2 },
'R' => .{ .cp = Key.f3 },
'S' => .{ .cp = Key.f4 },
else => null,
const k: Key = switch (buf[2]) {
'A' => .{ .cp = key.Up },
'B' => .{ .cp = key.Down },
'C' => .{ .cp = key.Right },
'D' => .{ .cp = key.Left },
'E' => .{ .cp = key.Kp_Begin },
'F' => .{ .cp = key.End },
'H' => .{ .cp = key.Home },
'P' => .{ .cp = key.F1 },
'Q' => .{ .cp = key.F2 },
'R' => .{ .cp = key.F3 },
'S' => .{ .cp = key.F4 },
else => continue,
};
if (key) |k| this.postEvent(.{ .key = k });
this.postEvent(.{ .key = k });
},
0x5B => { // csi
if (read_bytes < 3) continue;
@@ -215,23 +210,23 @@ pub fn App(comptime E: type) type {
// Legacy keys
// CSI {ABCDEFHPQS}
// CSI 1 ; modifier:event_type {ABCDEFHPQS}
const key: Key = .{
const k: Key = .{
.cp = switch (final) {
'A' => Key.up,
'B' => Key.down,
'C' => Key.right,
'D' => Key.left,
'E' => Key.kp_begin,
'F' => Key.end,
'H' => Key.home,
'P' => Key.f1,
'Q' => Key.f2,
'R' => Key.f3,
'S' => Key.f4,
'A' => key.Up,
'B' => key.Down,
'C' => key.Right,
'D' => key.Left,
'E' => key.Kp_Begin,
'F' => key.End,
'H' => key.Home,
'P' => key.F1,
'Q' => key.F2,
'R' => key.F3,
'S' => key.F4,
else => unreachable, // switch case prevents in this case form ever happening
},
};
this.postEvent(.{ .key = key });
this.postEvent(.{ .key = k });
},
'~' => {
// Legacy keys
@@ -242,33 +237,33 @@ pub fn App(comptime E: type) type {
const number_buf = field_iter.next() orelse unreachable; // always will have one field
const number = std.fmt.parseUnsigned(u16, number_buf, 10) catch break;
const key: Key = .{
const k: Key = .{
.cp = switch (number) {
2 => Key.insert,
3 => Key.delete,
5 => Key.page_up,
6 => Key.page_down,
7 => Key.home,
8 => Key.end,
11 => Key.f1,
12 => Key.f2,
13 => Key.f3,
14 => Key.f4,
15 => Key.f5,
17 => Key.f6,
18 => Key.f7,
19 => Key.f8,
20 => Key.f9,
21 => Key.f10,
23 => Key.f11,
24 => Key.f12,
2 => key.Insert,
3 => key.Delete,
5 => key.Page_Up,
6 => key.Page_Down,
7 => key.Home,
8 => key.End,
11 => key.F1,
12 => key.F2,
13 => key.F3,
14 => key.F4,
15 => key.F5,
17 => key.F6,
18 => key.F7,
19 => key.F8,
20 => key.F9,
21 => key.F10,
23 => key.F11,
24 => key.F12,
// 200 => return .{ .event = .paste_start, .n = sequence.len },
// 201 => return .{ .event = .paste_end, .n = sequence.len },
57427 => Key.kp_begin,
57427 => key.Kp_Begin,
else => unreachable,
},
};
this.postEvent(.{ .key = key });
this.postEvent(.{ .key = k });
},
'I' => this.postEvent(.{ .focus = true }),
'O' => this.postEvent(.{ .focus = false }),
@@ -323,24 +318,24 @@ pub fn App(comptime E: type) type {
}
} else {
const b = buf[0];
const key: Key = switch (b) {
const k: Key = switch (b) {
0x00 => .{ .cp = '@', .mod = .{ .ctrl = true } },
0x08 => .{ .cp = Key.backspace },
0x09 => .{ .cp = Key.tab },
0x0a, 0x0d => .{ .cp = Key.enter },
0x08 => .{ .cp = key.Backspace },
0x09 => .{ .cp = key.Tab },
0x0a, 0x0d => .{ .cp = key.Enter },
0x01...0x07, 0x0b...0x0c, 0x0e...0x1a => .{ .cp = b + 0x60, .mod = .{ .ctrl = true } },
0x1b => escape: {
std.debug.assert(read_bytes == 1);
break :escape .{ .cp = Key.escape };
break :escape .{ .cp = key.Escape };
},
0x7f => .{ .cp = Key.backspace },
0x7f => .{ .cp = key.Backspace },
else => {
var iter = terminal.code_point.Iterator{ .bytes = buf[0..read_bytes] };
while (iter.next()) |cp| this.postEvent(.{ .key = .{ .cp = cp.code } });
continue;
},
};
this.postEvent(.{ .key = key });
this.postEvent(.{ .key = k });
}
continue;
};