2016-07-23 77 views
0

我想製作一個庫,以簡化ncurses用來顯示顏色。我正在做面向對象的事情,以便將來處理變化很容易。但問題是我無法工作這個代碼。Linux終端不顯示任何與ncurses

#include <ncurses.h> 
#include <string.h> 
#include <string> 
#include <unistd.h> 
#include <iostream> 

using namespace std; 

class ColorWindow { 
    private: 
     bool canColor; 
     WINDOW* container; 
     int height, width, startx, starty; 

    public: 
     ColorWindow() { 
      if(has_colors()) { 
       canColor=true; 
      } 

      this->height = 20; 
      this->width = 84; 
      this->starty = (LINES - height)/2; /* Calculating for a center placement */ 
      this->startx = (COLS - width)/2; 
     } 

     bool writeStringWithColor(int x, int y, const char* message) { 
      if(!canColor) { 
       writeString(3, 5, "Sorry, your term can't show colors."); 
       return false; 
      } 

      init_pair(1, COLOR_RED, COLOR_BLACK); 

      writeString(0, 10, "aaaaaaaaa"); 

      wattron(getContainer(), COLOR_PAIR(1)); 
      writeString(x, y, message); 
      wattroff(getContainer(), COLOR_PAIR(1)); 
     } 

     void writeString(int x, int y, const char* message) { 
      mvwprintw(getContainer(), x, y, message); 
     } 

     WINDOW* createNewContainer() { 
      this->container = newwin(height, width, starty, startx); 
      wrefresh(this->container);  /* Show that box  */ 

      return getContainer(); 
     } 

     WINDOW* getContainer() { 
      return this->container; 
     } 

     void refreshContainer() { 
      refresh(); 
      wrefresh(this->container);  /* Show that box  */ 
     }  
}; 

int main() { 
    ColorWindow cw = ColorWindow(); 


    initscr();   /* Start curses mode  */ 
    cbreak();   /* Line buffering disabled, Pass on 
         * everything to me  */ 
    keypad(stdscr, TRUE); 
    start_color(); 

    cw.createNewContainer(); 

    bool success = cw.writeStringWithColor(0, 10, "Hello everyone in color!!"); 
    if(!success) 
     cw.writeString(0, 10, "Write with color failed :("); 
    cw.refreshContainer(); 
    sleep(2); 
    endwin(); 

    return 0; 
} 

在此先感謝。

+0

'canColor'看起來可能未初始化的給我。 '容器'也看起來未初始化。 – melpomene

+0

如果會顯示「Sorry,your term [...]」,但它會顯示任何內容,即使是我在那裏寫的「aaaaaa」字符串。 – Shirkam

回答

0

有一對夫婦在你的代碼的bug:

  • 你不初始化canColorcontainer,所以複製的字段爲cwmain是未定義行爲。通過修正:

    ColorWindow() : canColor(false), container(nullptr) { 
    
  • writeStringWithColor末缺少return聲明,也導致未定義行爲main。通過:

    return true; 
    

    writeStringWithColor的末尾。

  • 您的xy參數在調用中被換成mvwprintwwriteString

    mvwprintw(getContainer(), y, x, message); 
    
  • LINESCOLS僅ncurses的初始化後有效,所以你startystartx值是垃圾:由固定。由ncurses的後移動cw初始化固定在初始化代碼main

    initscr();   /* Start curses mode  */ 
    cbreak();   /* Line buffering disabled, Pass on 
            * everything to me  */ 
    keypad(stdscr, TRUE); 
    start_color(); 
    
    ColorWindow cw = ColorWindow(); 
    

全部程序:

#include <ncurses.h> 
#include <string.h> 
#include <string> 
#include <unistd.h> 
#include <iostream> 

using namespace std; 

class ColorWindow { 
    private: 
     bool canColor; 
     WINDOW* container; 
     int height, width, startx, starty; 

    public: 
     ColorWindow() : canColor(false), container(nullptr) { 
      if(has_colors()) { 
       canColor=true; 
      } 

      this->height = 20; 
      this->width = 84; 
      this->starty = (LINES - height)/2; /* Calculating for a center placement */ 
      this->startx = (COLS - width)/2; 
     } 

     bool writeStringWithColor(int x, int y, const char* message) { 
      if(!canColor) { 
       writeString(3, 5, "Sorry, your term can't show colors."); 
       return false; 
      } 

      init_pair(1, COLOR_RED, COLOR_BLACK); 

      writeString(0, 10, "aaaaaaaaa"); 

      wattron(getContainer(), COLOR_PAIR(1)); 
      writeString(x, y, message); 
      wattroff(getContainer(), COLOR_PAIR(1)); 
      return true; 
     } 

     void writeString(int x, int y, const char* message) { 
      mvwprintw(getContainer(), y, x, message); 
     } 

     WINDOW* createNewContainer() { 
      this->container = newwin(height, width, starty, startx); 
      wrefresh(this->container);  /* Show that box  */ 

      return getContainer(); 
     } 

     WINDOW* getContainer() { 
      return this->container; 
     } 

     void refreshContainer() { 
      refresh(); 
      wrefresh(this->container);  /* Show that box  */ 
     }  
}; 

int main() { 
    initscr();   /* Start curses mode  */ 
    cbreak();   /* Line buffering disabled, Pass on 
         * everything to me  */ 
    keypad(stdscr, TRUE); 
    start_color(); 

    ColorWindow cw = ColorWindow(); 

    cw.createNewContainer(); 

    bool success = cw.writeStringWithColor(0, 10, "Hello everyone in color!!"); 
    if(!success) 
     cw.writeString(0, 10, "Write with color failed :("); 
    cw.refreshContainer(); 
    sleep(2); 
    endwin(); 

    return 0; 
} 
+0

仍然不存在(構造函數有缺陷)。 –

+0

@ThomasDickey嗯,這個版本至少在這裏產生可見的輸出。少了什麼東西? – melpomene

+0

@melpomene恩,這不適合我。它顯示與我的測試程序相同的blanc控制檯。對我來說沒有可見的輸出。它沒有任何意義,因爲我已經嘗試了另一個簡單的例子,它顯示了豐富多彩的輸出。 – Shirkam