diff --git a/kilo.c b/kilo.c index 14d18af..911b437 100644 --- a/kilo.c +++ b/kilo.c @@ -1,9 +1,11 @@ /*** includes ***/ +#include #include #include #include #include +#include #include #include @@ -14,6 +16,8 @@ /*** data ***/ struct editorConfig { + int screenrows; + int screencols; struct termios orig_termios; }; @@ -67,10 +71,22 @@ char editorReadKey() { return c; } +int getWindowSize(int *rows, int *cols) { + struct winsize ws; + + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) { + return -1; + } + + *rows = ws.ws_row; + *cols = ws.ws_col; + return 0; +} + /*** output ***/ void editorDrawRows() { - for (int y = 0; y < 24; y++) { + for (int y = 0; y < E.screenrows; y++) { write(STDOUT_FILENO, "~\r\n", 3); } } @@ -105,8 +121,15 @@ void editorProcessKeypress() { /*** init ***/ +void initEditor() { + if (getWindowSize(&E.screenrows, &E.screencols) == -1) { + die("getWindowSize"); + } +} + int main() { enableRawMode(); + initEditor(); while(1) { editorRefreshScreen();