aboutsummaryrefslogtreecommitdiff
path: root/modules/TUI/module.jai
diff options
context:
space:
mode:
authordam <dam@gudinoff>2024-05-04 01:43:34 +0100
committerdam <dam@gudinoff>2024-05-04 01:43:34 +0100
commit675b0a5fea60dc33a97b0dc1871bb9a0b61818cc (patch)
tree0d663361d843a878114d496bc4e9817c2e9da7f4 /modules/TUI/module.jai
parent6b90a43d69142ff684b792ba2da951e5bbddc8a6 (diff)
downloadtask-time-tracker-675b0a5fea60dc33a97b0dc1871bb9a0b61818cc.tar.zst
task-time-tracker-675b0a5fea60dc33a97b0dc1871bb9a0b61818cc.zip
WIP : Cleanup TUI module. Finally decided to go with hard-asserts (vs soft-errors/logs).
Diffstat (limited to 'modules/TUI/module.jai')
-rw-r--r--modules/TUI/module.jai70
1 files changed, 31 insertions, 39 deletions
diff --git a/modules/TUI/module.jai b/modules/TUI/module.jai
index 464b0d9..a6a3d11 100644
--- a/modules/TUI/module.jai
+++ b/modules/TUI/module.jai
@@ -120,6 +120,9 @@ Commands :: struct #type_info_none {
CursorNormalMode :: "\e[?1l";
}
+// TODO Check which procedures need the assert_is_active call.
+// TODO Review the error messages on the asserts.
+
#if COLOR_MODE_BITS == 4 {
#load "palette_4b.jai";
@@ -189,15 +192,15 @@ set_font_style :: inline (bold: bool, underline: bool = false, strike_through: b
set_style :: (style: Style) {
set_font_style(style.bold, style.underline, style.strike_through, style.negative);
set_colors(style.foreground, style.background);
- context.tui_style = style;
+ context.terminal_style = style;
}
-clear_style :: () {
+clear_style :: inline () {
write_string(#run sprint(Commands.SetGraphicsRendition, "0"));
}
using_style :: (style: Style) #expand {
- __style := context.tui_style;
+ __style := context.terminal_style;
set_style(style);
`defer set_style(__style);
}
@@ -215,24 +218,23 @@ using_style :: (style: Style) #expand {
Key :: u64; // Terminal key-codes have 1 to 6 bytes so we'll use 8 bytes.
-to_key :: inline (str: $T) -> Key #modify { return T == ([]u8) || T == string; } {
+to_key :: (str: $T) -> Key #modify { return T == ([]u8) || T == string; } {
+ assert(str.count <= KEY_SIZE, "Invalid argument passed to to_key(): 'str.count' must be less-than or equal to %, but it was %.", KEY_SIZE, str.count);
+
k: Key;
- // #if DEBUG {
- // assert(str.count <= 8); // TODO Add DEBUG to module parameters.
- // }
- for 0..str.count-1 #no_abc {
+ for 0..str.count-1 {
k |= ((cast(u64)str[it]) << (it*8));
}
return k;
}
-to_string :: inline (key: Key) -> string { // TODO FIXME TEMPORARY MEMORY
- str := talloc_string(KEY_SIZE);
+to_string :: (key: Key) -> string {
+ str := alloc_string(KEY_SIZE);
str.count = 0;
- while key != 0 #no_abc {
- str[str.count] = xx key & 0xFF;
- key >>= 8;
+ while key != 0 {
str.count += 1;
+ str[str.count-1] = xx key & 0xFF;
+ key >>= 8;
}
return str;
}
@@ -280,7 +282,7 @@ Keys :: struct #type_info_none {
F12 : Key : #run to_key("#f12");
}
-#add_context tui_style: Style;
+#add_context terminal_style: Style;
active := false;
@@ -297,7 +299,7 @@ module_logger :: (message: string, data: *void, info: Log_Info) {
}
assert_is_active :: inline () {
- assert(active, "Please call TUI.setup_terminal() before using this procedure."); // TODO Improve error message... and maybe use the logger and return success=false flag
+ assert(active, "Please call setup_terminal() before using the module procedures.");
}
////////////////////////////////////////////////////////////////////////////////
@@ -309,6 +311,7 @@ set_next_key :: inline (key: Key) {
input_override = key;
}
+// TODO Provide some documentation comments.
get_key :: (timeout_milliseconds: s32 = -1) -> Key {
assert_is_active();
@@ -377,15 +380,15 @@ get_key :: (timeout_milliseconds: s32 = -1) -> Key {
return to_key(to_parse);
}
-// TODO Review me and add some comments.
-read_input :: (count_limit: int = -1, terminators: .. u8) -> string, success := true {
+// If count_limit has a non-negative value it will be used as the limit to the number of bytes on the returned string.
+// If any characters are provided in the terminators list, they will be used to scan and interrupt the input, including
+// the terminator as the last character.
+// At least one of the arguments must be properly setup to avoid an infinite-loop reading the input.
+read_input :: (count_limit: int = -1, terminators: .. u8) -> string {
assert_is_active();
-
- if count_limit < 0 && terminators.count <= 0 {
- log_error("Invalid arguments passed to read_input(): (%).\n", count_limit); // TODO Improve error message.
- return "", false;
- }
-
+ assert(count_limit >= 0 || terminators.count > 0, "Invalid arguments passed to read_input() will result in infinite-loop.");
+
+ // TODO Provide some documentation comments.
if count_limit < 0 {
builder: String_Builder;
init_string_builder(*builder);
@@ -439,13 +442,7 @@ read_input :: (count_limit: int = -1, terminators: .. u8) -> string, success :=
// Resize discards the input returning an empty string and a Resize key.
read_input_line :: (count_limit: int, is_visible: bool = true) -> string, Key {
assert_is_active();
-
- // TODO If we pass success... then, does it make sense to return success=true on resize?
- assert(count_limit >= 0, "Invalid value passed to count_limit.");
- // if count_limit < 0 {
- // log_error("Invalid arguments passed to read_input_line(): (%, %).\n", count_limit, is_visible);
- // return "", Keys.None, false;
- // }
+ assert(count_limit >= 0, "Invalid value passed to count_limit(): %.", count_limit);
builder := String_Builder.{ allocator = temporary_allocator };
@@ -510,7 +507,7 @@ read_input_line :: (count_limit: int, is_visible: bool = true) -> string, Key {
if is_escape_code(key) continue;
buff_idx := map_character_to_buffer_idx(str, idx);
- key_str := to_string(key);
+ key_str := to_string(key,, allocator = temporary_allocator);
// Make sure we have space to append the new character at the end (in case we're trying to do it).
if buff_idx > count_limit - key_str.count then continue;
@@ -538,7 +535,7 @@ read_input_line :: (count_limit: int, is_visible: bool = true) -> string, Key {
setup_terminal :: () -> success := true #must {
if active == true return;
-
+
input_string.data = input_buffer.data;
input_string.count = 0;
input_override = xx Keys.None;
@@ -591,14 +588,9 @@ flush_input :: () {
input_string.count = 0;
}
-// TODO What if we return success and fail when input arguments are invalid?
draw_box :: (x: int, y: int, width: int, height: int) {
- assert_is_active(); //TODO NOT NEEDED
-
- if x <= 0 || y <= 0 || width <= 1 || height <= 1 {
- log_error("Invalid arguments passed to draw_box(): (%, %, %, %).\n", x, y, width, height); // TODO Improve error message.
- return;
- }
+ assert_is_active();
+ assert(x > 0 && y > 0 && width > 1 && height > 1, "Invalid arguments passed to draw_box(): (%, %, %, %).\n", x, y, width, height);
auto_release_temp();