加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

unix – 如何确定终端是否具有颜色能力?

发布时间:2020-12-15 19:50:16 所属栏目:安全 来源:网络整理
导读:我想更改一个程序来自动检测终端是否具有颜色能力,所以当我从一个无色能的终端(比如(X)Emacs中的Mx shell)中运行该程序时,颜色会自动关闭。 我不想硬编码程序来检测TERM = {emacs,dumb}。 我认为termcap / terminfo应该能够帮助这个,但到目前为止,我只
我想更改一个程序来自动检测终端是否具有颜色能力,所以当我从一个无色能的终端(比如(X)Emacs中的Mx shell)中运行该程序时,颜色会自动关闭。

我不想硬编码程序来检测TERM = {emacs,dumb}。

我认为termcap / terminfo应该能够帮助这个,但到目前为止,我只是拼凑了这个(n)诅咒 – 使用的代码片段,当它找不到终端时,它会失败:

#include <stdlib.h>
#include <curses.h>

int main(void) {
 int colors=0;

 initscr();
 start_color();
 colors=has_colors() ? 1 : 0;
 endwin();

 printf(colors ? "YESn" : "NOn");

 exit(0);
}

即我得到这个:

$ gcc -Wall -lncurses -o hep hep.c
$ echo $TERM
xterm
$ ./hep
YES
$ export TERM=dumb
$ ./hep           
NO
$ export TERM=emacs
$ ./hep            
Error opening terminal: emacs.
$

这是…次优。

一个朋友指着我(t)(1),我煮了这个解决方案:
#!/bin/sh

# ack-wrapper - use tput to try and detect whether the terminal is
#               color-capable,and call ack-grep accordingly.

OPTION='--nocolor'

COLORS=$(tput colors 2> /dev/null)
if [ $? = 0 ] && [ $COLORS -gt 2 ]; then
    OPTION=''
fi

exec ack-grep $OPTION "$@"

这对我有用如果我有办法把它整合到ack,那将是巨大的。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读