为什么颜色对标准标准不起作用?(PDCurses)

Why aren't colors working for stdscr? (PDCurses)

本文关键字:标准 PDCurses 不起作用 颜色 为什么      更新时间:2023-10-16

因此,我正在使用pdcurses为控制台应用程序添加一些颜色,但我遇到了问题。如果我制作第二个窗口并尝试为其输出着色,它会很好地工作,但如果我尝试为stdscr的输出着色,则不会发生任何事情。

我想继续使用stdscr,而不是用另一个窗口覆盖它,因为stdscr将正常接收我发送到stdout的输出,允许我使用C++风格的控制台接口。通过将输出发送到cout,它将进入stdscr,这是我目前所知道的使用pdcurses的C++接口的唯一方法。此外,其他库偶尔会将其输出直接发送到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,那么这应该是有效的。你还记得打电话给refresh让屏幕上显示你对stdscr的更改吗?