在 Perl 中获取当前终端颜色对
我正在尝试了解终端窗口中的彩色文本。 (如果重要的话,我在 OS X 上使用 Terminal.app。)我想获取终端当前的前景色和背景色对。看起来我应该能够使用 Term::Cap 库在 perl 脚本中获取此信息,但解决方案却让我困惑。
在 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. 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.
In a perl script how would I query the terminal's current foreground and background color pair value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该功能超出了 terminfo 和 termcap 的范围,因为它处理终端响应,而 terminfo/termcap 描述了这些功能:
虽然原则上,终端描述中的内容没有任何限制,但早在 20 世纪 80 年代,终端之间的响应就几乎没有共同点。少数终端可以报告特定功能,其中大多数是不变的(例如版本信息)。大多数可变响应是在 terminfo/termcap 或多或少在 X/Open Curses 中固化之后出现的。 ncurses extends that, but again, most of the扩展可以是功能或特殊键。
Terminal.app 实现了
xterm
最常用的功能,但(与其他模仿者一样)省略了大部分终端响应。除此之外,xterm
提供终端响应,可以告诉应用程序当前窗口的颜色是什么。有几个命令行实用程序(xtermset
和xtermcontrol
) 是为了使用这些信息而编写的(同样,它们只涵盖了曲目的一部分)。使用xtermcontrol
表明 Terminal.app 在这方面缺乏 - 请参阅屏幕截图:The feature is outside the scope of terminfo and termcap, because it deals with terminal responses, while terminfo/termcap describe these capabilities:
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. A few terminals could report specific features, most of those were constant (e.g., version information). Most of the variable responses came after terminfo/termcap had more or less solidified in X/Open Curses. ncurses extends that, but again, most of the extensions are either features or special keys.
Terminal.app implements the most commonly-used features of
xterm
, but (like other imitators) omits most of the terminal responses. Among other things,xterm
provides terminal responses which can tell an application what the window's colors are currently. There are a couple of command-line utilities (xtermset
andxtermcontrol
) which have been written to use this information (and again, they cover only a part of the repertoire). Usingxtermcontrol
demonstrates that Terminal.app is lacking in this area — see screenshot:我认为大多数终端都不支持报告这一点——而且 termcap 或 terminfo 似乎没有任何相关条目。您只需根据需要设置颜色对,而不是询问终端现在的设置。在 ECMA-48 标准(在 ANSI X3.64 之后,更广为人知的名称是“ANSI”)中,唯一引用颜色的命令是 SGR“Set Graphic Rendition”,它纯粹是只写的。
I don't think most terminals support reporting this -- and it doesn't look like termcap or terminfo have any entries for it. 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.
不知道 perl 或 Terminal.app,但如果您输出
"\033]10;?\07"
或"\033]11,xterm 等会将前景色/背景颜色控制序列写入 stdin ;?\07"
分别。查看http://invisible-island.net/xterm/ctlseqs/ctlseqs.html, http://invisible-island.net/特别是 xterm/ctlseqs/ctlseqs.html#h2-Operating-System-Controls 。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. Check out http://invisible-island.net/xterm/ctlseqs/ctlseqs.html, http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Operating-System-Controls in particular.