bash: update prompt to support displaying exit codes

This commit is contained in:
PowerUser64 2025-05-25 21:20:51 -07:00
parent 273acd5158
commit 4572baa5b4

22
.bashrc
View file

@ -61,11 +61,12 @@ if ${use_color} ; then
# $@ - the color list (color=number)
# $color_mod - a modifier to apply to the color (01 = bold, 00 = nothing)
make_colors() {
local -- a
for a; do
local -- cname="col_${a%%=*}${modname:+_}${modname:-}"
local -- val="${a#*=}"
local -- ccode="${val}"
declare -g "$cname"='['"$ccode"m
declare -rg "$cname"='['"$ccode"m
# declare -p "$cname"
done
}
@ -73,12 +74,11 @@ if ${use_color} ; then
# set prompt (see: man bash -> search for '^PROMPTING')
make_colors \
red=31 green=32 yellow=33 blue=34 purple=35 turquoise=36 white=38 white=37 \
normal=00 bold=01 quiet=02 italic=03 under=04 bg=07 gone=08 strike=09
col_end='(B'
red=31 green=32 yellow=33 blue=34 purple=35 turquoise=36 white=37 black=38 grey=90 \
normal=00 bold=01 quiet=02 italic=03 under=04 bg=07 strike=09
col_end=''
# shellcheck disable=SC2154
if [[ ${EUID} == 0 ]] ; then
# shellcheck disable=
ps1_color1="$col_bold$col_red"
ps1_color2="$col_bold$col_blue"
host='\h'
@ -88,17 +88,19 @@ if ${use_color} ; then
host='\u@\h'
fi
PS1='$ps1_color1['$host'$ps1_color2 \W$ps1_color1]$status\$$col_end '
PS1='\[$ps1_color1\]['"$host"'\[$ps1_color2\] \W\[$ps1_color1\]]\[$status_col\]$status\[$ps1_color1\]\$\[$col_end\] '
# assign $status to the exit code of the previous command, color it white on failure
fmt_exit_status() {
# status=$?
case $? in
status=$?
status_col="$col_end"
case $status in
(0)
status=" "
# shellcheck disable=SC2154
status_col+="$col_grey"
;;
(*)
status="$col_normal$col_white$?$ps1_color1"
status_col+="$col_bold$col_white"
;;
esac
}