diff options
| -rw-r--r-- | TUI/module.jai | 42 | ||||
| -rw-r--r-- | TUI/unix.jai | 66 | ||||
| -rw-r--r-- | ttt.jai | 14 |
3 files changed, 70 insertions, 52 deletions
diff --git a/TUI/module.jai b/TUI/module.jai index 18df2c5..332f7c5 100644 --- a/TUI/module.jai +++ b/TUI/module.jai @@ -86,24 +86,30 @@ initialized := false; input_buffer : [64] u8; input_string : string; +input_override : Key; -Key :: u8; // TODO To be improved. -Keys :: enum u8 { - None :: 0; - Resize :: 1; //410; // TODO Why?! -} +// TODO WIP WIP WIP +// >>>>>>>>>>>>>>>>> Decide what we'll use as Key... and start fixing the types used on the procedures. +; +Key :: u32; -dam :: (msg: string) { - print(msg, to_standard_error = true); +Keys :: enum Key { + None :: 0; + Resize :: 0xFF000001; // TODO Using a value incompatible with UTF-8. } -push_key :: (key: Key) { - // TODO +set_next_key :: (key: Key) { + input_override = key; } get_key :: (timeout_milliseconds: s32 = -1) -> Key { + if input_override != xx Keys.None { + defer input_override = xx Keys.None; + return input_override; + } + if OS_was_terminal_resized() return xx Keys.Resize; if input_string.count > 0 { @@ -127,10 +133,10 @@ get_key :: (timeout_milliseconds: s32 = -1) -> Key { return xx Keys.None; } -get_str :: (str_limit: int = -1, allocator: Allocator = temp) -> string { +get_str :: (count_limit: int = -1, allocator: Allocator = temp) -> string { assert(allocator.proc != null, "Argument 'allocator.proc' has invalid null value."); - - if str_limit < 0 { + + if count_limit < 0 { builder: String_Builder(); builder.allocator = allocator; init_string_builder(*builder); @@ -145,22 +151,21 @@ get_str :: (str_limit: int = -1, allocator: Allocator = temp) -> string { return builder_to_string(*builder, allocator); } else { - buffer := alloc_string(str_limit, allocator); - buffer.count = OS_read_input(buffer.data, str_limit); + buffer := alloc_string(count_limit, allocator); + buffer.count = OS_read_input(buffer.data, count_limit); return buffer; } } - - start :: () { if initialized == true return; write_strings(Commands.HideCursor, Commands.SaveCursorPosition, Commands.EnterAlternateBuffer, Commands.SetUTF8); OS_prepare_terminal(); - input_string.data = input_buffer.data; - input_string.count = 0; + input_string.data = input_buffer.data; + input_string.count = 0; + input_override = xx Keys.None; initialized = true; } @@ -259,6 +264,7 @@ get_terminal_size :: () -> rows: int, columns: int { } set_cursor_position :: (row: int, column: int) { + assert(initialized, "TUI is not ready."); auto_release_temp(); tmp_string := tprint(Commands.SetCursorPosition, row, column); write_string(tmp_string); diff --git a/TUI/unix.jai b/TUI/unix.jai index 7ff1e72..fb79f86 100644 --- a/TUI/unix.jai +++ b/TUI/unix.jai @@ -133,6 +133,39 @@ initial_tio_mode: Terminal_IO_Mode; raw_tio_mode: Terminal_IO_Mode; +//////////////////////////////////////////////////////////////////////////////// +// Resize detection +resize_mutex : Mutex; +was_resized : bool; + +resize_handler :: (signal_code : s32) #c_call { + new_context : Context; + push_context new_context { + print("SIGNAL:%", signal_code); + if signal_code != SIGWINCH then return; + if was_resized == true then return; + print("RESIZE\n"); + lock(*resize_mutex); + defer unlock(*resize_mutex); + was_resized = true; + } +} + +prepare_resize_handler :: () { + sa : sigaction_t; + sa.sa_handler = resize_handler; + sigemptyset(*(sa.sa_mask)); + sa.sa_flags = SA_RESTART; + sigaction(SIGWINCH, *sa, null); +} + +restore_resize_handler :: () { + sa : sigaction_t; + sa.sa_handler = SIG_DFL; + sigaction(SIGWINCH, null, *sa); +} + +//////////////////////////////////////////////////////////////////////////////// #scope_export @@ -154,9 +187,8 @@ OS_prepare_terminal :: () { } OS_reset_terminal :: () { - tcsetattr(STDIN_FILENO, 0, *initial_tio_mode); // TODO Log on error. - restore_resize_handler(); + tcsetattr(STDIN_FILENO, 0, *initial_tio_mode); // TODO Log on error. } OS_read_input :: (buffer: *u8, bytes_to_read: s64) -> bytes_read: s64, error: bool = false, error_message: string = "" { @@ -176,39 +208,9 @@ OS_wait_for_input :: (timeout_milliseconds: s32) -> is_input_available: bool { return ifx poll_return > 0 then true else false; } -resize_mutex : Mutex; -was_resized : bool; - OS_was_terminal_resized :: () -> bool { lock(*resize_mutex); defer unlock(*resize_mutex); defer was_resized = false; return was_resized; } - -resize_handler :: (signal_code : s32) #c_call { - new_context : Context; - push_context new_context { - print("SIGNAL:%", signal_code); - if signal_code != SIGWINCH then return; - if was_resized == true then return; - print("RESIZE\n"); - lock(*resize_mutex); - defer unlock(*resize_mutex); - was_resized = true; - } -} - -prepare_resize_handler :: () { - sa : sigaction_t; - sa.sa_handler = resize_handler; - sigemptyset(*(sa.sa_mask)); - sa.sa_flags = SA_RESTART; - sigaction(SIGWINCH, *sa, null); -} - -restore_resize_handler :: () { - sa : sigaction_t; - sa.sa_handler = SIG_DFL; - sigaction(SIGWINCH, null, *sa); -} @@ -1259,7 +1259,8 @@ main :: () { xcolumns, xrows: int; key: TUI.Key = #char "d"; while(key != #char "q") { - __mark := get_temporary_storage_mark(); + // __mark := get_temporary_storage_mark(); + TUI.set_cursor_position(3, 3); write_string("dam "); print("%:%", xcolumns, xrows); @@ -1273,8 +1274,17 @@ main :: () { xrows, xcolumns = TUI.get_terminal_size(); TUI.draw_box(1, 1, xcolumns, xrows); } + else if key == #char "i" { + auto_release_temp(); + TUI.set_cursor_position(7, 3); + write_string("input: "); + TUI.flush_input(); + str := TUI.get_str(3); + print(">%<\n\r", str); + TUI.set_cursor_position(8, 3); + } else { - print_character(key); + print_character(cast(u8)key); } } TUI.stop(); |
