aboutsummaryrefslogtreecommitdiff
path: root/TUI
diff options
context:
space:
mode:
authordam <dam@gudinoff>2024-05-26 11:50:22 +0100
committerdam <dam@gudinoff>2024-08-29 10:03:39 +0100
commit4383bcc293f8c9697ce54cec951e3bfc408a6c86 (patch)
tree1b618ddaf5da08026c51966619e558b5f04ea732 /TUI
parent7d358263c8b7dd154f2e7f049659f4c706ab5b75 (diff)
downloadjai-modules-4383bcc293f8c9697ce54cec951e3bfc408a6c86.tar.zst
jai-modules-4383bcc293f8c9697ce54cec951e3bfc408a6c86.zip
Improved head noise removal while parsing system responses on TUI.get_cursor_position and TUI.get_terminal_size.
Diffstat (limited to 'TUI')
-rw-r--r--TUI/module.jai32
1 files changed, 11 insertions, 21 deletions
diff --git a/TUI/module.jai b/TUI/module.jai
index 0563384..1ed950b 100644
--- a/TUI/module.jai
+++ b/TUI/module.jai
@@ -695,10 +695,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;<r>;<c>t
@@ -706,15 +708,9 @@ get_terminal_size :: () -> width: int, height: int {
FORMAT :: "\e[8;<r>;<c>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],
@@ -763,20 +759,14 @@ get_cursor_position :: () -> x: int, y: int {
FORMAT :: "\e[<r>;<c>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]);