From 7f8a90efed56db9f49e9d7e5167372ddd7aa1df2 Mon Sep 17 00:00:00 2001 From: dam Date: Sun, 26 May 2024 11:54:44 +0100 Subject: Patched TUI module. --- modules/TUI/module.jai | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) (limited to 'modules') diff --git a/modules/TUI/module.jai b/modules/TUI/module.jai index 3c40ee1..124b906 100644 --- a/modules/TUI/module.jai +++ b/modules/TUI/module.jai @@ -693,10 +693,12 @@ get_terminal_size :: () -> width: int, height: int { auto_release_temp(); + rows, columns: int = ---; + flush_input(); write_string(Commands.QueryWindowSizeInChars); - - rows, columns: int = ---; + + // Wait a bit for a response to QueryWindowSizeInChars... if OS_wait_for_input(1) { // Expected response format: \e[8;;t @@ -704,15 +706,9 @@ get_terminal_size :: () -> width: int, height: int { FORMAT :: "\e[8;;t"; input := read_input(64, #char "t",, allocator = temporary_allocator); - // Discard head noise. - while input.count >= 3 && (input[0] != FORMAT[0] || input[1] != FORMAT[1] || input[2] != FORMAT[2]) { - advance(*input); - } - - // Discard tail noise. - while input.count >= 3 && input[input.count-1] != FORMAT[FORMAT.count-1] { - input.count -= 1; - } + // Discard head noise, assuming that the response is at the end of the input (because we used a terminator character during read). + start_idx := find_index_from_right(input, #char "\e"); + if start_idx > 0 then advance(*input, start_idx); assert(input.count >= 3 && input[0] == FORMAT[0] && input[1] == FORMAT[1] && input[2] == FORMAT[2] && input[input.count-1] == FORMAT[FORMAT.count-1], @@ -761,20 +757,14 @@ get_cursor_position :: () -> x: int, y: int { FORMAT :: "\e[;R"; input := read_input(64, #char "R",, allocator = temporary_allocator); - // Discard head noise. - while input.count >= 2 && (input[0] != FORMAT[0] || input[1] != FORMAT[1]) { - advance(*input); - } - - // Discard tail noise. - while input.count >= 2 && input[input.count-1] != FORMAT[FORMAT.count-1] { - input.count -= 1; - } + // Discard head noise, assuming that the response is at the end of the input (because we used a terminator character during read). + start_idx := find_index_from_right(input, #char "\e"); + if start_idx > 0 then advance(*input, start_idx); assert(input.count >= 2 && input[0] == FORMAT[0] && input[1] == FORMAT[1] && input[input.count-1] == FORMAT[FORMAT.count-1], "Failed to query cursor position: invalid response."); - + advance(*input, 2); parts := split(input, ";",, allocator = temporary_allocator); row := parse_int(*parts[0]); -- cgit v1.2.3