diff options
author | AnotherTest <ali.mpfard@gmail.com> | 2020-04-29 01:47:41 +0430 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-04-28 23:29:07 +0200 |
commit | e83300dc276d0b64f528eb210ff933c418458e13 (patch) | |
tree | dd26a24f12a05ad627cb5de7aee74187ae369886 /Libraries/LibLine/Editor.cpp | |
parent | 9473733d7aacf5fdde1bb608dbb5b6d23ad8506b (diff) | |
download | serenity-e83300dc276d0b64f528eb210ff933c418458e13.zip |
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)
Diffstat (limited to 'Libraries/LibLine/Editor.cpp')
-rw-r--r-- | Libraries/LibLine/Editor.cpp | 23 |
1 files changed, 21 insertions, 2 deletions
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 <ctype.h> #include <stdio.h> #include <sys/ioctl.h> +#include <sys/select.h> +#include <sys/time.h> #include <unistd.h> namespace Line { @@ -1033,11 +1035,28 @@ size_t Editor::actual_rendered_string_length(const StringView& string) const Vector<size_t, 2> 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) { |