我正在写一个ncurses程序,并试图让它正确响应终端调整大小.虽然我可以在程序中正确读取终端尺寸,ncurses但似乎没有正确处理新尺寸.这是一个(有点冗长)示例程序:
#include <ncurses.h>
#include <string.h>
#include <signal.h>
#include <sys/ioctl.h>
void handle_winch(int sig){
struct winsize w;
ioctl(0, TIOCGWINSZ, &w);
COLS = w.ws_col;
LINES = w.ws_row;
wresize(stdscr, LINES, COLS);
clear();
mvprintw(0, 0, "COLS = %d, LINES = %d", COLS, LINES);
for (int i = 0; i < COLS; i++)
mvaddch(1, i, '*');
refresh();
}
int main(int argc, char *argv[]){
initscr();
struct sigaction sa;
memset(&sa, 0, sizeof(struct sigaction));
sa.sa_handler = handle_winch;
sigaction(SIGWINCH, &sa, NULL);
while(getch() != 27) {}
endwin();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果运行它,您可以看到正确检索了终端尺寸.但第二行,它应该*在屏幕上绘制字符,不起作用.尝试水平调整窗口大小以使其变大,*s 线不会变大.
这有什么问题?我知道可以暂时离开curses模式,但我更喜欢更清洁的解决方案.谢谢!
Nik*_* C. 16
不要设置COLS和LINES.这些由ncurses管理.另外,让ncurses在调整大小后正确重新初始化.这意味着,不要调用wresize().只需调用endwin()即可.在使用其他ncurses函数之前,确保在endwin()调用之后直接调用refresh().
您根本不需要ioctl().ncurses负责自动检测新的大小.
所以你需要的只是一个endwin()调用:
void handle_winch(int sig)
{
endwin();
// Needs to be called after an endwin() so ncurses will initialize
// itself with the new terminal dimensions.
refresh();
clear();
mvprintw(0, 0, "COLS = %d, LINES = %d", COLS, LINES);
for (int i = 0; i < COLS; i++)
mvaddch(1, i, '*');
refresh();
}
Run Code Online (Sandbox Code Playgroud)
此外,一些ncurses版本配置为提供自己的SIGWINCH处理程序.当调整大小时,这些版本将KEY_RESIZE作为键输入返回.如果你要使用它,你根本不需要信号处理程序.相反,您只需要:
#include <ncurses.h>
#include <string.h>
int main()
{
initscr();
int key;
while ((key = getch()) != 27) {
if (key == KEY_RESIZE) {
clear();
mvprintw(0, 0, "COLS = %d, LINES = %d", COLS, LINES);
for (int i = 0; i < COLS; i++)
mvaddch(1, i, '*');
refresh();
}
}
endwin();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,您不能依赖所有使用KEY_RESIZE配置的ncurses安装,因此信号处理程序是最便携的解决方案.