diff options
| -rw-r--r-- | modules/TUI/module.jai | 40 | ||||
| -rw-r--r-- | ttt.jai | 29 |
2 files changed, 39 insertions, 30 deletions
diff --git a/modules/TUI/module.jai b/modules/TUI/module.jai index 5399503..70675c7 100644 --- a/modules/TUI/module.jai +++ b/modules/TUI/module.jai @@ -33,7 +33,7 @@ #load "key_map.jai"; #add_context tui_style : Style; // This contains the last style applied by the module. -#add_context tui_builder : *String_Builder; // If set, this will serve as an output buffer for this module procedures. +#add_context tui_output_builder : *String_Builder; // If set, this will serve as an output buffer for this module procedures. KEY_SIZE :: #run type_info(Key).runtime_size; #assert(input_buffer.count >= KEY_SIZE); // The input buffer size must be capable to hold an entire Key. @@ -176,10 +176,10 @@ Style :: struct { } set_style :: (style: Style) { - // If no tui_builder is provided, use a temporary one and discard it afterwards. - builder := context.tui_builder; + // If no tui_output_builder is provided, use a temporary one and discard it afterwards. + builder := context.tui_output_builder; temp_mark: Temporary_Storage_State = ---; - if context.tui_builder == null { + if context.tui_output_builder == null { builder = *temp_builder; temp_mark = get_temporary_storage_mark(); } @@ -213,7 +213,7 @@ set_style :: (style: Style) { append(builder, #run sprint(Commands.SetGraphicsRendition, "49")); } - if context.tui_builder == null { + if context.tui_output_builder == null { write_builder(builder); set_temporary_storage_mark(temp_mark); } @@ -606,10 +606,10 @@ draw_box :: (x: int, y: int, width: int, height: int) { assert_is_active(); assert(x > 0 && y > 0 && width > 1 && height > 1, "Invalid arguments passed to draw_box(): 'x' and 'y' must be greater-than 0; 'width' and 'height' must be greater-than 1."); - // If no tui_builder is provided, use a temporary one and discard it afterwards. - builder := context.tui_builder; + // If no tui_output_builder is provided, use a temporary one and discard it afterwards. + builder := context.tui_output_builder; temp_mark: Temporary_Storage_State = ---; - if context.tui_builder == null { + if context.tui_output_builder == null { builder = *temp_builder; temp_mark = get_temporary_storage_mark(); } @@ -642,7 +642,7 @@ draw_box :: (x: int, y: int, width: int, height: int) { append(builder, Commands.TextMode); - if context.tui_builder == null { + if context.tui_output_builder == null { write_builder(builder); set_temporary_storage_mark(temp_mark); } @@ -702,11 +702,11 @@ get_terminal_size :: () -> width: int, height: int { set_cursor_position :: inline (x: int, y: int) { assert_is_active(); - if context.tui_builder == null { + if context.tui_output_builder == null { print(Commands.SetCursorPosition, y, x); } else { - print_to_builder(context.tui_builder, Commands.SetCursorPosition, y, x); + print_to_builder(context.tui_output_builder, Commands.SetCursorPosition, y, x); } } @@ -749,28 +749,26 @@ set_terminal_title :: inline (title: string) { print(Commands.SetWindowTitle, title); } -using_buffer :: (buffer: *String_Builder) #expand { - __buffer := context.tui_builder; - context.tui_builder = buffer; - `defer context.tui_builder = __buffer; +using_builder_as_output :: (builder: *String_Builder) #expand { + __builder := context.tui_output_builder; + context.tui_output_builder = builder; + `defer context.tui_output_builder = __builder; } -// TODO Maybe we should have a different name for this...? tui_print :: inline (format_string: string, args: .. Any) { - if context.tui_builder == null { + if context.tui_output_builder == null { print(format_string, ..args, to_standard_error = false); } else { - print_to_builder(context.tui_builder, format_string, ..args); + print_to_builder(context.tui_output_builder, format_string, ..args); } } -// TODO Maybe we should have a different name for this...? tui_write_string :: inline (s: string) { - if context.tui_builder == null { + if context.tui_output_builder == null { write_string(s, to_standard_error = false); } else { - append(context.tui_builder, s); + append(context.tui_output_builder, s); } } @@ -204,6 +204,11 @@ count_digits :: (number: s64, base: s64 = 10) -> s64 { // Prints, on row y and column x, the time using 5 characters centered on space. // Returns the result of a call to mvprintw. print_time :: (y: int, x: int, time: s64, space: int) -> int { + + // Use TUI stubs so that callers may choose to use the tui_builder buffer. + print :: TUI.tui_print; + write_string :: TUI.tui_write_string; + TIME_CHARS :: 5; assert(space >= TIME_CHARS); @@ -217,7 +222,7 @@ print_time :: (y: int, x: int, time: s64, space: int) -> int { print_padding :: (size: int) { assert(size >= 0, "Cannot print negative padding values. The procedure accepts signed values just for convenience."); while size > 0 { - TUI.tui_write_string(" "); + write_string(" "); size -= 1; } } @@ -226,19 +231,19 @@ print_time :: (y: int, x: int, time: s64, space: int) -> int { if time < 0 { print_padding(left_padding); - TUI.tui_write_string(" - "); + write_string(" - "); print_padding(right_padding); return 0; } else if time == 0 { print_padding(left_padding); - TUI.tui_write_string(" 0 "); + write_string(" 0 "); print_padding(right_padding); return 0; } else if time < SECONDS_IN_MINUTE { print_padding(left_padding); - TUI.tui_print("%s ", FormatInt.{value = time, minimum_digits=3, padding=#char " "}); + print("%s ", FormatInt.{value = time, minimum_digits=3, padding=#char " "}); print_padding(right_padding); return 0; } @@ -246,7 +251,7 @@ print_time :: (y: int, x: int, time: s64, space: int) -> int { hours := time / SECONDS_IN_HOUR; minutes := (time - (hours * SECONDS_IN_HOUR) ) / SECONDS_IN_MINUTE; print_padding(left_padding); - TUI.tui_print("%:%", FormatInt.{value = hours, minimum_digits=2}, FormatInt.{value = minutes, minimum_digits=2}); + print("%:%", FormatInt.{value = hours, minimum_digits=2}, FormatInt.{value = minutes, minimum_digits=2}); print_padding(right_padding); return 0; } @@ -257,7 +262,7 @@ print_time :: (y: int, x: int, time: s64, space: int) -> int { ifx time >= #run mul_f64_s64(9.995, SECONDS_IN_DAY) then 1 else 2; print_padding(left_padding); - TUI.tui_print("%d", FormatFloat.{value = value, trailing_width=decimals, width=4}); + print("%d", FormatFloat.{value = value, trailing_width=decimals, width=4}); print_padding(right_padding); return 0; } @@ -268,13 +273,13 @@ print_time :: (y: int, x: int, time: s64, space: int) -> int { ifx time >= #run mul_f64_s64(9.995, SECONDS_IN_YEAR) then 1 else 2; print_padding(left_padding); - TUI.tui_print("%y", FormatFloat.{value = value, trailing_width=decimals, width=4}); + print("%y", FormatFloat.{value = value, trailing_width=decimals, width=4}); print_padding(right_padding); return 0; } else { print_padding(left_padding); - TUI.tui_write_string(" ∞ "); + write_string(" ∞ "); print_padding(right_padding); return 0; } @@ -907,10 +912,16 @@ draw_user_interface :: (db: *Database, layout: *Layout) { empty_line := talloc_string(size_x); memset(empty_line.data, #char " ", size_x); + /* TODO + It's not safe to use temporary memory here because the console resolution may increase and use more than what we have in temporary memory. + And temporary memory is configured at compile time. + We should dynamically allocate memory with some headroom and, at beggining of function... adjust it if necessary. + // init_string_builder(*buffer, 100000); // builder := buffer; + */ builder := String_Builder.{ allocator = temporary_allocator }; - TUI.using_buffer(*builder); + TUI.using_builder_as_output(*builder); adjust_first_day_of_week := int.[ (0 + FIRST_DAY_OF_WEEK) % NUM_WEEK_DAYS, |
