Ncurses mvwprintw 不打印

Ncurses mvwprintw doesnt print

我有一个简单的程序,在底部有一个主 window 和一个小的 window(没有线条,这样你就可以看到两个 windows:

+------------------+
|                  |
|                  | 
|                  |
+------------------+
|                  |
+------------------+

我希望底部区域是您可以输入的地方,这是我的源代码:

#include <termios.h>
#include <bits/stdc++.h>
#include <ncurses.h>
int main()
{
    int scrx;
    int scry;

    initscr();
    cbreak();
    noecho();
    clear();
    raw();

    getmaxyx(stdscr, scrx, scry);
    WINDOW* input = newwin(1, scrx, scry, 0);

    std::string cmdbuf;

    while(true)
    {
        int newx;
        int newy;
        getmaxyx(stdscr, newx, newy);

        if(newx != scrx || newy != scry)
        {
            // do stuff;
        }

        char c = wgetch(input);
        cmdbuf.push_back(c);
        werase(input);

        mvwprintw(input, 0, 0, cmdbuf.c_str());
        refresh();
        wrefresh(input);
    }
}

但是,它似乎没有打印任何内容,只是移动了我的光标(它在屏幕上移动了一半)。我怎样才能使文本实际打印出来并且我的光标实际在整个屏幕上移动?

newwin的声明是:

WINDOW *newwin(
         int nlines, int ncols,
         int begin_y, int begin_x);

您正在呼叫:

newwin(1,scry,scrx,0)

将你 window 的尺寸设置为 1scry 宽,并将其放在坐标 (0,srcx) 处。你想要的是:

newwin(1,scry,scrx-1,0)

其中 1 是 window 的高度。

此外,cbreak 覆盖了 raw,因此没有必要调用两者。

refresh 正在覆盖 mvwprintw,因为它们不同 windows。对于给定的示例,没有理由刷新 stdscr,因为没有任何内容(除了 initscr 调用)更新了 window。将 refresh 移出循环会有所帮助(但“做事”显然会干扰它)。

newx/newy 的逻辑过于零碎,无法评论(我会使用 getbegyx ...)。

为您整理了一下。按 'q' 退出。你明白了。

#include <termios.h>                                                                                                                                                                         
#include <bits/stdc++.h>
#include <ncurses.h>

int main()
{
  int scrx, scry;
  initscr();
  getmaxyx(stdscr, scry, scrx);
  WINDOW *w = newwin(1, scrx, scry - 1, 0);
  std::string cmdbuf {};
  char c = '[=10=]';

  while (c != 'q')
  {
    int newx, newy;
    getmaxyx(stdscr, newx, newy);

    if(newx != scrx || newy != scry)
    {
      // do stuff;
    }

    c = wgetch(w);
    cmdbuf += c;
    mvwprintw(w, 0, 0, "%s", cmdbuf.c_str());
    wrefresh(w);
  }

  delwin(w);
  endwin();
}