为什么颜色不能用于stdscr?(PDCurses)

海顿诉哈拉赫

因此,我正在使用pdcurses向控制台应用程序添加一些颜色,但是遇到了问题。如果我创建第二个窗口并尝试对其输出进行着色,则它可以正常工作,但是如果我尝试对stdscr进行输出着色,则不会发生任何事情。

我想继续使用stdscr而不是用另一个窗口覆盖它,因为stdscr将接收正常发送到stdout的输出,从而允许我在控制台中使用C ++风格的接口。通过将输出发送到cout,它将发送到stdscr,这是目前我所知道的唯一使用C ++接口进行pdcurses的方法。此外,其他库偶尔也会将其输出直接发送到stdout,如果我使用stdscr,则该输出不会丢失(我知道lua的print功能远不如我所听到的那样)。

这里是一些示例代码:

// This prints a red '>' in the inputLine window properly. //
wattron(inputLine, A_BOLD | COLOR_PAIR(COLOR_RED));
wprintw(inputLine, "\n> ");
wattroff(inputLine, A_BOLD | COLOR_PAIR(COLOR_RED));

// This prints a light grey "Le Testing." to stdscr.  Why isn't it red? //
wattron(stdscr, A_BOLD | COLOR_PAIR(COLOR_RED));
cout << "\nLe Testing.\n";
wattroff(stdscr, A_BOLD | COLOR_PAIR(COLOR_RED));

// This does nothing.  I have no idea why. //
wattron(stdscr, A_BOLD | COLOR_PAIR(COLOR_RED));
wprintw(stdscr, "\nLe Testing.\n");
wattroff(stdscr, A_BOLD | COLOR_PAIR(COLOR_RED));

这是我初始化pdcurses的方法:

   // Open the output log which will mimic stdout. //
   if (userPath)
   {
      string filename = string(userPath) + LOG_FILENAME;
      log.open(filename.c_str());
   }

      // Initialize the pdCurses screen. //
   initscr();

      // Resize the stdout screen and create a line for input. //
   resize_window(stdscr, LINES - 1, COLS);
   inputLine = newwin(1, COLS, LINES - 1, 0);

      // Initialize colors. //
   if (has_colors())
    {
        start_color();
        for (int i = 1; i <= COLOR_WHITE; ++i)
        {
            init_pair(i, i, COLOR_BLACK);
        }
    }
    else
   {
      cout << "Terminal cannot print colors.\n";
      if (log.is_open())
         log << "Terminal cannot print colors.\n";
   }

   scrollok(stdscr, true);
    scrollok(inputLine, true);

   leaveok(stdscr, true);
    leaveok(inputLine, true);

    nodelay(inputLine, true);
    cbreak();
    noecho();
    keypad(inputLine, true);

我究竟做错了什么?

罗斯·里奇

您错误地认为写入标准输出将写入stdscr实际上,这样做完全绕开了PDCurses并直接写入控制台,就像您根本没有使用PDCurses一样。您需要使用PDCurses函数来写入PDCurses窗口,包括stdscr您需要说服正在使用的任何库,然后不要将输出发送到stdout来执行此操作,因为这会使PDCurses感到困惑。

wprintw(stdstc, "\nLe Testing.\n");不起作用的明显原因是您使用stdstc而不是stdscr假设那只是您文章中的错字,并且您确实确实stdscr在程序中编写过代码,那么那应该可行。您还记得打过电话refreshstdscr在屏幕上实际显示所做的更改吗?

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章