From e83300dc276d0b64f528eb210ff933c418458e13 Mon Sep 17 00:00:00 2001 From: AnotherTest Date: Wed, 29 Apr 2020 01:47:41 +0430 Subject: LibLine: Drop stray input before doing vt_dsr This patch fixes the issue where some data would be buffered while the editor is not editing, and vt_dsr would read them, resulting in the cursor to jump to (1,1) --- Libraries/LibLine/Editor.cpp | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'Libraries') diff --git a/Libraries/LibLine/Editor.cpp b/Libraries/LibLine/Editor.cpp index 57ff046e09..b2d37e7e76 100644 --- a/Libraries/LibLine/Editor.cpp +++ b/Libraries/LibLine/Editor.cpp @@ -29,6 +29,8 @@ #include #include #include +#include +#include #include namespace Line { @@ -1033,11 +1035,28 @@ size_t Editor::actual_rendered_string_length(const StringView& string) const Vector Editor::vt_dsr() { + char buf[16]; + u32 length { 0 }; + + // read whatever junk there is before talking to the terminal + bool more_junk_to_read { false }; + timeval timeout { 0, 0 }; + fd_set readfds; + FD_ZERO(&readfds); + FD_SET(0, &readfds); + + do { + more_junk_to_read = false; + (void)select(1, &readfds, nullptr, nullptr, &timeout); + if (FD_ISSET(0, &readfds)) { + read(0, buf, 16); + more_junk_to_read = true; + } + } while (more_junk_to_read); + fputs("\033[6n", stdout); fflush(stdout); - char buf[16]; - u32 length { 0 }; do { auto nread = read(0, buf + length, 16 - length); if (nread < 0) { -- cgit v1.2.3