调整终端大小后 box() 如何采用最大宽度

How box() is adopting max width after resizing terminal

我不知道重新执行函数 box() 是如何自动采用最大宽度的,这里是测试代码:

getmaxyx(stdscr,y,x);
      WINDOW *titleWin = update_title_win(NULL,2,x,0,0);
      while((ch = getch()) != 27) {
          if(ch == KEY_RESIZE) {
              update_title_win(titleWin);
          }
      }

update_title_win() 代码:

WINDOW* update_title_win(WINDOW *win, int height, int width, int y, int x)
{
    if(!win)
        win = newwin(height,width,y,x);
    box(win,0,0);
    refresh();
    wrefresh(win);
    return win;
}

ncurses 库使用 resizetermgetch returns KEY_RESIZE 时进行调整。如果 (terminal/screen) 缩小,该函数将减小 windows 的大小,但屏幕尺寸增加时的行为不太明显。

源代码中 resizeterm does not mention this, but a comment 的手册页解释了行为:

/*
 * If we're increasing size, recursively search for windows that have no
 * parent, increase those to fit, then increase the contained window, etc.
 */

A window 使用 newwin has no parent (unlike something made with subwin 创建)。由于 titleWin 以屏幕的全宽开始,随着屏幕宽度的变化,ncurses 将尝试保持标题 window full-width.