Commit e51265e

mo khan <mo.khan@gmail.com>
2021-01-22 04:06:14
get window size
1 parent b554eff
Changed files (1)
kilo.c
@@ -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();