aboutsummaryrefslogtreecommitdiff
path: root/libtui/keys.ha
diff options
context:
space:
mode:
Diffstat (limited to 'libtui/keys.ha')
-rw-r--r--libtui/keys.ha43
1 files changed, 43 insertions, 0 deletions
diff --git a/libtui/keys.ha b/libtui/keys.ha
new file mode 100644
index 0000000..a9955ba
--- /dev/null
+++ b/libtui/keys.ha
@@ -0,0 +1,43 @@
+// License: MPL-2.0
+// (c) 2022 Julian Hurst <ark@mansus.space>
+
+use encoding::utf8;
+
+type keycode = (specialkey, []u8);
+//const BACKSPACECODE: keycode = (specialkey::BACKSPACE, [127]);
+//const RIGHTCODE: keycode = (specialkey::RIGHT, [27, 91, 67]);
+const keycodes: []keycode = [
+ (specialkey::BACKSPACE, [127]),
+ (specialkey::RIGHT, [27, 91, 67]),
+];
+
+export type specialkey = enum {
+ BACKSPACE,
+ RIGHT,
+};
+
+fn iskey(r: rune, key: keycode) bool = {
+ const u = utf8::encoderune(r);
+ for (let i = 0z; i < len(u); i += 1) {
+ if (u[i] != key.1[i]) {
+ return false;
+ };
+ };
+ return true;
+};
+
+export fn getkey(r: rune) (specialkey | rune) = {
+ const u = utf8::encoderune(r);
+ for (let i = 0z; i < len(keycodes); i += 1) {
+ let b = true;
+ for (let j = 0z; j < len(u) && b; j += 1) {
+ if (len(keycodes[i].1) != len(u) || keycodes[i].1[j] != u[j]) {
+ b = false;
+ };
+ };
+ if (b) {
+ return keycodes[i].0;
+ };
+ };
+ return r;
+};