C Gnome终端无法在使用诅咒的程序中打印红色

C Gnome终端无法在使用诅咒的程序中打印红色,c,ubuntu,terminal,ncurses,gnome,C,Ubuntu,Terminal,Ncurses,Gnome,我正试图进入ncurses图书馆。我很失望Gnome终端可以通过ANSI转义字符打印红色,但不能从ncurses打印预定义的红色 #包括 void init_colors(){ INTA; 对于(a=1;a

我正试图进入ncurses图书馆。我很失望Gnome终端可以通过ANSI转义字符打印红色,但不能从ncurses打印预定义的红色

#包括
void init_colors(){
INTA;
对于(a=1;a
这应该以任何默认的ncurses颜色打印单词“color”,但是第二行(
init\u pair
应该将第二个
color\u pair
初始化为
red
)根本不打印。Gnome终端似乎只是跳过了这一行。我如何修复此问题?

这是

#包括
内部主(空){
initscr();
启动颜色();
初始对(1,颜色为黑色,颜色为红色);
初始对(2,颜色为黑色,颜色为绿色);
attron(颜色对(1));
printw(“这应该用红色背景的黑色打印!\n”);
attron(颜色对(2));
printw(“这是绿色背景!\n”);
刷新();
getch();
endwin();
}
笔记
  • 您需要在
    initscr()之后调用
    start\u color()
    ,才能使用颜色
  • 您必须使用
    COLOR\u对
  • 将分配有
    init_pair
    的颜色对传递给
    attron
  • 无法使用颜色对0
  • 只需调用
    refresh()
    一次
  • 只有当你想让别人看到你的输出时
  • 而且您没有调用像
    getch()
    这样的输入函数

您可以打印其他颜色吗?还是只有红色不起作用?@MooingDuck我可以毫无问题地打印任何其他预定义颜色:包括黑色、绿色、黄色、蓝色、品红、青色和白色。这可能是本地问题或gnome终端中的设置。它在这里与3.14.2一起工作。我在这里看不到任何OPOP程序中没有的步骤。OP的程序本身工作正常;一个有用的答案可能是评论颜色主题的一些怪癖,或者提到一个已知的bug(来自bug报告)。对于前者,想象是不够的。
#include <curses.h>

void init_colors() {
    int a;
    for(a = 1; a < COLORS; a++) {
        init_pair(a, a, COLOR_BLACK);
    }
}

void print_colors() {
    int a;
    for(a = 1; a < COLORS; a++) {
        attron(COLOR_PAIR(a));
        mvprintw(a, 0, "color");
        attroff(COLOR_PAIR(a));
    }
}

int main() {
    initscr();
    cbreak();
    noecho();
    curs_set(0);

    if(has_colors()) 
        start_color();          

    init_colors();
    print_colors();
    getch();
    endwin();

    return 0;
}
#include <curses.h>

int main(void) {
    initscr();
    start_color();

    init_pair(1, COLOR_BLACK, COLOR_RED);
    init_pair(2, COLOR_BLACK, COLOR_GREEN);

    attron(COLOR_PAIR(1));
    printw("This should be printed in black with a red background!\n");

    attron(COLOR_PAIR(2));
    printw("And this in a green background!\n");
    refresh();

    getch();

    endwin();
}