Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions cpp-terminal/terminal.h
Original file line number Diff line number Diff line change
Expand Up @@ -384,31 +384,28 @@ class Terminal: public BaseTerminal {
void get_cursor_position(int& rows, int& cols) const
{
char buf[32];
unsigned int i = 0;
write(cursor_position_report());
while (i < sizeof(buf) - 1) {
while (!read_raw(&buf[i])) {
};
if (buf[i] == 'R')
for (unsigned int i = 0; i < sizeof(buf) - 1; i++) {
while (!read_raw(&buf[i]));
if (buf[i] == 'R') {
if (i < 5) {
throw std::runtime_error("get_cursor_position(): too short response");
}
else {
buf[i] = '\0';
}
break;
i++;
}
buf[i] = '\0';
if (i < 5) {
throw std::runtime_error("get_cursor_position(): too short response");
}
}
// Find the result in the response, drop the rest:
i = 0;
while (i < sizeof(buf) - 1 - 5) {
for (unsigned int i = 0; i < sizeof(buf) - 6; i++) {
if (buf[i] == '\x1b' && buf[i+1] == '[') {
if (convert_string_to_int(&buf[i+2], "%d;%d", &rows, &cols) == 2) {
return;
} else {
if (convert_string_to_int(&buf[i+2], "%d;%d", &rows, &cols) != 2) {
throw std::runtime_error("get_cursor_position(): result could not be parsed");
}
return;
}
if (buf[i] == '\0') break;
i++;
}
throw std::runtime_error("get_cursor_position(): result not found in the response");
}
Expand Down