diff options
| author | dam <dam@gudinoff> | 2024-05-03 01:00:54 +0100 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2024-05-03 01:00:54 +0100 |
| commit | 6b90a43d69142ff684b792ba2da951e5bbddc8a6 (patch) | |
| tree | 80e97de313aeaa6259075eb4a049eebc29a07c1c /modules/TUI/module.jai | |
| parent | 8b312bad33617f9b718232141d2855d80cb8b912 (diff) | |
| download | task-time-tracker-6b90a43d69142ff684b792ba2da951e5bbddc8a6.tar.zst task-time-tracker-6b90a43d69142ff684b792ba2da951e5bbddc8a6.zip | |
WIP : Cleanup read_input_line on TUI module.
Diffstat (limited to 'modules/TUI/module.jai')
| -rw-r--r-- | modules/TUI/module.jai | 50 |
1 files changed, 29 insertions, 21 deletions
diff --git a/modules/TUI/module.jai b/modules/TUI/module.jai index d44edab..464b0d9 100644 --- a/modules/TUI/module.jai +++ b/modules/TUI/module.jai @@ -19,6 +19,8 @@ #import "UTF8"; #load "key_map.jai"; +KEY_SIZE :: #run type_info(Key).runtime_size; + #run { assert(COLOR_MODE_BITS == 4 || COLOR_MODE_BITS == 8 || COLOR_MODE_BITS == 24, "Invalid COLOR_MODE_BITS. Valid values are 4, 8, or 24 (default)."); assert(input_buffer.count >= KEY_SIZE, "The input buffer size must be capable to hold an entire Key, which should be able to hold an UTF8 code (4 bytes) or a terminal escape code (6 bytes)."); @@ -213,8 +215,6 @@ using_style :: (style: Style) #expand { Key :: u64; // Terminal key-codes have 1 to 6 bytes so we'll use 8 bytes. -KEY_SIZE :: #run type_info(Key).runtime_size; - to_key :: inline (str: $T) -> Key #modify { return T == ([]u8) || T == string; } { k: Key; // #if DEBUG { @@ -304,7 +304,7 @@ assert_is_active :: inline () { // #scope_export TODO Setup the scope_export and scope_file -set_next_key :: (key: Key) { +set_next_key :: inline (key: Key) { assert_is_active(); input_override = key; } @@ -432,42 +432,50 @@ read_input :: (count_limit: int = -1, terminators: .. u8) -> string, success := } } +// Uses the get_key to read user input and show it on screen. +// Allows to move the cursor left and right and to delete/backspace. +// Enter ends the input, returning the input string and the Enter key. +// Escape discards the input returning an empty string and a Escape key. +// Resize discards the input returning an empty string and a Resize key. read_input_line :: (count_limit: int, is_visible: bool = true) -> string, Key { - /* - Uses the get_key to read user input and show it on screen. - Allows to move the cursor left and right and to delete/backspace. - Enter ends the input, returning the input string and the Enter key. - Escape discards the input returning an empty string and a Escape key. - Resize discards the input returning an empty string and a Resize key. - */ assert_is_active(); - assert(count_limit >= 0, "Invalid value on count_limit parameter."); // TODO Too agressive + // 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; + // } + + builder := String_Builder.{ allocator = temporary_allocator }; + str := alloc_string(count_limit); str.count = 0; idx := 0; x, y := get_cursor_position(); - write_strings(Commands.ShowCursor, Commands.StartBlinking, Commands.BlinkingBarShape); - key := Keys.None; + + write_strings(Commands.ShowCursor, Commands.StartBlinking, Commands.BlinkingBarShape); + while true { auto_release_temp(); chars_count := count_characters(str); - // Preview input. TODO Optimize using temp_builder + // Draw preview. if is_visible { - set_cursor_position(x, y); - write_string(str); - for chars_count..count_limit-1 print_character(#char " "); + append(*builder, tprint(Commands.SetCursorPosition, y, x)); + append(*builder, str); + for chars_count..count_limit-1 append(*builder, " "); } else { - set_cursor_position(x, y); - for 1..chars_count print_character(#char "*"); - for chars_count..count_limit-1 print_character(#char " "); + append(*builder, tprint(Commands.SetCursorPosition, y, x)); + for 1..chars_count append(*builder, "*"); + for chars_count..count_limit-1 append(*builder, " "); } - set_cursor_position(x+idx, y); + append(*builder, tprint(Commands.SetCursorPosition, y, x+idx)); + write_string(builder_to_string(*builder)); // Process input key. key = get_key(); |
