diff options
| author | mo khan <mo.khan@gmail.com> | 2021-01-21 21:06:14 -0700 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2021-01-21 21:06:14 -0700 |
| commit | e51265e705d638909a0b3165f2c761432853d985 (patch) | |
| tree | d57dd372ed776492a5eeb1b699fb7892f7b3650d | |
| parent | b554effdabf7d2529738bd93855df884e3e19c65 (diff) | |
get window size
| -rw-r--r-- | kilo.c | 23 |
1 files changed, 22 insertions, 1 deletions
@@ -4,6 +4,7 @@ #include <errno.h> #include <stdio.h> #include <stdlib.h> +#include <sys/ioctl.h> #include <termios.h> #include <unistd.h> @@ -14,6 +15,8 @@ /*** data ***/ struct editor_config { + int screenrows; + int screencols; struct termios orig_termios; }; @@ -58,10 +61,22 @@ char editor_read_key() { return c; } +int get_window_size(int *rows, int *cols) { + struct winsize ws; + + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) { + return -1; + } else { + *cols = ws.ws_col; + *rows = ws.ws_row; + return 0; + } +} + /*** output ***/ void editor_draw_rows() { - for (int i = 0; i < 24; i++) { + for (int i = 0; i < E.screenrows; i++) { write(STDOUT_FILENO, "~\r\n", 3); } } @@ -99,8 +114,14 @@ void editor_process_keypress() { /*** init ***/ +void init_editor() { + if (get_window_size(&E.screenrows, &E.screencols) == -1) + die("get_window_size"); +} + int main() { enable_raw_mode(); + init_editor(); while (1) { editor_refresh_screen(); |
