0

So, I'm using pdcurses to add some color to my console application, but I'm having issues. If I make a second window and try to color it's output, it works just fine, but if I try to color output to stdscr, nothing happens.

I want to keep using stdscr rather than cover it with another window, because stdscr will receive output that I send to stdout normally, allowing me to use a C++ style interface to the console. By sending output to cout, it goes to stdscr, and that's currently the only way I know of to use a C++ interface to pdcurses. In addition, other libraries will occasionally send their output straight to stdout, and if I use stdscr that output wont be lost (I know of lua's print function off the top of my heard).

Here's some sample code:

// 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));

Here is how I initialize 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);

What am I doing wrong?

4

1 回答 1

2

您错误地假设写入标准输出将写入stdscr. 事实上,这样做完全绕过了 PDCurses 并直接写入控制台,就好像您根本没有使用 PDCurses 一样。您需要使用 PDCurses 函数写入 PDCurses 窗口,包括stdscr. 您需要说服您正在使用的任何库而不是将输出发送到标准输出不要这样做,因为这会混淆 PDCurses。

wprintw(stdstc, "\nLe Testing.\n");不起作用的明显原因是您使用stdstc而不是stdscr. 假设这只是您帖子中的一个错字,并且您确实确实stdscr在程序中进行了编写,那么这应该可以。你还记得打电话refresh让你的更改真正stdscr显示在屏幕上吗?

于 2014-09-21T01:32:01.697 回答