diff options
| author | dam <dam@gudinoff> | 2024-05-02 00:57:53 +0100 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2024-05-02 00:57:53 +0100 |
| commit | 66dfee359cbe2c18ce8400edeb801a2373a771f4 (patch) | |
| tree | f1324ad9a8e058ae6a7d2704bdb6092e02015e1f | |
| parent | 90d17cde7d92be5e2c1bedb97c8c8d5ca324a39d (diff) | |
| download | task-time-tracker-66dfee359cbe2c18ce8400edeb801a2373a771f4.tar.zst task-time-tracker-66dfee359cbe2c18ce8400edeb801a2373a771f4.zip | |
WIP : Using string builder to improve TUI performance.
| -rw-r--r-- | modules/TUI/module.jai | 87 | ||||
| -rw-r--r-- | ttt.jai | 2 |
2 files changed, 83 insertions, 6 deletions
diff --git a/modules/TUI/module.jai b/modules/TUI/module.jai index f8b8265..991dc74 100644 --- a/modules/TUI/module.jai +++ b/modules/TUI/module.jai @@ -3,7 +3,6 @@ #scope_file // - fix/implement/finish `TODO` on `TUI\module` (use some sort of buffet to reduce io/print calls) -// - fix/implement/finish `TODO` on `TUI\module` (use `dirty_bit_flag` to only update ehat has been changed) #if OS == { case .LINUX; @@ -297,7 +296,7 @@ module_logger :: (message: string, data: *void, info: Log_Info) { } assert_is_active :: inline () { - assert(active, "TUI is not ready."); + assert(active, "TUI is not ready. Please call TUI.start() before."); // TODO Improve error message... and maybe use the logger and return success=false flag } //////////////////////////////////////////////////////////////////////////////// @@ -444,10 +443,6 @@ read_input_line :: (count_limit: int, is_visible: bool = true) -> string, Key { str.count = 0; idx := 0; - // TODO Some of these may be nice to have: - // > https://unix.stackexchange.com/questions/255707/what-are-the-keyboard-shortcuts-for-the-command-line - // At least... add the Ctrl+u to clear the entire line. - x, y := get_cursor_position(); write_strings(Commands.ShowCursor, Commands.StartBlinking, Commands.BlinkingBarShape); @@ -531,6 +526,11 @@ read_input_line :: (count_limit: int, is_visible: bool = true) -> string, Key { start :: () -> success := true #must { if active == true return; + + if inited == false { + init_string_builder(*temp_builder,, temp); // TODO Testing + inited = true; + } input_string.data = input_buffer.data; input_string.count = 0; @@ -583,7 +583,19 @@ flush_input :: () { input_string.count = 0; } + + +// Using String_Builder improves: +// - procedure time is now ~12x faster; +// - reduces CPU usage by ~ +average: float64 = 0; +counter: float64 = 0; +inited:=false; +temp_builder: String_Builder; + +#if false { draw_box :: (x: int, y: int, width: int, height: int) { + t0 := current_time_monotonic(); assert_is_active(); // TODO Check if using a String_Builder improves performance (measure it)! @@ -626,6 +638,69 @@ draw_box :: (x: int, y: int, width: int, height: int) { write_string(Drawings.CornerBR); write_string(Commands.TextMode); + + t1 := current_time_monotonic(); + set_cursor_position(2, 47); + sample := cast(float64)to_nanoseconds(t1-t0)/1000; + average = (sample + counter * average) / (counter + 1); + counter += 1; + print(">%<", average); +} + +} else { + + +// TODO Not sure how... but this seems to be leaking memory... maybe it's the String_Builder!? +draw_box :: (x: int, y: int, width: int, height: int) { + t0 := current_time_monotonic(); + + assert_is_active(); + + // TODO Check if using a String_Builder improves performance (measure it)! + // TODO Validate input parameters against the terminal size. + assert(x > 0 && y > 0 && width > 1 && height > 1, "Invalid arguments."); + + auto_release_temp(); + + builder := temp_builder; + // init_string_builder(*builder); + + append(*builder, Commands.DrawingMode); + append(*builder, tprint(Commands.SetCursorPosition, y, x)); + append(*builder, Drawings.CornerTL); + + for 1..width-2 { + append(*builder, Drawings.LineH); + } + append(*builder, Drawings.CornerTR); + + for idx: y+1..y+height-2 { + tmpL := tprint(Commands.SetCursorPosition, idx, x); + tmpR := tprint(Commands.SetCursorPosition, idx, x+width-1); + append(*builder, tmpL); + append(*builder, Drawings.LineV); + append(*builder, tmpR); + append(*builder, Drawings.LineV); + } + + append(*builder, tprint(Commands.SetCursorPosition, y+height-1, x)); + append(*builder, Drawings.CornerBL); + for 1..width-2 { + append(*builder, Drawings.LineH); + } + append(*builder, Drawings.CornerBR); + + append(*builder, Commands.TextMode); + + write_string(builder_to_string(*builder)); + + t1 := current_time_monotonic(); + set_cursor_position(2, 47); + sample := cast(float64)to_nanoseconds(t1-t0)/1000; + average = (sample + counter * average) / (counter + 1); + counter += 1; + print(">%<", average); +} } // TODO Maybe rename to "clear()" @@ -28,6 +28,8 @@ #import "UTF8"; TUI :: #import "TUI"(COLOR_MODE=4); +// - fix/implement/finish TODO : use `dirty_bit_flag` to only update ehat has been changed + VERSION :: "2.0"; // Use only 3 chars (to fit layouts). YEAR :: "2024"; FIRST_DAY_OF_WEEK :: 1; // (0-6, Sunday = 0). |
