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

发布于 2024-10-17 04:09:42 字数 166 浏览 6 评论 0原文

我正在尝试了解终端窗口中的彩色文本。 (如果重要的话,我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅语花开 2024-10-24 04:09:42

该功能超出了 terminfo 和 termcap 的范围,因为它处理终端响应,而 terminfo/termcap 描述了这些功能:

  • 如何告诉终端执行一些常用的功能 em>(例如清除屏幕),或者
  • 某些特殊键(例如Home)可以从键盘发送什么字符序列。

虽然原则上,终端描述中的内容没有任何限制,但早在 20 世纪 80 年代,终端之间的响应就几乎没有共同点。少数终端可以报告特定功能,其中大多数是不变的(例如版本信息)。大多数可变响应是在 terminfo/termcap 或多或少在 X/Open Curses 中固化之后出现的。 ncurses extends that, but again, most of the扩展可以是功能特殊键

Terminal.app 实现了 xterm 最常用的功能,但(与其他模仿者一样)省略了大部分终端响应。除此之外,xterm 提供终端响应,可以告诉应用程序当前窗口的颜色是什么。有几个命令行实用程序(xtermset xtermcontrol) 是为了使用这些信息而编写的(同样,它们只涵盖了曲目的一部分)。使用 xtermcontrol 表明 Terminal.app 在这方面缺乏 - 请参阅屏幕截图:

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

The feature is outside the scope of terminfo and termcap, because it deals with terminal responses, while terminfo/termcap describe these capabilities:

  • 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.

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 and xtermcontrol) which have been written to use this information (and again, they cover only a part of the repertoire). Using xtermcontrol demonstrates that Terminal.app is lacking in this area — see screenshot:

screenshot showing Terminal.app vs xterm using xtermcontrol

GRAY°灰色天空 2024-10-24 04:09:42

我认为大多数终端都不支持报告这一点——而且 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.

甜扑 2024-10-24 04:09:42

不知道 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文