-
Notifications
You must be signed in to change notification settings - Fork 13
/
gettermbg
executable file
·69 lines (61 loc) · 1.94 KB
/
gettermbg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env bash
# gettermbg -- Check whether the terminal has a light or dark background.
#
# Queries the Xterm background and uses the formula from:
# https://learn.microsoft.com/en-us/windows/apps/desktop/modernize/apply-windows-themes#know-when-dark-mode-is-enabled
#
# A more advanced tool is available at:
# https://github.com/rocky/shell-term-background/blob/master/term-background.bash
usage() {
echo "Usage: ${0##*/} {-d|-l|--is-dark|--is-light}"
}
if [[ ! -t 0 ]]; then
exit 1
fi
exec </dev/tty >/dev/tty
trap "stty echo" EXIT
stty -echo
# Request Xterm background color OSC
printf '\e]11;?\e\\' # returns '\e]11;rgb:XXXX/YYYY/ZZZZ\e\\'
# Request Primary Device Attributes, to avoid timeouts when the terminal
# doesn't understand the Xterm OSC
printf '\e[c' # returns '\e[?XXXc'
# (XXX: Could also use DSR '\e[5n' => '\e[0n')
# Read up to the ? in the middle of Primary DA answer
if read -r -d '?' -t 2 ans1 &&
read -r -d 'c' -t 2 ans2; then
ans="${ans1}?${ans2}c"
re=$'^\e\\]11;(rgb:([[:alnum:]]+)/([[:alnum:]]+)/([[:alnum:]]+))[\a\e]'
if [[ $ans1 =~ $re ]]; then
declare -i r="0x${BASH_REMATCH[2]}"
declare -i g="0x${BASH_REMATCH[3]}"
declare -i b="0x${BASH_REMATCH[4]}"
# X11 colors are rrrr/gggg/bbbb, e.g. "rgb:ffff/f7f7/dfdf"
# or "rgb:1c1c/1c1c/1c1c".
r="r >> 8"
g="g >> 8"
b="b >> 8"
# Use the formula from Microsoft
declare -i is_light='(5*g + 2*r + b) > (8*128)'
case $1 in
"") echo "${BASH_REMATCH[1]}";;
-l|--is-light) (( is_light ));;
-d|--is-dark) (( !is_light ));;
*) usage >&2; exit 2;;
esac
elif [[ $ans1 == $'\e[' ]]; then
# No answer to color inquiry, all we got is Primary DA
case $1 in
"") echo "${0##*/}: No OSC reply from terminal" >&2; exit 1;;
-l|--is-light) false;;
-d|--is-dark) true;;
*) usage >&2; exit 2;;
esac
else
echo "${0##*/}: Invalid reply from terminal: ${ans1@Q}" >&2
exit 3
fi
else
echo "${0##*/}: Did not receive Primary DA reply from terminal" >&2
exit 4
fi