【问题标题】:Linux terminal not displaying anything with ncursesLinux 终端不显示任何带有 ncurses 的内容
【发布时间】:2016-07-23 20:13:13
【问题描述】:

我正在尝试创建一个库来简化 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;
}

提前致谢。

【问题讨论】:

  • canColor 在我看来可能未初始化。 container 看起来也未初始化。
  • 如果是这样,它会显示“对不起,你的术语 [...]”,但它会显示任何内容,甚至是我在那里写的“aaaaaa”字符串。

标签: c++ ncurses


【解决方案1】:

您的代码中有几个错误:

  • 您没有初始化canColorcontainer,因此将字段复制到main 中的cw 具有未定义的行为。修复者:

    ColorWindow() : canColor(false), container(nullptr) {
    
  • writeStringWithColor 在末尾缺少 return 语句,这也会导致 main 中的未定义行为。修复者:

    return true;
    

    writeStringWithColor 结尾。

  • 您的xy 参数在writeString 中对mvwprintw 的调用中交换。修复者:

    mvwprintw(getContainer(), y, x, message);
    
  • LINESCOLS 仅在 ncurses 初始化后有效,因此您的 startystartx 值是垃圾。通过将 cw 的初始化移动到 main 中的 ncurses 初始化代码之后来修复:

    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;
}

【讨论】:

  • 仍然不存在(构造函数有缺陷)。
  • @ThomasDickey 好吧,这个版本至少在这里产生了可见的输出。缺少什么?
  • @melpomene 好吧,这对我不起作用。它显示与我的测试程序相同的 blanc 控制台。对我来说没有可见的输出。它没有意义,因为我尝试了另一个简单的例子,它显示了一个彩色的输出。
猜你喜欢
  • 1970-01-01
  • 2011-12-31
  • 2013-09-03
  • 2018-12-22
  • 1970-01-01
  • 2021-12-15
  • 1970-01-01
  • 2021-10-23
  • 1970-01-01
相关资源
最近更新 更多