@@ -31,7 +31,15 @@ function update-terminal-color-mode() {
3131 ;;
3232 esac
3333
34- if [[ $new_mode == " unknown" ]]; then
34+ if [[ " $new_mode " == " auto-detect-failed" ]]; then
35+ if [[ " $quiet " == " true" ]]; then
36+ return 9
37+ else
38+ new_mode=" unknown"
39+ fi
40+ fi
41+
42+ if [[ " $new_mode " == " unknown" ]]; then
3543 if ! tty -s; then
3644 [[ " $quiet " == " true" ]] || echo " No terminal detected." >&2
3745 elif [[ -z " $( tput op 2> /dev/null) " ]]; then
@@ -138,11 +146,13 @@ function _geodesic_tput_cache_init() {
138146 ;;
139147 esac
140148
149+ # Treat any failure of color detection as historical default "light" mode
141150 if [[ $new_mode == " dark" ]]; then
142151 _geodesic_tput_cache[black]=$( tput setaf 7) # swap black and white
143152 _geodesic_tput_cache[white]=$( tput setaf 0) # 0 is ANSI black, 7 is ANSI white
144153 _geodesic_tput_cache[blue]=${_geodesic_tput_cache[cyan]} # blue is too dark, use cyan instead
145154 else
155+ new_mode=" light"
146156 _geodesic_tput_cache[yellow]=${_geodesic_tput_cache[magenta]} # yellow is too light, use magenta instead
147157 fi
148158
@@ -297,18 +307,29 @@ function reset_terminal_colors() {
297307
298308_geodesic_tput_cache_init
299309
310+ function auto-update-terminal-color-mode() {
311+ [[ ${GEODESIC_TERM_COLOR_UPDATING} == " true" ]] || [[ ${GEODESIC_TERM_COLOR_AUTO:- true} != " true" ]] && return 0
312+
313+ # Ignore repeated signals while a signal is being processed
314+ export GEODESIC_TERM_COLOR_UPDATING=true
315+ update-terminal-color-mode quiet
316+ if [[ $? -eq 9 ]]; then
317+ # If the color detection failed, we disable automatic detection.
318+ export GEODESIC_TERM_COLOR_AUTO=disabled
319+ fi
320+ unset GEODESIC_TERM_COLOR_UPDATING
321+ }
322+
300323# Although SIGWINCH is a standard signal to indicate the window *size* has changed,
301324# some terminals (not sure which ones) also send a SIGWINCH signal when the window colors change.
302325# For the other terminals, catching SIGWINCH gives users an easy way of triggering a color update: resize the window.
303326# So we catch the signal to update the terminal colors, preserving any existing signal handlers.
304-
327+ # However, we do the actual color update in a separate function called from the shell prompt command,
328+ # to avoid issues with async access to the TTY and other issues with running inside a signal handler.
305329function _update-terminal-color-mode-sigwinch() {
306- [[ ${GEODESIC_TERM_COLOR_SIGNAL} == " true" ]] || [[ ${GEODESIC_TERM_COLOR_AUTO:- true} != " true" ]] && return 0
307-
308330 # Ignore repeated signals while a signal is being processed
309- export GEODESIC_TERM_COLOR_SIGNAL=true
310- update-terminal-color-mode quiet
311- unset GEODESIC_TERM_COLOR_SIGNAL
331+ [[ ${GEODESIC_TERM_COLOR_UPDATING} == " true" ]] || [[ ${GEODESIC_TERM_COLOR_AUTO:- true} != " true" ]] && return 0
332+ export GEODESIC_TERM_COLOR_UPDATING=" needed"
312333}
313334
314335if [[ ${GEODESIC_TERM_COLOR_AUTO} != " unsupported" ]] && _is_color_term; then
0 commit comments