Ncurses program exits when terminal resized
当我调整终端窗口的大小时,以下程序退出。为什么以及如何阻止它?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <ncurses.h> #include <unistd.h> int main () { initscr (); printw ("Some text\ "); refresh (); sleep (100); endwin (); return 0; } |
我在这里找到了答案
调整端子大小后,
这是解决方案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <ncurses.h> #include <unistd.h> #include <signal.h> int main () { initscr (); signal (SIGWINCH, NULL); printw ("Some text\ "); refresh (); sleep (100); endwin (); return 0; } |
您需要处理SIGWINCH信号:
1 2 3 4 5 6 7 8 9 10 11 12 13 | #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 // ... } |