-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathterm-background.bash
More file actions
executable file
·430 lines (381 loc) · 10.7 KB
/
term-background.bash
File metadata and controls
executable file
·430 lines (381 loc) · 10.7 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#!/usr/bin/env bash
# ^^^^^^^^^^^^ Use env rather than assume we've got /bin/bash.
# This works better on OSX where /bin/bash is brain dead.
# Copyright (C) 2019-2020, 2025 Rocky Bernstein <rocky@gnu.org>
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not, write to
# the Free Software Foundation, 59 Temple Place, Suite 330, Boston,
# MA 02111 USA.
# Try to determine if we have dark or light terminal background.
# This file is resides in, or is copied from, the project:
# https://github.com/rocky/shell-term-background
#
# If you have problems with this script open an issue there. Note: I
# use github project ratings to help me determine if project issues
# are worth fixing when (as usually the case), there are several
# issues I could be working on.
typeset method="xterm control"
# "0" is the black in the standard 16-color ANSI pallet.
# "15" is white in the standard 16-color ANSI pallet.
# ";" separates the foreground and background color.
#
# So COLORFGBG="0;15" indicates a white background
# with black foreground text, or a light background.
WHITE_BACKGROUND_FGBG="0;15"
BLACK_BACKGROUND_FGBG="15;0"
# On return, variable is_dark_bg is set
# We follow Emacs logic (at least initially)
get_default_colorfgbg() {
if [[ -n $COLORFGBG ]]; then
method="COLORFGBG"
is_dark_colorfgbg
elif [[ -n $TERM ]]; then
case $TERM in
xterm-256color)
# 382.5 = (* .6 (+ 255 255 255))
TERMINAL_COLOR_MIDPOINT=${TERMINAL_COLOR_MIDPOINT:-383}
;;
xterm* | dtterm | eterm*)
# 117963 = (* .6 (+ 65535 65535 65535))
TERMINAL_COLOR_MIDPOINT=${TERMINAL_COLOR_MIDPOINT:-117963}
is_dark_bg=0
;;
*)
TERMINAL_COLOR_MIDPOINT=${TERMINAL_COLOR_MIDPOINT:-117963}
is_dark_bg=1
;;
esac
fi
}
# Pass as parameters R, G, and B values.
# In some terminals the value is in hex and in some terminals it is in decimal.
# Compare FG to BG for light/dark, not just FG and midpoint
# NOTE: We could have FG=#403020 BG=#403040 and tie
# On return, variable is_dark_bg is set
is_dark_rgb() {
typeset fg_r fg_g fg_b
typeset bg_r bg_g bg_b
typeset a_fg a_bg
fg_r=${1:-0}
fg_g=${2:-0}
fg_b=${3:-0}
bg_r=${4:-255}
bg_g=${5:-255}
bg_b=${6:-255}
# Check if any of the R, G, or B values contain hex letters.
# If so, convert to decimal.
if [[ "$fg_r" =~ [a-fA-F] || "$fg_g" =~ [a-fA-F] || "$fg_b" =~ [a-fA-F] ]]; then
a_fg=$((16#"$fg_r" + 16#"$fg_g" + 16#"$fg_b"))
a_bg=$((16#"$bg_r" + 16#"$bg_g" + 16#"$bg_b"))
else
a_fg=$((fg_r + fg_g + fg_b))
a_bg=$((bg_r + bg_g + bg_b))
fi
if [[ $bg_r =~ [a-fA-F] || "$fb_g" =~ [a-fA-F] || "$fg_b" =~ [a-fA-F] ]]; then
a_fg=$((16#"$fg_r" + 16#"$fg_g" + 16#"$fg_b"))
a_bg=$((16#"$bg_r" + 16#"$bg_g" + 16#"$bg_b"))
else
a_fg=$((fg_r + fg_g + fg_b))
a_bg=$((bg_r + bg_g + bg_b))
fi
if [[ $a_fg -gt $a_bg ]]; then
is_dark_bg=1
else
is_dark_bg=0
fi
}
remove_comma_and_quote() {
bg_color="${1%,}" # remove trailing comma
bg_color="${bg_color#\"}" # remove leading quote
bg_color="${bg_color%\"}" # remove trailing quote
echo ${bg_color}
}
# NOTE: We could have FG=#403020 BG=#403040 and tie
# On return, variable is_dark_bg is set
is_dark_rgb_from_bg() {
midpoint=32767
bg_r=$(remove_comma_and_quote $1)
bg_g=$(remove_comma_and_quote $2)
bg_b=$(remove_comma_and_quote $3)
typeset -i a_bg=$((bg_r + bg_g + bg_b))
if (( a_bg < midpoint )); then
is_dark_bg=1
else
is_dark_bg=0
fi
}
# Consult (environment) variable CLITHEME.
# See: https://wiki.tau.garden/cli-theme.
#
# On return, variable "is_dark_bg" is set,
# and "success" can be changed from "0" to
# "1".
#
is_dark_clitheme() {
case $CLITHEME in
"light" | "light:*")
is_dark_bg=0
success=1
;;
"dark" | "dark:*")
is_dark_bg=1
success=1
;;
"auto" | "auto:*")
;;
*)
is_dark_bg=-1
;;
esac
}
# Consult (environment) variable COLORFGBG.
# On return, variable "is_dark_bg" is set,
# and "success" can be changed from "0" to
# "1".
#
is_dark_colorfgbg() {
case $COLORFGBG in
${WHITE_BACKGROUND_FGBG} | '0;default;15')
is_dark_bg=0
success=1
;;
${BLACK_BACKGROUND_FGBG} | '15;default;0')
is_dark_bg=1
success=1
;;
*)
is_dark_bg=-1
;;
esac
}
is_sourced() {
if [[ $0 == "${BASH_SOURCE[0]}" ]]; then
return 1
else
return 0
fi
}
# Exit if we are not source.
# if sourced, then we just set exitrc
# which was assumed to be declared outside
exit_if_not_sourced() {
exitrc=${1:-0}
if ! is_sourced; then
exit "$exitrc"
fi
}
# Light and Dark Mode detection using Control Sequence Introducer (CSI)
# CSI ?996 will indicate whether a terminal is in a light or dark theme.
# See https://contour-terminal.org/vt-extensions/color-palette-update-notifications/
#
# On a successful return, "is_dark_bg" is set to "1" or "0", and "1" is returned.
# On failure, "is_dark_bg" is unchanged and "0" is returned.
csc_compatible_fg_bg() {
typeset theme_mode_indicator
# Turn TTY off
stty -echo 2>/dev/null
# Issue CSI (Operating System Command) "996n" to get light-or-dark style.
# Send CSI query to the terminal
echo -ne '\e[?996n\a' > $(tty)
# Read response from the terminal
IFS=: read -t 0.3 -d $'\a' theme_mode_indicator < $(tty)
# Turn TTY back on [
stty echo 2>/dev/null
typeset -p theme_mode_indicator
[[ -z $theme_mode_indicator ]] && return 0
# Convert to array
case $theme_mode_indicator in
"997;1n" )
# Dark mode
contour_osc_done=1
is_dark_bg=1
return 1
;;
'997;2n' )
# Light mode
contour_osc_done=1
is_dark_bg=0
return 1
;;
*)
# Unknown
;;
esac
contour_osc_done=1
return 0
}
# From:
# http://unix.stackexchange.com/questions/245378/common-environment-variable-to-set-dark-or-light-terminal-background/245381#245381
# and:
# https://bugzilla.gnome.org/show_bug.cgi?id=733423#c1
#
# User should set up RGB_fg and RGB_bg arrays
xterm_compatible_fg_bg() {
typeset fg bg
# Turn TTY off
stty -echo 2>/dev/null
# Issue OSC (Operating System Command) to get foreground.
# OSC is described in https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands.
echo -ne '\e]10;?\a'
# Read back in terminal program reporting its OSC fg values.
IFS=: read -t 0.1 -d $'\a' x fg
# Turn TTY back on
stty echo 2>/dev/null
# Remove any escape or control characters.
# Note: gnome-terminal tacked \e at end
fg="${fg//[^a-zA-Z0-9\/]/}"
[[ -z $fg ]] && return 1
# Convert to array
IFS='/' read -ra RGB_fg <<<"$fg"
if [[ -n $DEBUG_TERM_BACKGROUND ]]; then
typeset -p fg
typeset -p RGB_fg
fi
# Turn TTY off
stty -echo 2>/dev/null
# Issue command to get background.
echo -ne '\e]11;?\a'
# Read back in terminal program reporting its OSC bg values
# purposely reading x, then discarding it.
IFS=: read -t 0.1 -d $'\a' x bg
# Turn TTY back on
stty echo 2>/dev/null
# Remove any escape or control characters.
# Note: gnome-terminal tacked \e at end
bg="${bg//[^a-zA-Z0-9\/]/}"
[[ -z $bg ]] && return 1
# Convert to array
IFS='/' read -ra RGB_bg <<<"$bg"
if [[ -n $DEBUG_TERM_BACKGROUND ]]; then
typeset -p bg
typeset -p RGB_bg
fi
xterm_osc_done=1
return 0
}
osx_get_terminal_fg_bg() {
if [[ -n $CLITHEME ]] ; then
method="CLITHEME"
is_dark_clitheme
fi
if ((success == 0)) && [[ -n $COLORFGBG ]]; then
method="COLORFGBG"
is_dark_colorfgbg
else
# From a comment left by user "duthen" in my StackOverflow answer cited above.
# shellcheck disable=SC2207
RGB_bg=($(osascript -e 'tell application "Terminal" to get the background color of the current settings of the selected tab of front window'))
retsts=$?
# typeset -p RGB_bg
((retsts != 0)) && return 1
is_dark_rgb_from_bg "${RGB_bg[@]}"
method="OSX osascript"
success=1
fi
}
typeset -i success=0
typeset -i is_dark_bg=0
typeset -i exitrc=0
typeset -i contour_csi_done=0
typeset -i xterm_osc_done=0
# if [[ -n $COLORTERM ]] && [[ $COLORTERM == "truecolor" ]]; then
# # Try to get light/dark mode using CSC 996 command
# csc_compatible_fg_bg
# fi
# Pre-analysis for non-COLORFGBG terminals
if ((!success)) && (( 3711 < VTE_VERSION )) && [[ -z "$COLORFGBG" ]]; then
# Try Xterm Operating System Command (OSC) (Esc-])
if xterm_compatible_fg_bg; then
is_dark_rgb "${RGB_fg[@]}" "${RGB_bg[@]}"
if [[ $is_dark_bg == 1 ]]; then
# Even though, we're xterm, assist COLORFGBG
export COLORFGBG=${WHITE_BACKGROUND_FGBG}
else
# Even though, we're xterm, assist COLORFGBG
export COLORFGBG=${BLACK_BACKGROUND_FGBG}
fi
else
echo "xterm/vte Esc-] OSC has empty string"
get_default_colorfgbg
fi
unset x fg bg avg_RGB_fg avg_RGB_bg
fi
if ((!success)) && [[ ${BASH_VERSINFO[5]} == *"darwin"* ]]; then
osx_get_terminal_fg_bg
fi
if ((!success)); then
if [[ -n $TERM ]]; then
case $TERM in
xterm* | gnome | rxvt* | linux | contour )
typeset -a RGB_fg RGB_bg
if [[ $xterm_osc_done -eq 0 ]]; then
if xterm_compatible_fg_bg; then
is_dark_rgb "${RGB_fg[@]}" "${RGB_bg[@]}"
fi
success=1
fi
;;
*) ;;
esac
fi
fi
if ((!success)) && [[ -n $CLITHEME ]]; then
is_dark_clitheme
fi
if ((success)); then
if ((is_dark_bg == 1)); then
echo "Dark background from ${method}"
else
echo "Light background from ${method}"
fi
elif [[ -n $COLORFGBG ]]; then
# Note that this can be wrong if
# COLORFGBG was set prior invoking a terminal
is_dark_colorfgbg
case $is_dark_bg in
0)
echo "Light background from COLORFGBG"
;;
1)
echo "Dark background from COLORFGBG"
;;
-1 | *)
echo "Can't decide from COLORFGBG"
exit_if_not_sourced 1
;;
esac
else
echo "Can't decide"
exit_if_not_sourced 1
fi
# If we were sourced, then set
# some environment variables
if is_sourced; then
if ((exitrc == 0)); then
if ((is_dark_bg == 1)); then
export DARK_BG=1 # deprecated
export LC_DARK_BG=1
if [[ -z "$COLORFGBG" ]]; then
export COLORFGBG=${WHITE_BACKGROUND_FGBG}
fi
else
export DARK_BG=0
export LC_DARK_BG=0
if [[ -z "$COLORFGBG" ]] ; then
export COLORFGBG=${BLACK_BACKGROUND_FGBG}
fi
fi
fi
else
exit 0
fi