feat: Query screen size and store it in global state.

This commit is contained in:
jmug
2026-10-09 20:44:43 -07:00
parent ec85525a55
commit 7fbe39b04f
+24 -1
View File
@@ -1,9 +1,11 @@
/*** includes ***/
#include <asm-generic/ioctls.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
@@ -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();