1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-30 22:38:12 +00:00

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)
This commit is contained in:
AnotherTest 2020-04-29 01:47:41 +04:30 committed by Andreas Kling
parent 9473733d7a
commit e83300dc27

View file

@ -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) {