문제

Hullo - If I want to determine the # of colours a terminal can display I simply read the output of

 tput colors

... however I cannot figure out how to determine if the terminal I'm writing to can support other formatting requests such as bold, underline, reverse-video, blink (yuck!).

I see how to set output via tput, but not determine if the capability exists before attempting.

Should I simply assume the capability exists in all terminals ?

도움이 되었습니까?

해결책

You can use infocmp for this, and grep on the capability you are looking for. If a result is found, then the capability is supported:

infocmp | grep bold
    bel=^G, bold=\E[1m, clear=\E[H\E[2J, cr=^M,

In order to get the names of the different capabilities check out the Linux man pages for terminfo, or this one for a tabular layout of names.

다른 팁

There may be an easier way, but you can query the terminfo database by running

infocmp $TERM

to see the full entry for the current terminal (replace $TERM with the name of any supported terminal type). You'll probably want to read the man page to figure out how to create output most easily parsed for your given need.

If you try a tput command with an undefined attribute it will return a non-zero exit code. You can redirect the normal output, if any, to avoid having it take effect.

for attr in bold rev smul blink sshm
do
    if ! tput "$attr" > /dev/null
    then
        echo "Attribute $attr is undefined"
    fi
done

Otherwise, if you grep the output of infocmp, use the -1 (that's a one) option to output the attributes one per line.

infocmp -1 | grep smul
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top