我想知道是否有办法确定终端的背景颜色?
在我的情况下,使用的是gnome-terminal。
这可能很重要,因为完全由终端应用程序绘制其窗口的背景,这甚至可能不是纯色。
我想知道是否有办法确定终端的背景颜色?
在我的情况下,使用的是gnome-terminal。
这可能很重要,因为完全由终端应用程序绘制其窗口的背景,这甚至可能不是纯色。
#!/bin/sh
#
# Query a property from the terminal, e.g. background color.
#
# XTerm Operating System Commands
# "ESC ] Ps;Pt ST"
oldstty=$(stty -g)
# What to query?
# 11: text background
Ps=${1:-11}
stty raw -echo min 0 time 0
# stty raw -echo min 0 time 1
printf "\033]$Ps;?\033\\"
# xterm needs the sleep (or "time 1", but that is 1/10th second).
sleep 0.00000001
read -r answer
# echo $answer | cat -A
result=${answer#*;}
stty $oldstty
# Remove escape at the end.
echo $result | sed 's/[^rgb:0-9a-f/]\+$//'
源代码/仓库/Gist: https://gist.github.com/blueyed/c8470c2aad3381c33ea3
sleep 0.01
。 - fzbdsed 's/.*\(rgb:[0-9a-f/]*\).*/\1/'
进行过滤。 - fzbd0.01
也可以使其在那里可靠。 - xerufxtermcontrol --get-bg
)COLORFGBG
环境变量(由Rxvt使用,但不是许多其他应用程序...):<foreground-color>:[<other-setting>:]<background-color>
,
(<other setting>:
是可选的),如果<background-color>
在{0,1,2,3,4,5,6,8}中,则有一些深色背景。例如,来自Neovim问题2764的相关片段:
/*
* Return "dark" or "light" depending on the kind of terminal.
* This is just guessing! Recognized are:
* "linux" Linux console
* "screen.linux" Linux console with screen
* "cygwin" Cygwin shell
* "putty" Putty program
* We also check the COLORFGBG environment variable, which is set by
* rxvt and derivatives. This variable contains either two or three
* values separated by semicolons; we want the last value in either
* case. If this value is 0-6 or 8, our background is dark.
*/
static char_u *term_bg_default(void)
{
char_u *p;
if (STRCMP(T_NAME, "linux") == 0
|| STRCMP(T_NAME, "screen.linux") == 0
|| STRCMP(T_NAME, "cygwin") == 0
|| STRCMP(T_NAME, "putty") == 0
|| ((p = (char_u *)os_getenv("COLORFGBG")) != NULL
&& (p = vim_strrchr(p, ';')) != NULL
&& ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
&& p[2] == NUL))
return (char_u *)"dark";
return (char_u *)"light";
}
COLORFGBG
环境变量,来自Gnome BugZilla 733423:def is_dark_terminal_background():
"""
:return: Whether we have a dark Terminal background color, or None if unknown.
We currently just check the env var COLORFGBG,
which some terminals define like "<foreground-color>:<background-color>",
and if <background-color> in {0,1,2,3,4,5,6,8}, then we have some dark background.
There are many other complex heuristics we could do here, which work in some cases but not in others.
See e.g. `here <https://dev59.com/GXE95IYBdhLWcg3wAo9U
But instead of adding more heuristics, we think that explicitly setting COLORFGBG would be the best thing,
in case it's not like you want it.
:rtype: bool|None
"""
if os.environ.get("COLORFGBG", None):
parts = os.environ["COLORFGBG"].split(";")
try:
last_number = int(parts[-1])
if 0 <= last_number <= 6 or last_number == 8:
return True
else:
return False
except ValueError: # not an integer?
pass
return None # unknown (and bool(None) == False, i.e. expect light by default)
正如其他人所提到的,您可以使用 OSC 11 ?
查询终端(尽管支持程度有所不同)。
在 bash 和 gnome-terminal 中:
read -rs -d \\ -p $'\e]11;?\e\\' BG
echo "$BG" | xxd
00000000: 1b5d 3131 3b72 6762 3a30 3030 302f 3262 .]11;rgb:0000/2b
00000010: 3262 2f33 3633 361b 0a 2b/3636..
read -s
禁用回显,$''
字符串用于转义代码),但不幸的是它会吞掉最后一个反斜杠。
rxvt
之外仍然无法获得。) - ELLIOTTCABLE❯ printf "\e]11;?\a"
会产生^[]11;rgb:0000/0000/0000^G%
,在iTerm2中则会产生^[]11;rgb:0000/0000/0000^G
。但是我无法捕获这个输出并在shell脚本中使用if语句。这是否有可能? - Konstantin Gredeskoulsleep
超时时间。 - devstuff