ncurses的has_colors()返回false。问题是什么?

问题描述 投票:0回答:1

我想在ncurses模式下使用颜色属性。但正如在主题中所述,has_colors()返回false,这意味着终端不支持颜色操作。但是使用printf()和ansi颜色代码(不是在ncurses模式下),我可以打印彩色文本。我认为这意味着终端支持颜色操作。我错了吗?问题是什么?

这是我使用的代码:

int main()
{
    initscr();

    if (has_colors() == false)
    {
        endwin();
        printf("No color support!\n");
        return -1;
    }

    start_color();
    init_pair(1, COLOR_RED, COLOR_BLACK);

    attron(COLOR_PAIR(1));
    printw("This is a test!");
    attroff(COLOR_PAIR(1));

    endwin();

    return 0;
}
c colors ncurses ansi
1个回答
1
投票

ncurses中的终端配置来自两种风格,具体取决于是否使用terminfo支持或termcap支持编译ncurses。

无论哪种方式,has_colors()都是一个函数,它根据您用于TERM环境变量的值来查询curses数据库(终端根据终端的行为控制数据库)。

xterm的值可能不包括颜色控件,可能是什么使您的终端软件无法显示颜色。只需将您的TERM变量更改为:

TERM=xterm-color

然后再试一次。该终端定义包括颜色支持并使has_colors()返回true

© www.soinside.com 2019 - 2024. All rights reserved.