编程访问当前 xterm 颜色

编程访问当前 xterm 颜色

在以下帖子中

通过编程访问当前 xterm 背景颜色?

Alex North-Keys 提供了一个有用的 bash 脚本,可以返回当前 xterm 的背景颜色。我想使用此脚本的输出在背景颜色发生更改后重置背景颜色(例如,在登录远程系统后)。

例如,他的脚本可能会返回类似

rgb:e0e0/ffff/ffff

不幸的是我用来设置背景颜色的转义序列

echo -ne "\033]11;!*\007"

似乎只有当我给它喂一种指定的颜色(如象牙色)时才会起作用。

有没有办法修改这个命令,以便它将类似 e0e0/ffff/ffff 之类的命令作为参数?

谢谢!

答案1

复制并粘贴到 Emacs 或 Vim 中。删除其中一行开头的 #?????。该行中有一个包含“^[”的 sed 语句。这是 2 个字符的文本,需要用一个字符 ESC(^[)替换。在 Vim 中,输入 C+v ESC。在 Emacs 中,输入 C+q ESC。

# filename: OSC_11_Pt_ST.sh
echo -e "\e[7m Reverse the FG/BG colors on this terminal as a visual aid. \e[0m"
y='\' # Add to end of $result (\e]11;rgb:??/??/??\e) to complete ST terminating
# OSC sequence.
# Query the background color using ST (string terminate) vice BEL (^G).Replies
# with a control sequence in RGB specification format using same terminator as
# the query (in this case, ST): "^[]11;rgb:4242/2525/5353^[\" (see file
# ctlseqs.txt). "^[" is ESC (\e); "^[]" is OSC; "^[\" is ST.
echo -en "\e]11;?\e\\"
# Read line from the standard input & assign to variable CSPEC (color specifi-
# cation as indicated by ctlseqs.txt). Do not echo input coming from terminal
# (-s); raw (-r); delimiter \ (-d).
read -s -r -d '\' CSPEC
#?????result=$(echo $CSPEC | sed 's/^[/\\\e/g')
# Set BG to "WebGrey" (decimal 128 128 128).
echo 'Set BG to "Webgrey" in 5 seconds...'
sleep 5
echo -en "\e]11;rgb:80/80/80\007"
echo "Return to original BG ($result$y) in 5 seconds..."
sleep 5
echo -en "$result$y"

相关内容