diff options
| author | dam <dam@gudinoff> | 2023-12-15 02:02:33 +0000 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2023-12-15 02:02:33 +0000 |
| commit | 8d06729eb3353705f5d35457c5f921407f8c0f4a (patch) | |
| tree | a7fb3d6a25e7948e7556aa61be3913c3bf584890 | |
| parent | ef69c4f8daa14a580dd8bb442cae551f08711ca2 (diff) | |
| download | task-time-tracker-8d06729eb3353705f5d35457c5f921407f8c0f4a.tar.zst task-time-tracker-8d06729eb3353705f5d35457c5f921407f8c0f4a.zip | |
WIP: Trying to define Key.
| -rw-r--r-- | TUI/module.jai | 61 | ||||
| -rw-r--r-- | ttt.jai | 60 |
2 files changed, 56 insertions, 65 deletions
diff --git a/TUI/module.jai b/TUI/module.jai index bfed510..48fca16 100644 --- a/TUI/module.jai +++ b/TUI/module.jai @@ -86,14 +86,65 @@ Commands :: struct { // TODO Maybe make the OS_* procedures as inline?! -// Let's return keys with 32bits so we can merge up to 4bytes and support UTF-8 encoding. -Key :: u32; +// We wanted the Key type to represent either UTF-8 encoded characters and also keyboard keys. +// The UTF-8 only requires up to 4 bytes, but some keyboard keys return up to 6 bytes. +// Therefore, we rounded it up to 8 bytes to support all this and more (if needed). + +Key :: union { + text: [8] u8; + code: u64; +} + +to_key :: inline (value: $T) -> Key #modify { return T == u8 || T == ([]u8) || T == string; } { + k: Key; + #if T == u8 { + k.text[0] = value; + } + #if T == ([]u8) || T == string { + size := ifx value.count > Key.text.count then Key.text.count else value.count; + // assert(size <= Key.text.count); This is now under the user responsability. + memcpy(k.text.data, value.data, size); + } + return k; +} + +operator == :: inline (a: Key, b: Key) -> bool { + return a.code == b.code; +} + +operator == :: inline (a: Key, b: $T) -> bool { + k := to_key(b); + return a.code == k.code; +} + +#run { + print("\n:::%\n", Key.text.count); + // ti := type_info(Key); + print("\n---\n%\n---\n", type_info(Key).*); + a: Key; + b: u8; + print(">%\n", a == to_key(b)); + + c: string = ""; + print(">%\n", a == to_key(c)); + + d: []u8 = xx ""; + print(">%\n", a == to_key(d)); + + print(">%\n", a == b); + print(">%\n", a == c); + print(">%\n", a == d); +} // Terminal action codes are encoded with values incompatible with UTF-8 to avoid collisions. -Keys :: enum Key { - None :: 0xFF000000; - Resize :: 0xFF000001; +Keys :: struct { + None :: #run to_key("\0"); + Resize :: #run to_key(xx 1); } +// Keys :: enum Key { + // None :: 0xFF000000; + // Resize :: 0xFF000001; +// } initialized := false; @@ -1185,66 +1185,6 @@ main :: () { // TODO Test input - Key :: union { - text: [4] u8; - code: u64; - } - - // Instead of checking if it's an array or string... check the type is array of u8. - to_key :: inline (value: $T) -> Key #modify { return T == u8 || T == string || (T == []u8); } { - k: Key; - #if T == u8 { - k.text[0] = value; - } - #if T == string || (T == []u8) { - assert(value.count <= 4); - for 0..value.count-1 k.text[it] = value[it]; - } - return k; - } - - operator == :: inline (a: Key, b: Key) -> bool { - return a.code == b.code; - } - - // Instead of checking if it's an array or string... check the type is array of u8. - // We'll need to use "Type_Info" and "Type_Info_Array" - // and cast T using `type_info := cast(*Type_Info)T;` - operator == :: inline (a: Key, b: $T) -> bool - #modify { - if T == u8 { - return true; - } - ti := cast(*Type_Info)T; - if ti.type == .ARRAY { - ti_array := cast(*Type_Info_Array)T; - // tia_type := ti_array.element_type.type; - return ti_array.element_type.type == .INTEGER && ti_array.element_type.runtime_size == 1; - } - return false; - // return T == u8 || T == string || (T == []u8); } { - } { - // operator == :: inline (a: Key, b: $T) -> bool #modify { return (T == [4]u8); } { - k := to_key(b); - return a == k; - } - - // k := K. {xx "01"}; - // k := to_key(xx "\0\0\01"); - // k := to_key(xx "€"); - // k := to_key(xx "A"); - k := to_key("A"); - // a: u8 = #char "A"; - a: [4]u8; - a[0] = #char "A"; - print("Is equal to 'A': >%<\n", k == a); - // b: u8 = 3; - // print("Is equal to 'A': >%<\n", k == #char); - print("Text :>%<\n", cast(string)k.text, to_standard_error = true); - print("Key :>%<\n", FormatInt.{value=k.code, base=16, minimum_digits=8}, to_standard_error = true); - return; - - if 1 { print("TEST : set and get cursor position --\n", to_standard_error = true); TUI.start(); |
