Skip to content

Commit

Permalink
https://viewsourcecode.org/snaptoken/kilo/03.rawInputAndOutput.html#t…
Browse files Browse the repository at this point in the history
…he-last-line
  • Loading branch information
Gurrium committed Dec 21, 2022
1 parent 3ecfc44 commit cdd1fef
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions kilo.c
Expand Up @@ -61,12 +61,31 @@ char editorReadKey() {
return c;
}

int getCursorPosition(int *rows, int *cols) {
char buf[32];
unsigned int i = 0;

if (write(STDIN_FILENO, "\x1b[6n", 4) != 4) return -1;

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) {
struct winsize ws;

if (1 || ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
if (write(STDOUT_FILENO, "\x1b[999C\x1b[999B", 12) != 12) return -1;
editorReadKey();
return getCursorPosition(rows, cols);
} else {
*cols = ws.ws_col;
*rows = ws.ws_row;
Expand Down

0 comments on commit cdd1fef

Please sign in to comment.