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