端末の背景色を決定する方法があるかどうか知りたいですか?
私の場合、gnome-terminalを使用しています。
ウィンドウの背景を描画するのはターミナルアプリケーション次第であるため、問題になる可能性があります。これは、無地以外の色である場合もあります。
端末の背景色を決定する方法があるかどうか知りたいですか?
私の場合、gnome-terminalを使用しています。
ウィンドウの背景を描画するのはターミナルアプリケーション次第であるため、問題になる可能性があります。これは、無地以外の色である場合もあります。
これにはxterm制御シーケンスがあります。
\e]11;?\a
(\e
および\a
はそれぞれESC文字とBEL文字です。)
Xterm互換の端末は、同じシーケンスで応答する必要があります。疑問符はX11の色の名前に置き換えられます(例:rgb:0000/0000/0000
黒)。
私は次のことを思いついた:
#!/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/]\+$//'
ソース/リポジトリ/要点:https ://gist.github.com/blueyed/c8470c2aad3381c33ea3
いくつかのリンク:
xtermcontrol --get-bg
)COLORFGBG
環境変数(Rxvtで使用されますが、他の多くでは使用されません...): sth<foreground-color>:[<other-setting>:]<background-color>
、(<other setting>:
オプション)、<background-color>
{0,1,2,3,4,5,6,8}の場合、背景が暗い。たとえば、 Neovim Issue 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
envについて、 Gnome BugZilla 733423から:
Linuxで試したばかりのかなりの数の端末のうち、urxvtとkonsoleだけが設定しました(xterm、st、terminology、ptermを設定しない端末)。KonsoleとUrxvtは異なる構文とセマンティクスを使用します。つまり、私にとってkonsoleはそれを「0; 15」に設定します(「ライトイエローに黒」の配色を使用しているのに、「15」ではなく「デフォルト」にしないのはなぜですか?)、一方、私のurxvtはそれを「0; default; 15」に設定します(実際には白地に黒ですが、なぜ3つのフィールドなのですか?)。したがって、これら2つのどちらでも、値は仕様と一致しません。
これは私が使用している独自のコードです(経由):
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://stackoverflow.com/questions/2507337/terminals-background-color>`__.
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)
どうやらrxvtのみの$COLORFGBGを除けば、他に何かが存在することすら気づいていません。ほとんどの人はvimがそれをどのように行うかについて言及しているようです、そしてそれでさえせいぜい知識に基づいた推測です。
ターミナルの背景色を確認する方法、またはターミナルの色を設定する方法を意味しますか?
後者の場合は、端末のPS1環境変数を照会して色を取得できます。
ターミナルの色の設定(およびその導出)に関する記事がここにあります: http ://www.ibm.com/developerworks/linux/library/l-tip-prompt/