Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ncurses program exits when terminal resized

Tags:

c

linux

ncurses

When i resize my terminal window, the below program exits. Why and how can stop it?

#include <ncurses.h>
#include <unistd.h>

int main () {
    initscr ();

    printw ("Some text\n");
    refresh ();

    sleep (100);
    endwin ();

    return 0;
}

2 Answers

I found the answer here

When terminal has resized, the SIGWINCH signal raises and program exits.

Here is the solution:

#include <ncurses.h>
#include <unistd.h>
#include <signal.h>

int main () {
    initscr ();

    signal (SIGWINCH, NULL);

    printw ("Some text\n");
    refresh ();

    sleep (100);
    endwin ();

    return 0;
}

You need to handle the SIGWINCH signal :

#include <signal.h>

/* resizer handler, called when the user resizes the window */
void resizeHandler(int sig) {
    // update layout, do stuff...
}

int main(int argc, char **argv) {
    signal(SIGWINCH, resizeHandler);

    // play with ncurses
    // ...
}
like image 40
wldsvc Avatar answered Sep 07 '25 17:09

wldsvc