2014-09-20 43 views
0

所以,我使用pdcurses來添加一些顏色到我的控制檯應用程序,但我遇到了問題。如果我製作第二個窗口並嘗試着色它的輸出,它工作得很好,但如果我嘗試將輸出顏色爲stdscr,則什麼都不會發生。爲什麼不能用於stdscr的顏色? (PDCurses)

我想繼續使用stdscr而不是用另一個窗口來覆蓋它,因爲stdscr會收到我通常發送給標準輸出的輸出,允許我使用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); 

我在做什麼錯?

回答

1

您錯誤地認爲寫入標準輸出將寫入stdscr。實際上,這樣做完全繞過了PDCurses並直接寫入控制檯,就好像您根本沒有使用PDCurses一樣。您需要使用PDCurses函數來寫入到PDCurses窗口,包括stdscr。你需要說服你正在使用的庫,而不是將輸出發送到標準輸出不執行此操作,因爲這會混淆PDCurses。

wprintw(stdstc, "\nLe Testing.\n");不起作用的明顯原因是您使用stdstc而不是stdscr。假設這只是你的文章中的一個錯字,而且你確實在你的程序中寫入了stdscr,那麼這應該起作用。您是否記得致電refresh實際上對屏幕上顯示的stdscr進行了更改?

+0

確實這是一個錯字,謝謝。按照預期,使用printw和wprintw可以打印彩色文本。有一件事讓我困惑,即'printw'的輸出出現在'cout'的上面,即使'printw'後來出現在代碼中也是如此。我想這只是標準輸出,像你說的那樣令人困惑。我可以修改我的代碼,純粹使用pdcurses api(至少在這個階段)。現在我正在使用'std :: ostringstream流;流<<數據; (stream.str()c_str())printw;'。 – 2014-09-21 05:35:23

相關問題