以编程方式访问当前的 xterm 背景颜色?

Mis*_*ble 14 xterm

我想.bashrc根据它用于前景和背景的颜色来设置提示颜色。

例如,背景浅色提示蓝色,深色提示米色。

有没有办法找出脚本中的当前设置?

小智 24

Thomas Dickey(xterm 的维护者)回复的电子邮件有这个。特别注意关于 的部分?。的Ps = 4是指OSC Ps ; Pt ST其中OSC(“操作系统控制”前缀)是ESC ]ST(“字符串终结者”后缀)是\(反斜杠)。该4是可能的子命令OSC之一。

对于整个调色板,可以使用 88/256 颜色扩展设置/检索。在ctlseqs.txt中,总结如下:

  Ps = 4 ; c ; spec -> Change Color Number c to the color
specified by spec.  This can be a name or RGB specification as
per XParseColor.  Any number of c/spec pairs may be given.
The color numbers correspond to the ANSI colors 0-7, their
bright versions 8-15, and if supported, the remainder of the
88-color or 256-color table.

If a "?" is given rather than a name or RGB specification,
xterm replies with a control sequence of the same form which
can be used to set the corresponding color.  Because more than
one pair of color number and specification can be given in one
control sequence, xterm can make more than one reply.
Run Code Online (Sandbox Code Playgroud)

稍后在文档中有更多的 OSC 子命令,Ps = 10以及Ps = 11,等等。

Ps = 1 0  -> Change VT100 text foreground color to Pt.
Ps = 1 1  -> Change VT100 text background color to Pt.
Run Code Online (Sandbox Code Playgroud)

示例 - 这使用Ps = "11"(从上面)和查询背景Pt = "?",插入OSC Ps ; Pt ST. 在 echo 中,\033用于转义和\\最后的反斜杠。

echo -en "\033]11;?\033\\"
Run Code Online (Sandbox Code Playgroud)

输出:

^[]11;rgb:0000/0000/0000^[\ 
Run Code Online (Sandbox Code Playgroud)

警告:返回的颜色不反映是否-rv启用了反向视频,例如,并且通过可用的~260 种颜色爬行OSC 4 ; c ; ? ST不会显示任何跟随背景并随反向视频变化的颜色。由于许多用户仅使用 设置深色背景xterm -rv,这使得确定背景是否实际上是深色变得复杂。大多数颜色也不适应-rv

执行完整查询并实际捕获来自 xterm 的回复的脚本:

#!/bin/bash
success=false
exec < /dev/tty
oldstty=$(stty -g)
stty raw -echo min 0
col=11      # background
#          OSC   Ps  ;Pt ST
echo -en "\033]${col};?\033\\" >/dev/tty  # echo opts differ w/ OSes
result=
if IFS=';' read -r -d '\' color ; then
    result=$(echo $color | sed 's/^.*\;//;s/[^rgb:0-9a-f/]//g')
    success=true
fi
stty $oldstty
echo $result
$success
Run Code Online (Sandbox Code Playgroud)


aki*_*ira 1

有点儿

将设置放入 ~/.Xdefaults 文件中:

xterm*foreground: blue
xterm*background: white
Run Code Online (Sandbox Code Playgroud)

在你的 shell 中你只需 grep 值:

awk '/xterm\*foreground:(.*)/ { print $2 }' < .Xdefaults
Run Code Online (Sandbox Code Playgroud)

否则很难获得 xterm 的一些内部值。