From 675b0a5fea60dc33a97b0dc1871bb9a0b61818cc Mon Sep 17 00:00:00 2001 From: dam Date: Sat, 4 May 2024 01:43:34 +0100 Subject: WIP : Cleanup TUI module. Finally decided to go with hard-asserts (vs soft-errors/logs). --- ttt.jai | 255 +--------------------------------------------------------------- 1 file changed, 3 insertions(+), 252 deletions(-) (limited to 'ttt.jai') diff --git a/ttt.jai b/ttt.jai index 3512d20..d471661 100644 --- a/ttt.jai +++ b/ttt.jai @@ -17,8 +17,7 @@ // - release : jai ttt.jai -quiet -x64 -release // - debug : jai ttt.jai -quiet -x64 -// #import "Basic"()(MEMORY_DEBUGGER=true); // TODO Remove after final debug sessions. This takes up ~30MB of RAM. -#import "Basic"; +#import "Basic"()(MEMORY_DEBUGGER=true); // TODO Remove after final debug sessions. This takes up ~30MB of RAM. #import "System"; #import "Sort"; #import "Math"; @@ -1107,7 +1106,7 @@ read_input_string :: (x: int, y: int, input_limit: int, padding: int = 0) -> val write_string(TUI.Commands.TextMode); TUI.set_cursor_position(x, y); - style_input := context.tui_style; + style_input := context.terminal_style; style_input.underline = true; TUI.using_style(style_input); @@ -1158,256 +1157,8 @@ prompt_user_key :: (y: int, message: string) -> TUI.Key { } main :: () { - - // -- -- -- Testing TUI -- START - - perform_test := false; - - assert_result :: (result: bool, error_message: string) { - if result == true { - print("- success\n", to_standard_error = true); - } - else { - assert(TUI.reset_terminal(), "Failed to reset TUI."); - print("- ERROR: %", error_message, to_standard_error = true); - exit(1); - } - } - - next_line :: inline () { - x, y := TUI.get_cursor_position(); - TUI.set_cursor_position(1, y+1); - } - - if perform_test && 1 { - print("TEST : set and get cursor position\n", to_standard_error = true); - assert(TUI.setup_terminal(), "Failed to setup TUI."); - X :: 2; - Y :: 3; - TUI.set_cursor_position(X, Y); - x, y := TUI.get_cursor_position(); - assert(TUI.reset_terminal(), "Failed to reset TUI."); - assert_result(x == X && y == Y, "Failed set/get cursor position.\n"); - } - - if perform_test && 1 { - print("TEST : module logger\n", to_standard_error = true); - log("- log: before module start."); - assert(TUI.setup_terminal(), "Failed to setup TUI."); - TUI.set_cursor_position(3, 3); - print("wait"); - sleep_milliseconds(1000); - log("- log: while module is active."); - sleep_milliseconds(1000); - print(" a bit"); - sleep_milliseconds(1000); - assert(TUI.reset_terminal(), "Failed to reset TUI."); - log("- log: after module stop."); - } - - if perform_test && 1 { - print("TEST : test key input\n", to_standard_error = true); - auto_release_temp(); - assert(TUI.setup_terminal(), "Failed to setup TUI."); - TUI.clear_terminal(); - TUI.set_cursor_position(1, 1); - write_string("Press q to exit, other key to print it to screen, wait 1s to see animation."); - next_line(); - key: TUI.Key; - while(key != #char "q") { - key = TUI.get_key(1000); - if key == TUI.Keys.None { - write_string("-"); - } - else if key == TUI.Keys.Resize { - write_string("#"); - } - else { - // else if key >= 32 && key <= 128 then print_character(cast,force(u8)key) - write_string(TUI.to_string(key)); - } - } - assert(TUI.reset_terminal(), "Failed to reset TUI."); - print("- success\n", to_standard_error = true); - } - - if perform_test && 1 { - print("TEST : draw box\n", to_standard_error = true); - auto_release_temp(); - assert(TUI.setup_terminal(), "Failed to setup TUI."); - TUI.flush_input(); - TUI.clear_terminal(); - TUI.draw_box(1, 2, 5, 3); - TUI.set_cursor_position(1, 1); - print("Can you see the box below? (y/n)"); - key := TUI.get_key(); - assert(TUI.reset_terminal(), "Failed to reset TUI."); - assert_result(key == #char "y", "Failed to draw box.\n"); - } - - if perform_test && 1 { - print("TEST : get terminal size\n", to_standard_error = true); - auto_release_temp(); - assert(TUI.setup_terminal(), "Failed to setup TUI."); - TUI.clear_terminal(); - width, height := TUI.get_terminal_size(); - TUI.set_cursor_position(1, 1); - print("Is terminal size %x%? (y/n)", width, height); - key: TUI.Key = xx TUI.Keys.None; - while (key == xx TUI.Keys.None || key == xx TUI.Keys.Resize) { - key = TUI.get_key(); - } - assert(TUI.reset_terminal(), "Failed to reset TUI."); - assert_result(key == #char "y", "Failed to get terminal size.\n"); - } - - if perform_test && 1 { - print("TEST : set terminal title\n", to_standard_error = true); - assert(TUI.setup_terminal(), "Failed to setup TUI."); - title := "BAZINGA"; - TUI.set_terminal_title(title); - TUI.set_cursor_position(1, 1); - print("Is terminal title '%'? (y/n)", title); - key: TUI.Key = xx TUI.Keys.None; - while (key == xx TUI.Keys.None || key == xx TUI.Keys.Resize) { - key = TUI.get_key(); - } - assert(TUI.reset_terminal(), "Failed to reset TUI."); - assert_result(key == #char "y", "Failed to set terminal title.\n"); - } - - if perform_test && 1 { - print("TEST : print keys and set terminal title\n", to_standard_error = true); - auto_release_temp(); - assert(TUI.setup_terminal(), "Failed to setup TUI."); - TUI.set_terminal_title("bazinga"); - key: TUI.Key = #char "d"; - last_none_char := "X"; - - width, height := TUI.get_terminal_size(); - TUI.clear_terminal(); - TUI.draw_box(1, 1, width, height); - drop_down := 0; - - while(key != #char "q") { - - if key == { - case TUI.Keys.None; { - TUI.set_cursor_position(2, 2); - last_none_char = ifx last_none_char == "X" then "+" else "X"; - write_string(last_none_char); - } - - case TUI.Keys.Resize; #through; - case #char "c"; { - width, height = TUI.get_terminal_size(); - TUI.clear_terminal(); - TUI.draw_box(1, 1, width, height); - drop_down = 0; - } - - case; { - TUI.set_cursor_position(2, 3+drop_down); - str := TUI.to_string(key); - array_to_print: [..] string; - for 0..str.count-1 { - tmp := tprint("%", FormatInt.{value = cast(u8)str[it], base=16},, temporary_allocator); - array_add(*array_to_print, tmp); - } - string_to_print := join(..array_to_print, separator = " "); - print(": % : ", string_to_print); - for 0..str.count-1 { - if str[it] == #char "\e" { - str[it] = #char "#"; - } - } - write_string(str); - write_string(" :"); - drop_down += 1; - } - } - - x := ifx width > 24 then width-24 else 1; - y := ifx height > 1 then height-1 else 1; - - TUI.set_cursor_position(x, y); - print("size = %x%\n", width, height); - key = TUI.get_key(1000); - - // __mark := get_temporary_storage_mark(); - // set_temporary_storage_mark(__mark); - } - print("- success"); - assert(TUI.reset_terminal(), "Failed to reset TUI."); - } - - if perform_test && 1 { - print("TEST : user input\n", to_standard_error = true); - auto_release_temp(); - assert(TUI.setup_terminal(), "Failed to setup TUI."); - TUI.clear_terminal(); - TUI.set_cursor_position(1, 1); - print("Enter some text (use Enter to finish, Esc to cancel, or resize to abort):"); - next_line(); - str, key := TUI.read_input_line(15); - TUI.set_cursor_position(1, 3); - error_message: string; - if key == { - case TUI.Keys.Escape; { - print("Have you pressed Esc? (y/n)"); - error_message = "Failed to read line on Esc."; - } - - case TUI.Keys.Resize; { - print("Have you resized the terminal? (y/n)"); - error_message = "Failed to read line on resize."; - - } - case; { - print("Have you entered '%'? (y/n)", str); - error_message = "Failed to read line."; - } - } - answer := TUI.get_key(); - assert(TUI.reset_terminal(), "Failed to reset TUI."); - assert_result(answer == #char "y", error_message); - } - - if perform_test && 1 { - print("TEST : hidden user input\n", to_standard_error = true); - auto_release_temp(); - assert(TUI.setup_terminal(), "Failed to setup TUI."); - TUI.clear_terminal(); - TUI.set_cursor_position(1, 1); - print("Enter some secret (use Enter to finish, Esc to cancel, or resize to abort):"); - next_line(); - str, key := TUI.read_input_line(15, false); - TUI.set_cursor_position(1, 3); - error_message: string; - if key == { - case TUI.Keys.Escape; { - print("Have you pressed Esc? (y/n)"); - error_message = "Failed to read line on Esc."; - } - - case TUI.Keys.Resize; { - print("Have you resized the terminal? (y/n)"); - error_message = "Failed to read line on resize."; - } - case; { - print("Have you entered '%'? (y/n)", str); - error_message = "Failed to read line."; - } - } - answer := TUI.get_key(); - assert(TUI.reset_terminal(), "Failed to reset TUI."); - assert_result(answer == #char "y", error_message); - } - - // -- -- -- Testing TUI -- STOP - - // defer report_memory_leaks(); // TODO Remove after final debug sessions. + defer report_memory_leaks(); // TODO Remove after final debug sessions. defer free_memory(); -- cgit v1.2.3