feat: Query screen size and store it in global state.
This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
/*** includes ***/
|
/*** includes ***/
|
||||||
|
|
||||||
|
#include <asm-generic/ioctls.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
@@ -14,6 +16,8 @@
|
|||||||
/*** data ***/
|
/*** data ***/
|
||||||
|
|
||||||
struct editorConfig {
|
struct editorConfig {
|
||||||
|
int screenrows;
|
||||||
|
int screencols;
|
||||||
struct termios orig_termios;
|
struct termios orig_termios;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -67,10 +71,22 @@ char editorReadKey() {
|
|||||||
return c;
|
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 ***/
|
/*** output ***/
|
||||||
|
|
||||||
void editorDrawRows() {
|
void editorDrawRows() {
|
||||||
for (int y = 0; y < 24; y++) {
|
for (int y = 0; y < E.screenrows; y++) {
|
||||||
write(STDOUT_FILENO, "~\r\n", 3);
|
write(STDOUT_FILENO, "~\r\n", 3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -105,8 +121,15 @@ void editorProcessKeypress() {
|
|||||||
|
|
||||||
/*** init ***/
|
/*** init ***/
|
||||||
|
|
||||||
|
void initEditor() {
|
||||||
|
if (getWindowSize(&E.screenrows, &E.screencols) == -1) {
|
||||||
|
die("getWindowSize");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
enableRawMode();
|
enableRawMode();
|
||||||
|
initEditor();
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
editorRefreshScreen();
|
editorRefreshScreen();
|
||||||
|
|||||||
Reference in New Issue
Block a user