summaryrefslogtreecommitdiff
path: root/src/key.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/key.zig')
-rw-r--r--src/key.zig51
1 files changed, 21 insertions, 30 deletions
diff --git a/src/key.zig b/src/key.zig
index f2538bd..d2748e7 100644
--- a/src/key.zig
+++ b/src/key.zig
@@ -27,7 +27,7 @@ pub const Key = enum(u16) {
27 _, 27 _,
28 28
29 pub fn char(ch: u8) Key { 29 pub fn char(ch: u8) Key {
30 return @intToEnum(Key, ch); 30 return @enumFromInt(ch);
31 } 31 }
32 32
33 pub fn shift(k: anytype) Key { 33 pub fn shift(k: anytype) Key {
@@ -52,20 +52,16 @@ pub const Key = enum(u16) {
52 } 52 }
53 53
54 fn mod(comptime modifier: Key, k: Key) Key { 54 fn mod(comptime modifier: Key, k: Key) Key {
55 comptime std.debug.assert( 55 comptime std.debug.assert(modifier == .mod_shft or modifier == .mod_meta or modifier == .mod_ctrl);
56 modifier == .mod_shft
57 or modifier == .mod_meta
58 or modifier == .mod_ctrl
59 );
60 56
61 const shft_int = @enumToInt(Key.mod_shft); 57 const shft_int = @intFromEnum(Key.mod_shft);
62 const meta_int = @enumToInt(Key.mod_meta); 58 const meta_int = @intFromEnum(Key.mod_meta);
63 const ctrl_int = @enumToInt(Key.mod_ctrl); 59 const ctrl_int = @intFromEnum(Key.mod_ctrl);
64 60
65 const max_char_int = @enumToInt(Key.max_char); 61 const max_char_int = @intFromEnum(Key.max_char);
66 62
67 const mod_int = @enumToInt(modifier); 63 const mod_int = @intFromEnum(modifier);
68 const k_int = @enumToInt(k); 64 const k_int = @intFromEnum(k);
69 if (k_int & mod_int == mod_int) { 65 if (k_int & mod_int == mod_int) {
70 return k; 66 return k;
71 } 67 }
@@ -76,12 +72,12 @@ pub const Key = enum(u16) {
76 // Appending S- to a character is not smart 72 // Appending S- to a character is not smart
77 std.debug.assert(modifier != .mod_shft); 73 std.debug.assert(modifier != .mod_shft);
78 return switch (modifier) { 74 return switch (modifier) {
79 .mod_meta => @intToEnum(Key, k_int | meta_int), 75 .mod_meta => @enumFromInt(k_int | meta_int),
80 .mod_ctrl => @intToEnum(Key, k_origmod | (k_nomod & 0x1f)), 76 .mod_ctrl => @enumFromInt(k_origmod | (k_nomod & 0x1f)),
81 else => unreachable, 77 else => unreachable,
82 }; 78 };
83 } else { 79 } else {
84 return @intToEnum(Key, k_int | mod_int); 80 return @enumFromInt(k_int | mod_int);
85 } 81 }
86 } 82 }
87 83
@@ -125,15 +121,15 @@ pub const Key = enum(u16) {
125 options: std.fmt.FormatOptions, 121 options: std.fmt.FormatOptions,
126 writer: anytype, 122 writer: anytype,
127 ) @TypeOf(writer).Error!void { 123 ) @TypeOf(writer).Error!void {
128 const shft_int = @enumToInt(Key.mod_shft); 124 const shft_int = @intFromEnum(Key.mod_shft);
129 const meta_int = @enumToInt(Key.mod_meta); 125 const meta_int = @intFromEnum(Key.mod_meta);
130 const ctrl_int = @enumToInt(Key.mod_ctrl); 126 const ctrl_int = @intFromEnum(Key.mod_ctrl);
131 127
132 const key_int = @enumToInt(key); 128 const key_int = @intFromEnum(key);
133 if (key_int & shft_int == shft_int) { 129 if (key_int & shft_int == shft_int) {
134 try std.fmt.formatBuf("S-", options, writer); 130 try std.fmt.formatBuf("S-", options, writer);
135 return Key.format( 131 return Key.format(
136 @intToEnum(Key, key_int & ~shft_int), 132 @enumFromInt(key_int & ~shft_int),
137 "", 133 "",
138 options, 134 options,
139 writer, 135 writer,
@@ -141,7 +137,7 @@ pub const Key = enum(u16) {
141 } else if (key_int & meta_int == meta_int) { 137 } else if (key_int & meta_int == meta_int) {
142 try std.fmt.formatBuf("M-", options, writer); 138 try std.fmt.formatBuf("M-", options, writer);
143 return Key.format( 139 return Key.format(
144 @intToEnum(Key, key_int & ~meta_int), 140 @enumFromInt(key_int & ~meta_int),
145 "", 141 "",
146 options, 142 options,
147 writer, 143 writer,
@@ -149,22 +145,17 @@ pub const Key = enum(u16) {
149 } else if (key_int & ctrl_int == ctrl_int) { 145 } else if (key_int & ctrl_int == ctrl_int) {
150 try std.fmt.formatBuf("C-", options, writer); 146 try std.fmt.formatBuf("C-", options, writer);
151 return Key.format( 147 return Key.format(
152 @intToEnum(Key, key_int & ~ctrl_int), 148 @enumFromInt(key_int & ~ctrl_int),
153 "", 149 "",
154 options, 150 options,
155 writer, 151 writer,
156 ); 152 );
157 } else if (key_int < 0x20) { 153 } else if (key_int < 0x20) {
158 try std.fmt.formatBuf("C-", options, writer); 154 try std.fmt.formatBuf("C-", options, writer);
159 return Key.format( 155 return Key.format(Key.char(@intCast(key_int + 0x40)), "", options, writer);
160 Key.char(@intCast(u8, key_int + 0x40)),
161 "",
162 options,
163 writer
164 );
165 } else if (key_int < 0x100) { 156 } else if (key_int < 0x100) {
166 const ch = @intCast(u8, key_int); 157 const ch: u8 = @intCast(key_int);
167 if (std.ascii.isGraph(ch)) { 158 if (std.ascii.isPrint(ch)) {
168 return writer.writeByte(ch); 159 return writer.writeByte(ch);
169 } else { 160 } else {
170 try writer.writeAll("<\\x"); 161 try writer.writeAll("<\\x");