diff --git a/README.md b/README.md
index 674d7a57..19d11c27 100644
--- a/README.md
+++ b/README.md
@@ -6,9 +6,9 @@ Here, we have a debugger for Bash 5.2 and higher.
Other branches in this repository support earlier versions of Bash. For example, use branch 4.1 for Bash version 4.1.
-The command syntax generally follows that of the [zsh debugger ](https://github.com/rocky/zshdb), trepanning debuggers and, more generally, GNU debugger *gdb*.
+The command syntax generally follows that of the [zsh debugger ](https://github.com/rocky/zshdb), trepanning debuggers, and, more generally, GNU debugger *gdb*.
-There are 3 ways to get into the debugger. If bash (with debugger support enabled which is the default) is installed and the debugger are *both* installed properly. Then:
+There are 3 ways to get into the debugger. If bash (with debugger support enabled, which is the default) is installed, and the debugger is installed properly. Then:
```
bash --debugger -- bash-script-name script-arg1 script-arg2...
@@ -21,7 +21,7 @@ If bash isn't installed in a way that will find bashdb, then:
```
The downside here is that $0 will be "bashdb" not
-bash-script-name. Also call stack will show the invocation to bashdb.
+bash-script-name. Also acall stack will show the invocation to bashdb.
Finally, to invoke the debugger from the script
@@ -36,15 +36,14 @@ Finally, to invoke the debugger from the script
stop_here
```
-An advantage of the above is that there is no overhead up until you
-invoke the debugger. Typically for large bash programs like
+An advantage of the above is that there is no overhead until you
+invoke the debugger. Typically, for large bash programs like
configuration scripts, this is a big win.
*IMPORTANT NOTE IF YOU USE THE ABOVE TO DEBUG CONFIGURE SCRIPTS...*
stdin is closed by configure early on. This causes the debugger to quit.
-You can get around this invoking a command script that sets debugger
-up input and output. Run tty to figure out what the terminal tty is set to.
+You can get around this by invoking a command script that sets up the debugger input and output. Run tty to figure out what the terminal tty is set to.
```
$ tty
diff --git a/htdocs/index.html b/htdocs/index.html
index e9f71114..3ee8fb09 100644
--- a/htdocs/index.html
+++ b/htdocs/index.html
@@ -153,7 +153,7 @@
BASH Debugger
Zsh and the
Korn Shell.
-Slightly related, there is cool debugger embedded im my fork of
+Slightly related, there is cool debugger embedded in my fork of
GNU Make.
@@ -179,6 +179,8 @@ Screenshots
Documentation
+ - BASHDB
+Reference – Debugger documentation online in outline format from Rod Smith
- BASHDB
Reference – Debugger documentation on ReadTheDocs
- BASHDB
diff --git a/init/term-background.sh b/init/term-background.sh
index cff8db53..b750a99b 100644
--- a/init/term-background.sh
+++ b/init/term-background.sh
@@ -2,7 +2,7 @@
# ^^^^^^^^^^^^ 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, Rocky Bernstein
+# Copyright (C) 2019-2020, 2025 Rocky Bernstein
# 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
@@ -18,10 +18,27 @@
# the Free Software Foundation, 59 Temple Place, Suite 330, Boston,
# MA 02111 USA.
-# Try to determine if we have dark or light terminal background
+# Try to determine if we have dark or light terminal background.
-typeset -i success=0
-typeset method="xterm control"
+# 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)
@@ -49,9 +66,10 @@ get_default_colorfgbg() {
fi
}
-# Pass as parameters R G B values in hex
-# Compare FG to BG for light/dark, not just FB and midpoint
-# NOTE: We could have FG=#403020 BG=#203040 and tie
+# 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
@@ -60,11 +78,27 @@ is_dark_rgb() {
fg_r=${1:-0}
fg_g=${2:-0}
fg_b=${3:-0}
- bg_r=${4:-FF}
- bg_g=${5:-FF}
- bg_b=${6:-FF}
- a_fg=$((16#"$fg_r" + 16#"$fg_g" + 16#"$fg_b"))
- a_bg=$((16#"$bg_r" + 16#"$bg_g" + 16#"$bg_b"))
+ 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
@@ -72,18 +106,69 @@ is_dark_rgb() {
fi
}
-# Consult (environment) variable COLORFGB
+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_colorfgbg() {
- case $COLORFGBG in
- '15;0' | '15;default;0')
+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
;;
- '0;15' | '0;default;15')
+ "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
;;
@@ -91,7 +176,7 @@ is_dark_colorfgbg() {
}
is_sourced() {
- if [[ $0 == ${BASH_SOURCE[0]} ]]; then
+ if [[ $0 == "${BASH_SOURCE[0]}" ]]; then
return 1
else
return 0
@@ -108,6 +193,53 @@ exit_if_not_sourced() {
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
+ ;;
+ *)
+ # Unkonwn
+ ;;
+ 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:
@@ -118,74 +250,104 @@ xterm_compatible_fg_bg() {
typeset fg bg
# Turn TTY off
stty -echo 2>/dev/null
- # Issue command to get foreground
+
+ # 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
+
+ # 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
+
+ # Remove any escape or control characters.
# Note: gnome-terminal tacked \e at end
- fg=$(echo "$fg" | sed 's/[^a-zA-Z0-9/]//g')
+ fg="${fg//[^a-zA-Z0-9\/]/}"
[[ -z $fg ]] && return 1
+
# Convert to array
- typeset -p fg
IFS='/' read -ra RGB_fg <<<"$fg"
- typeset -p RGB_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
+
+ # 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
+
+ # Remove any escape or control characters.
# Note: gnome-terminal tacked \e at end
- bg=$(echo "$bg" | sed 's/[^a-zA-Z0-9/]//g')
+ bg="${bg//[^a-zA-Z0-9\/]/}"
[[ -z $bg ]] && return 1
+
# Convert to array
- typeset -p bg
IFS='/' read -ra RGB_bg <<<"$bg"
- typeset -p RGB_bg
+
+ if [[ -n $DEBUG_TERM_BACKGROUND ]]; then
+ typeset -p bg
+ typeset -p RGB_bg
+ fi
+
xterm_osc_done=1
return 0
}
-# From a comment left by user "duthen" in my StackOverflow answer cited above.
osx_get_terminal_fg_bg() {
- if [[ -n $COLORFGBG ]]; then
- method="COLORFGBG"
- is_dark_colorfgbg
- else
- 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 ${RGB_fg[@]} ${RGB_bg[@]}
- method="OSX osascript"
- success=1
- fi
+ 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 (( 3711 < VTE_VERSION )) && [[ -z "$COLORFGBG" ]]; then
+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[@]}
+ is_dark_rgb "${RGB_fg[@]}" "${RGB_bg[@]}"
if [[ $is_dark_bg == 1 ]]; then
# Even though, we're xterm, assist COLORFGBG
- export COLORFGBG='0;15'
+ export COLORFGBG=${WHITE_BACKGROUND_FGBG}
else
# Even though, we're xterm, assist COLORFGBG
- export COLORFGBG='15;0'
+ export COLORFGBG=${BLACK_BACKGROUND_FGBG}
fi
else
echo "xterm/vte Esc-] OSC has empty string"
@@ -194,24 +356,28 @@ if (( 3711 < VTE_VERSION )) && [[ -z "$COLORFGBG" ]]; then
unset x fg bg avg_RGB_fg avg_RGB_bg
fi
-if [[ ${BASH_VERSINFO[5]} == *"darwin"* ]]; then
+if ((!success)) && [[ ${BASH_VERSINFO[5]} == *"darwin"* ]]; then
osx_get_terminal_fg_bg
fi
-if ((!success)) && [[ -n $TERM ]]; then
- case $TERM in
- xterm* | gnome | rxvt* | linux)
- typeset -a RGB_fg RGB_bg
- if [[ $xterm_osc_done -eq 1 ]]; then
- if xterm_compatible_fg_bg; then
- is_dark_rgb ${RGB_fg[@]} ${RGB_bg[@]}
- fi
- success=1
+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
- ;;
- *) ;;
-
- esac
+fi
+if ((!success)) && [[ -n $CLITHEME ]]; then
+ is_dark_clitheme
fi
if ((success)); then
@@ -248,11 +414,15 @@ if is_sourced; then
if ((is_dark_bg == 1)); then
export DARK_BG=1 # deprecated
export LC_DARK_BG=1
- [[ -z $COLORFGBG ]] && export COLORFGBG='0;15'
+ if [[ -z "$COLORFGBG" ]]; then
+ export COLORFGBG=${WHITE_BACKGROUND_FGBG}
+ fi
else
export DARK_BG=0
export LC_DARK_BG=0
- [[ -z $COLORFGBG ]] && export COLORFGBG='15;0'
+ if [[ -z "$COLORFGBG" ]] ; then
+ export COLORFGBG=${BLACK_BACKGROUND_FGBG}
+ fi
fi
fi
else