使用terminfo的终端颜色? [英] Terminal color using terminfo?
问题描述
我正在编写一个C ++类,允许在终端中使用颜色.我希望它可以在每个终端上运行:
- 在支持该功能的终端上以真彩色(24位)打印
- 在支持它的终端上具有256色(6x6x6),
- 其他16种基本颜色.
我曾经使用termcap编写过一次C函数,并且我想在这种情况下使用它.但是,手册页显示:
termcap数据库是一种过时的工具,用于描述字符单元终端和打印机的功能.它仅保留用于旧程序的功能.新的应该使用terminfo数据库和相关的库.
因此,我尝试使用terminfo,但找不到如何执行此操作.我的系统中没有terminfo.h(我在Debian上运行).
我的问题是:
如何使用最新工具(例如,根据manpage,不是termcap)获得C/C ++中当前端子的颜色可能性?
简短的回答是,直到 ncurses 6.1 于2018年1月发布,您才能从terminfo获取信息.
更长的答案:
- 要有效使用TrueColor,您需要一个接口来处理3个参数( red , green , blue ).Termcap无法做到这一点.Terminfo可以处理多个参数,但是...
- 没有像TrueColor这样的标准终端功能 (功能的名称,可以是布尔值,数字或字符串).
- 您可以调整现有功能,但是它们有局限性
查看 terminfo(5)手册,您可能会看到这些(字符串):
initialize_color initc Ic初始化颜色#1至(#2,#3,#4)initialize_pair initp Ip初始化颜色配对#1至fg =(#2,#3,#4),bg =(#5,#6,#7)
与这些(数字)有关的
:
max_colors colors Co的最大数量屏幕上的颜色max_pairs对pa的最大数量上的颜色对屏幕
与这些颜色兼容的ANSI颜色和方案(例如16、88和256色)假定您正在对对中的前景色和背景色进行着色.原因是很久以前,硬件终端就是这样工作的. initialize_color
功能用于另一种方案(Tektronix),这似乎很有用.
但是,terminfo是编译的,因此生成的二进制文件仅存储有符号的16位整数.您不能使用终端描述为24位颜色存储合适的 max_pairs
或 max_colors
.(termcap将所有内容存储为字符串,但如上所述不适用于此应用程序.)
首次编写此问题和答案的几年后,terminfo进行了更新,以使用一种新的文件格式,该文件格式使用带符号的32位整数,足以表示24位RGB颜色的颜色数量./p>
更多详细信息可以在 ncurses 6.1 的发行公告中找到.更新的 term(5)
手册页,后者会指出,某些直接访问terminfo数据的应用程序所使用的旧API仍然存在一些限制.
进一步阅读:
- 为什么只有16(或256)种颜色?(ncurses常见问题解答)
- 我可以通过颜色编号设置颜色吗?(xterm常见问题解答)
- ncurses terminfo是否与我的系统兼容?(ncurses常见问题解答)
I am writing a C++ class permitting the usage of color in terminal. I want it works on every terminal :
- printing with true color (24 bits) on terminals that supports it,
- with 256 color (6x6x6) on terminals that supports it,
- else with the basic 16 colors.
I wrote once C functions using termcap, and I thought using it in this case. However, the man page says :
The termcap database is an obsolete facility for describing the capabilities of character-cell terminals and printers. It is retained only for capability with old programs; new ones should use the terminfo database and associated libraries.
So I tried to use terminfo, but I could not find how to do this. There is not terminfo.h in my system (I run on Debian).
My questions is :
How can I get the color possibilities of the current terminal in C/C++, using the newest tools (ie not termcap, according to manpage) ?
The short answer is that you could not get the information from terminfo until ncurses 6.1 was released in January 2018.
The longer answer:
- to effectively use TrueColor, you need an interface handling 3 parameters (for red, green, blue). Termcap cannot do this. Terminfo can handle multiple parameters, but...
- there is no standard terminal capability (a name for a feature which may be a boolean, number or a string) dealing with TrueColor as such.
- you could adapt existing capabilities, but they have limitations
Looking at the terminfo(5) manual, you might see these (strings):
initialize_color initc Ic initialize color #1
to (#2,#3,#4)
initialize_pair initp Ip Initialize color
pair #1 to
fg=(#2,#3,#4),
bg=(#5,#6,#7)
which are related to these (numbers):
max_colors colors Co maximum number of
colors on screen
max_pairs pairs pa maximum number of
color-pairs on the
screen
ANSI colors and schemes compatible with those (such as 16-, 88- and 256-colors) assume you are coloring foreground and background in pairs. The reason for that was that long ago, hardware terminals just worked that way. The initialize_color
capability is for a different scheme (Tektronix), which might seem useful.
However, terminfo is compiled, and the resulting binary files stored only signed 16-bit integers. You could not use the terminal description to store a suitable max_pairs
or max_colors
for 24-bit color. (termcap stores everything as strings, but as noted is unsuited for this application).
A couple of years after this question and answer were first written, terminfo was updated to use a new file format that uses signed 32-bit integers, which is enough for expressing the number of colours in 24-bit RGB colour.
More details can be found in release announcement for ncurses 6.1 and in the updated term(5)
manual page, which latter notes that there are still restrictions in the old API used by some applications that access the terminfo data directly.
Further reading:
- Why only 16 (or 256) colors? (ncurses FAQ)
- Can I set a color by its number? (xterm FAQ)
- Is ncurses terminfo compatible with my system? (ncurses FAQ)
这篇关于使用terminfo的终端颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!