Skip to content

Commit

Permalink
Handle SIGWINCH signal to properly resize editor
Browse files Browse the repository at this point in the history
  • Loading branch information
lpereira authored and antirez committed Jul 2, 2020
1 parent c1a27fa commit e10bc60
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions kilo.c
Expand Up @@ -34,7 +34,9 @@

#define KILO_VERSION "0.0.1"

#ifdef __GLIBC__
#define _POSIX_C_SOURCE 200809L
#endif

#include <termios.h>
#include <stdlib.h>
Expand All @@ -49,6 +51,7 @@
#include <unistd.h>
#include <stdarg.h>
#include <fcntl.h>
#include <signal.h>

/* Syntax highlight types */
#define HL_NORMAL 0
Expand Down Expand Up @@ -1244,6 +1247,22 @@ int editorFileWasModified(void) {
return E.dirty;
}

void updateWindowSize(void) {
if (getWindowSize(STDIN_FILENO,STDOUT_FILENO,
&E.screenrows,&E.screencols) == -1) {
perror("Unable to query the screen for size (columns / rows)");
exit(1);
}
E.screenrows -= 2; /* Get room for status bar. */
}

void handleSigWinCh(int unused __attribute__((unused))) {
updateWindowSize();
if (E.cy > E.screenrows) E.cy = E.screenrows - 1;
if (E.cx > E.screencols) E.cx = E.screencols - 1;
editorRefreshScreen();
}

void initEditor(void) {
E.cx = 0;
E.cy = 0;
Expand All @@ -1254,13 +1273,8 @@ void initEditor(void) {
E.dirty = 0;
E.filename = NULL;
E.syntax = NULL;
if (getWindowSize(STDIN_FILENO,STDOUT_FILENO,
&E.screenrows,&E.screencols) == -1)
{
perror("Unable to query the screen for size (columns / rows)");
exit(1);
}
E.screenrows -= 2; /* Get room for status bar. */
updateWindowSize();
signal(SIGWINCH, handleSigWinCh);
}

int main(int argc, char **argv) {
Expand Down

0 comments on commit e10bc60

Please sign in to comment.