aboutsummaryrefslogtreecommitdiff
path: root/libtui/keys.ha
blob: a9955ba74a53cabb2c3e5bab374a0ff72b16b5bb (plain)
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
// 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;
};