简体   繁体   English

获取Perl中的当前终端颜色对

[英]Get current terminal color pair in Perl

I'm trying to learn about color text in a terminal window. 我正在尝试在终端窗口中了解彩色文本。 (In case it matters I'm using Terminal.app on OS X.) I'd like to get the terminal's current foreground and background color pair. (如果重要的是我在OS X上使用Terminal.app。)我想得到终端的当前前景色和背景色对。 It looks like I should be able to get this info in a perl script using the Term::Cap library, but the solution eludes me. 看起来我应该能够使用Term :: Cap库在perl脚本中获取此信息,但解决方案使我无法解决。

In a perl script how would I query the terminal's current foreground and background color pair value? 在perl脚本中,如何查询终端的当前前景和背景颜色对值?

The feature is outside the scope of terminfo and termcap, because it deals with terminal responses , while terminfo/termcap describe these capabilities: 该功能超出了terminfo和termcap的范围,因为它处理终端响应 ,而terminfo / termcap描述了这些功能:

  • how to tell the terminal to do some commonly-implemented feature (such as clearing the screen), or 如何告诉终端做一些常用功能 (如清除屏幕),或者
  • what sequence of characters might some special key (such as Home ) send from the keyboard. 可以从键盘发送一些特殊键 (例如Home )的字符序列。

While in principle, there is no limitation on what could be part of a terminal description, there was little commonality across terminals back in the 1980s for responses . 虽然原则上对终端描述中可能存在的内容没有限制,但在20世纪80年代,各种终端之间几乎没有共同点用于响应 A few terminals could report specific features, most of those were constant (eg, version information). 一些终端可以报告特定功能,其中大多数是不变的(例如,版本信息)。 Most of the variable responses came after terminfo/termcap had more or less solidified in X/Open Curses. 大多数变量响应来自terminfo / termcap在X / Open Curses中或多或少地固化。 ncurses extends that , but again, most of the extensions are either features or special keys . ncurses扩展了这一点 ,但同样,大多数扩展都是功能特殊键

Terminal.app implements the most commonly-used features of xterm , but (like other imitators) omits most of the terminal responses. Terminal.app实现了xterm最常用的功能,但(像其他模仿者一样)省略了大多数终端响应。 Among other things, xterm provides terminal responses which can tell an application what the window's colors are currently. 除此之外, xterm提供了终端响应,可以告诉应用程序窗口的颜色当前是什么。 There are a couple of command-line utilities ( xtermset and xtermcontrol ) which have been written to use this information (and again, they cover only a part of the repertoire). 有几个命令行实用程序( xtermsetxtermcontrol )已被编写以使用此信息(同样,它们仅涵盖了所有指令的一部分)。 Using xtermcontrol demonstrates that Terminal.app is lacking in this area — see screenshot: 使用xtermcontrol演示了Terminal.app在这个领域缺乏 - 见截图:

使用xtermcontrol显示Terminal.app vs xterm的屏幕截图

I don't think most terminals support reporting this -- and it doesn't look like termcap or terminfo have any entries for it. 我不认为大多数终端都支持报告这个 - 并且它看起来不像termcap或terminfo有任何条目。 You're just expected to set the color pair as necessary, not to ask the terminal what it's set to right now. 您只需要根据需要设置颜色对,而不是询问终端现在设置的颜色。 In the ECMA-48 standard (better known as "ANSI" after ANSI X3.64, where it used to live), the only command that makes reference to color is SGR "Set Graphic Rendition", which is purely write-only. 在ECMA-48标准中(在ANSI X3.64之后更为人所知的“ANSI”,以前它曾经存在),唯一引用颜色的命令是SGR“Set Graphic Rendition”,它纯粹是只写的。

Dunno about perl or Terminal.app, but xterm etc will write foreground/background color control sequences to stdin if you output "\\033]10;?\\07" or "\\033]11;?\\07" respectively. Dunno关于perl或Terminal.app,但xterm等会将前景/背景颜色控制序列写入stdin,如果你分别输出"\\033]10;?\\07""\\033]11;?\\07" Check out http://invisible-island.net/xterm/ctlseqs/ctlseqs.html , http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Operating-System-Controls in particular. 退房http://invisible-island.net/xterm/ctlseqs/ctlseqs.htmlhttp://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Operating-System-Controls尤其如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM