Update screen when terminal window will be changed #2

This commit is contained in:
Aaron Fischer 2018-10-18 13:27:51 +02:00
parent 6b2c3a00a5
commit 45c0f51cb3
4 changed files with 22 additions and 4 deletions

BIN
fn

Binary file not shown.

21
fn.c
View file

@ -16,6 +16,7 @@
#include <termios.h> #include <termios.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <signal.h>
#include "fn.h" #include "fn.h"
#include "terminal.h" #include "terminal.h"
@ -847,6 +848,18 @@ void editorProcessKeypress() {
/*** init ***/ /*** init ***/
void windowResizeCallback(int signum) {
if (signum != SIGWINCH) {
return;
}
if (setWindowSize(&E.screenrows, &E.screencols) == -1) {
die("setWindowSize");
}
E.screenrows -= 2;
editorRefreshScreen();
}
void initEditor() { void initEditor() {
E.cx = 0; E.cx = 0;
E.cy = 0; E.cy = 0;
@ -861,12 +874,14 @@ void initEditor() {
E.statusmsg_time = 0; E.statusmsg_time = 0;
E.syntax = NULL; E.syntax = NULL;
if (getWindowSize(&E.screenrows, &E.screencols) == -1) if (setWindowSize(&E.screenrows, &E.screencols) == -1) {
die("getWindowSize"); die("setWindowSize");
}
E.screenrows -= 2; E.screenrows -= 2;
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
enableRawMode(); enableRawMode();
initEditor(); initEditor();
if (argc >= 2) { if (argc >= 2) {
@ -875,6 +890,8 @@ int main(int argc, char *argv[]) {
editorSetStatusMessage("HELP: Ctrl-S = save | Ctrl-Q = Quit | Ctrl-F = find"); editorSetStatusMessage("HELP: Ctrl-S = save | Ctrl-Q = Quit | Ctrl-F = find");
signal(SIGWINCH, windowResizeCallback);
while (1) { while (1) {
editorRefreshScreen(); editorRefreshScreen();
editorProcessKeypress(); editorProcessKeypress();

View file

@ -107,7 +107,7 @@ int getCursorPosition(int *rows, int *cols) {
return 0; return 0;
} }
int getWindowSize(int *rows, int *cols) { int setWindowSize(int *rows, int *cols) {
struct winsize ws; struct winsize ws;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) { if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
@ -123,3 +123,4 @@ int getWindowSize(int *rows, int *cols) {
return 0; return 0;
} }
} }

View file

@ -8,6 +8,6 @@ void enableRawMode();
int editorReadKey(); int editorReadKey();
int getCursorPosition(int *rows, int *cols); int getCursorPosition(int *rows, int *cols);
int getWindowSize(int *rows, int *cols); int setWindowSize(int *rows, int *cols);
#endif #endif