ncurses 的 has_colors() 返回 false.问题是什么? [英] ncurses' has_colors() returns false. What is the problem?

查看:30
本文介绍了ncurses 的 has_colors() 返回 false.问题是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to use color attributes in ncurses mode. But as stated in the topic, has_colors() returns false, meaning that the terminal does not support color manipulation. But using printf() and ansi color codes (not in ncurses mode), I can print color texts. I think this means that the terminal supports color manipulation. Am I wrong? What is the problem?

这是我使用的代码:

int main()
{
    initscr();

    if (has_colors() == false)
    {
        endwin();
        printf("No color support!
");
        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;
}

推荐答案

ncurses 中的终端配置有两种风格,具体取决于 ncurses 是使用 terminfo 支持还是 termcap 支持编译的.

Terminal configuration in ncurses comes from two flavours, depending on if ncurses was compiled with terminfo support or termcap support.

无论哪种方式,has_colors() 都是一个函数,它根据您用于 TERM环境变量.

Either way, has_colors() is a function that consults the curses database (a terminal controls database based on how your terminal behaves) based on the value you use for your TERM environment variable.

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

A value of xterm is possible to include no color controls and probably what is making your terminal software not able to display colors. Just change your TERM variable to something like:

TERM=xterm-color

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

and try again. That terminal definition includes color support and makes has_colors() to return true.

来自我自己的 termcap 文件(基于 FreeBSD 12.1 系统):

from my own termcap file (based on a FreeBSD 12.1 system):

xterm-clear:
        :te=E[?1049l:ti=E[?1049h:
        :tc=xterm-new:
xterm-new|modern xterm:
        :@7=EOF:@8=EOM:F1=E[23~:F2=E[24~:K2=EOE:Km=E[M:
        :k1=EOP:k2=EOQ:k3=EOR:k4=EOS:k5=E[15~:k6=E[17~:
        :k7=E[18~:k8=E[19~:k9=E[20~:k;=E[21~:kI=E[2~:
        :kN=E[6~:kP=E[5~:kd=EOB:kh=EOH:kl=EOD:kr=EOC:ku=EOA:
        :tc=xterm-basic:
#
# This chunk is used for building the VT220/Sun/PC keyboard variants.
xterm-basic|modern xterm common:
        :am:bs:km:mi:ms:ut:xn:AX:
        :Co#8:co#80:kn#12:li#24:pa#64:
        :AB=E[4%dm:AF=E[3%dm:AL=E[%dL:DC=E[%dP:DL=E[%dM:
        :DO=E[%dB:LE=E[%dD:RI=E[%dC:UP=E[%dA:ae=E(B:al=E[L:
        :as=E(0:bl=^G:cd=E[J:ce=E[K:cl=E[HE[2J:
        :cm=E[%i%d;%dH:cs=E[%i%d;%dr:ct=E[3g:dc=E[P:dl=E[M:
        :ei=E[4l:ho=E[H:im=E[4h:is=E[!pE[?3;4lE[4lE>:
        :kD=E[3~:kb=^H:ke=E[?1lE>:ks=E[?1hE=:kB=E[Z:le=^H:md=E[1m:
        :me=E[m:ml=El:mr=E[7m:mu=Em:nd=E[C:op=E[39;49m:
        :rc=E8:rs=E[!pE[?3;4lE[4lE>:sc=E7:se=E[27m:sf=^J:
        :so=E[7m:sr=EM:st=EH:
        :ue=E[24m:up=E[A:us=E[4m:ve=E[?12lE[?25h:vi=E[?25l:vs=E[?12;25h:

# The xterm-new description has all of the features, but is not completely
# compatible with vt220.  If you are using a Sun or PC keyboard, set the
# sunKeyboard resource to true:
#       + maps the editing keypad
#       + interprets control-function-key as a second array of keys, so a
#         12-fkey keyboard can support vt220's 20-fkeys.
#       + maps numeric keypad "+" to ",".
#       + uses DEC-style control sequences for the application keypad.
#
xterm-vt220|xterm emulating vt220:
        :*6=E[4~:@7=E[4~:K2=EOu:Km=E[M:kH=E[4~:kh=E[1~:
        :tc=xterm-basic:

xterm-24|xterms|vs100|24x80 xterm:
        :li#24:tc=xterm-old:
xterm-65|65x80 xterm:
        :li#65:tc=xterm-old:
xterm-bold|xterm with bold for underline:
        :so=E[7m:us=E[1m:tc=xterm-old:
xterm-boldso|xterm with bold for standout:
        :se=E[m:so=E[1m:tc=xterm-old:
xterm-mono|monochrome xterm:
        :ut@:
        :Co@:NC@:kn#20:pa@:
        :AB@:AF@:Sb@:Sf@:op@:st@:tc=xterm-old:
#
# Alternate terminal description that "works" for interactive shells such as
# tcsh and bash.
xterm-noapp|xterm with cursor keys in normal mode:
        :kd=E[B:ke=E>:kl=E[D:kr=E[C:ks=E=:ku=E[A:te@:ti@:
        :tc=xterm:
#
# This should work for the commonly used "color xterm" variations (XFree86
# xterm, color_xterm, nxterm, rxvt).  Note that it does not set 'bce', so for
# XFree86 and rxvt, some applications that use colors will be less efficient,
# and in a few special cases (with "smart" optimization) the wrong color will
# be painted in spots.
xterm-color|generic "ANSI" color xterm:
        :Co#8:NC@:pa#64:
        :AB=E[4%dm:AF=E[3%dm:ac=:op=E[m:tc=xterm-r6:
#
# These aliases are for compatibility with the terminfo; termcap cannot provide
# the extra features such as color initialization, but termcap applications
# still want the names.
xterm-16color|xterm alias 1:
        :tc=xterm-new:

xterm-88color|xterm alias 2:
        :Co#88:pa#7744:tc=xterm-256color:

xterm-256color|xterm alias 3:
        :Co#256:pa#32767:
        :AB=E[48;5;%dm:AF=E[38;5;%dm:tc=xterm-new:

xterm-nrc|xterm alias 4:
        :tc=xterm:
xterm-rep|xterm alias 5:
        :tc=xterm:
xterm-xmc|xterm alias 6:
        :sg#1:tc=xterm:
#
# An 8-bit description is doable with termcap, but there are probably no
# termcap (or BSD curses) applications that are able to use it.
#
# Add the capability to "clear the screen" after exiting vi, more/less, etc.
#
xterm-8bit-clear:
        :te=233?1049l:ti=233?1049h:ue=23324m:
        :tc=xterm-8bit:
xterm-8bit|xterm terminal emulator 8-bit controls (X Window System):
        :am:km:mi:ms:xn:
        :co#80:it#8:li#24:
        :AL=233%dL:DC=233%dP:DL=233%dM:DO=233%dB:IC=233%d@:
        :K2=217y:Km=233M:LE=233%dD:RI=233%dC:UP=233%dA:
        :ae=E(B:al=233L:as=E(0:bl=^G:bt=233Z:cd=233J:ce=233K:
        :cl=233H2332J:cm=233%i%d;%dH:cr=^M:cs=233%i%d;%dr:
        :ct=2333g:dc=233P:dl=233M:do=^J:ei=2334l:ho=233H:
        :im=2334h:
        :is=E[62"pE G233m233?7hE>E7233?1;3;4;6l2334l233rE8:
        :k1=23311~:k2=23312~:k3=23313~:k4=23314~:k5=23315~:
        :k6=23317~:k7=23318~:k8=23319~:k9=23320~:kD=2333~:
        :kI=2332~:kN=2336~:kP=2335~:kb=^H:kd=217B:
        :ke=233?1lE>:kh=2331~:kl=217D:kr=217C:ks=233?1hE=:
        :ku=217A:le=^H:mb=2335m:md=2331m:me=233m:mr=2337m:
        :nd=233C:rc=E8:sc=E7:se=23327m:sf=^J:so=2337m:sr=215:
        :st=210:ta=^I:ue=23324m:
        :up=233A:us=2334m:vb=233?5h233?5l:ve=233?25l233?25h:
        :vs=233?12;25h:vi=233?25l:
#
xterm-hp|xterm with hpterm function keys:
        :@7=EF:k1=Ep:k2=Eq:k3=Er:k4=Es:k5=Et:k6=Eu:k7=Ev:
        :k8=Ew:kC=EJ:kD=EP:kI=EQ:kN=ES:kP=ET:kd=EB:kh=Eh:
        :kl=ED:kr=EC:ku=EA:tc=xterm-basic:
#
xterm-sco|xterm with SCO function keys:
        :@7=E[F:F1=E[W:F2=E[X:F3=E[Y:F5=E[a:F6=E[b:F7=E[c:
        :F8=E[d:F9=E[e:FA=E[f:FB=E[g:FC=E[h:FD=E[i:FE=E[j:
        :FF=E[k:ac=:k1=E[M:k2=E[N:k3=E[O:k4=E[P:k5=E[Q:
        :k6=E[R:k7=E[S:k8=E[T:k9=E[U:k;=E[V:kD=177:kI=E[L:
        :kN=E[G:kP=E[I:kd=E[B:kh=E[H:kl=E[D:kr=E[C:ku=E[A:
        :tc=xterm-basic:
#
xterm-vt52|xterm emulating vt52:
        :bs:
        :co#80:it#8:li#24:
        :ae=EG:as=EF:bl=^G:cd=EJ:ce=EK:cl=EHEJ:cm=EY%+ %+ :
        :cr=^M:do=EB:ho=EH:kb=^H:kd=EB:kl=ED:kr=EC:ku=EA:
        :le=ED:nd=EC:nw=^M^J:sf=^J:sr=EI:ta=^I:up=EA:
#
xterm-sun|xterm with Sun functionkeys:
        :%1=E[196z:&8=E[195z:@0=E[200z:@5=E[197z:@7=E[220z:
        :F1=E[192z:F2=E[193z:K2=E[218z:Km=E[M:k1=E[224z:
        :k2=E[225z:k3=E[226z:k4=E[227z:k5=E[228z:k6=E[229z:
        :k7=E[230z:k8=E[231z:k9=E[232z:k;=E[233z:kD=E[3z:
        :kI=E[2z:kN=E[222z:kP=E[216z:kh=E[214z:
        :tc=xterm-basic:
#
# vi may work better with this entry, because vi doesn't use insert mode much.
# |xterm-ic|xterm-vi|xterm with insert character instead of insert mode:
xterm-ic|xterm-vi|xterm with insert char:
        :mi@:
        :IC=E[%d@:ei@:ic=E[@:im@:tc=xterm:
#
# Compatible with the X11R6.3 xterm
#
# Add the capability to "clear the screen" after exiting vi, more/less, etc.
#
xterm-r6-clear:
        :te=E[2JE[?47lE8:ti=E7E[?47h:ue=E[m:
        :tc=xterm-r6:
xterm-r6|xterm-old|X11R6 xterm:
        :am:bs:km:mi:ms:pt:xn:
        :co#80:kn#20:li#24:
        :*6=E[4~:@0=E[1~:@7=E[4~:AL=E[%dL:DC=E[%dP:DL=E[%dM:
        :DO=E[%dB:F1=E[23~:F2=E[24~:F3=E[25~:F4=E[26~:
        :F5=E[28~:F6=E[29~:F7=E[31~:F8=E[32~:F9=E[33~:
        :FA=E[34~:LE=E[%dD:RI=E[%dC:UP=E[%dA:ae=^O:al=E[L:
        :as=^N:bl=^G:cd=E[J:ce=E[K:cl=E[HE[2J:cm=E[%i%d;%dH:
        :cs=E[%i%d;%dr:ct=E[3g:dc=E[P:dl=E[M:eA=E)0:ei=E[4l:
        :ho=E[H:im=E[4h:
        :is=E[mE[?7hE[4lE>E7E[rE[?1;3;4;6lE8:
        :k1=E[11~:k2=E[12~:k3=E[13~:k4=E[14~:k5=E[15~:
        :k6=E[17~:k7=E[18~:k8=E[19~:k9=E[20~:k;=E[21~:
        :kD=E[3~:kI=E[2~:kN=E[6~:kP=E[5~:kb=^H:kd=EOB:
        :ke=E[?1lE>:kh=E[1~:kl=EOD:kr=EOC:ks=E[?1hE=:
        :ku=EOA:md=E[1m:me=E[m:ml=El:mr=E[7m:mu=Em:nd=E[C:
        :rc=E8:rs=E[mE[?7hE[4lE>E7E[rE[?1;3;4;6lE8:
        :sc=E7:se=E[m:sf=^J:so=E[7m:sr=EM:
        :ue=E[m:up=E[A:us=E[4m:
#
# Compatible with the R5 xterm
#
# Add the capability to "clear the screen" after exiting vi, more/less, etc.
#
xterm-r5-clear:
        :te=E[2JE[?47lE8:ti=E7E[?47h:ue=E[m:
        :tc=xterm-r5:
xterm-r5|X11R5 xterm X11R5:
        :am:bs:km:mi:ms:pt:xn:
        :co#80:kn#4:li#24:
        :@7=E[4~:AL=E[%dL:DC=E[%dP:DL=E[%dM:DO=E[%dB:
        :IC=E[%d@:UP=E[%dA:al=E[L:cd=E[J:ce=E[K:cl=E[HE[2J:
        :cm=E[%i%d;%dH:cs=E[%i%d;%dr:ct=E[3g:dc=E[P:dl=E[M:
        :ei=E[4l:ho=E[H:im=E[4h:
        :is=E[rE[mE[2JE[HE[?7hE[?1;3;4;6lE[4l:
        :k1=E[11~:k2=E[12~:k3=E[13~:k4=E[14~:kb=^H:kd=EOB:
        :ke=E[?1lE>:kh=E[1~:kl=EOD:kr=EOC:ks=E[?1hE=:
        :ku=EOA:md=E[1m:me=E[m:mr=E[7m:nd=E[C:rc=E8:
        :rs=E>E[?1;3;4;5;6lE[4lE[?7hE[mE[rE[2JE[H:
        :sc=E7:se=E[m:sf=^J:so=E[7m:sr=EM:
        :up=E[A:us=E[4m:
#
# Customization begins here.
xterm-xfree86|xterm terminal emulator (XFree86):
        :tc=xterm-new:
#
# This is the only entry which you should have to customize, since "xterm"
# is widely used for a variety of incompatible terminal emulations including
# color_xterm and rxvt.
xterm|X11 terminal emulator:
        :tc=xterm-new:
#       :tc=xterm-r6:

这篇关于ncurses 的 has_colors() 返回 false.问题是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆