ncurses - 调整小故障

时间:2012-12-04 16:14:29

标签: c ncurses

我正在编写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;
}

如果您运行它,您可以看到正确检索终端尺寸。但第二行,它应该在屏幕上绘制*个字符,但不起作用。尝试水平调整窗口大小以使其变大,* s的行不会变大。

这里有什么问题?我知道可以暂时离开curses模式,但我更喜欢更清洁的解决方案。谢谢!

1 个答案:

答案 0 :(得分:16)

请勿设置COLSLINES。这些由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();
}

此外,一些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;
}

不幸的是,您不能依赖所有使用KEY_RESIZE配置的ncurses安装,因此信号处理程序是最便携的解决方案。