feat: Fallback to ioctl get window size with cursor position.
This commit is contained in:
@@ -71,11 +71,41 @@ char editorReadKey() {
|
|||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int getCursorPosition(int *rows, int *cols) {
|
||||||
|
if (write(STDOUT_FILENO, "\x1b[6n", 4) != 4) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
char buf[32];
|
||||||
|
unsigned int i = 0;
|
||||||
|
// Leave space for null terminator.
|
||||||
|
while (i < sizeof(buf) - 1) {
|
||||||
|
if (read(STDIN_FILENO, &buf[i], 1) != 1)
|
||||||
|
break;
|
||||||
|
if (buf[i] == 'R')
|
||||||
|
break;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
buf[i] = '\0';
|
||||||
|
|
||||||
|
if (buf[0] != '\x1b' || buf[1] != '[')
|
||||||
|
return -1;
|
||||||
|
if (sscanf(&buf[2], "%d;%d", rows, cols) != 2)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int getWindowSize(int *rows, int *cols) {
|
int getWindowSize(int *rows, int *cols) {
|
||||||
struct winsize ws;
|
struct winsize ws;
|
||||||
|
|
||||||
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
|
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
|
||||||
return -1;
|
// Fallback if ioctl can't query size:
|
||||||
|
// Move cursor to the bottom right, then query its position.
|
||||||
|
if (write(STDOUT_FILENO, "\x1b[999C\x1b[999B", 12) != 12) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return getCursorPosition(rows, cols);
|
||||||
}
|
}
|
||||||
|
|
||||||
*rows = ws.ws_row;
|
*rows = ws.ws_row;
|
||||||
|
|||||||
Reference in New Issue
Block a user