From 1ae2422a6b77accc1b0e421cf8fa276fbcc60ad4 Mon Sep 17 00:00:00 2001 From: PowerUser64 Date: Tue, 6 May 2025 01:29:18 -0700 Subject: [PATCH] bash: improvements to prompt --- .bashrc | 97 +++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 70 insertions(+), 27 deletions(-) diff --git a/.bashrc b/.bashrc index 3eee8e9..70051be 100644 --- a/.bashrc +++ b/.bashrc @@ -22,12 +22,12 @@ export SHELL_CONFIG_DIR="${SHELL_CONFIG_DIR:-"${XDG_CONFIG_HOME:-"$HOME/.config" # Change the window title of X terminals case ${TERM} in - xterm*|rxvt*|Eterm*|aterm|kterm|gnome*|interix|konsole*) - PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/\~}\007"' - ;; - screen*) - PROMPT_COMMAND='echo -ne "\033_${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/\~}\033\\"' - ;; + (screen*) + PROMPT_COMMAND=('echo -ne "\033_${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/\~}\033\\"') + ;; + (xterm*|rxvt*|Eterm*|aterm|kterm|gnome*|interix|konsole*|*) + PROMPT_COMMAND=('echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/\~}\007"') + ;; esac use_color=true @@ -42,33 +42,76 @@ match_lhs="" [[ -f ~/.dir_colors ]] && match_lhs="${match_lhs}$(<~/.dir_colors)" [[ -f /etc/DIR_COLORS ]] && match_lhs="${match_lhs}$(/dev/null \ - && match_lhs=$(dircolors --print-database) + && type -P dircolors >/dev/null \ + && match_lhs=$(dircolors --print-database) [[ $'\n'${match_lhs} == *$'\n'"TERM "${safe_term}* ]] && use_color=true if ${use_color} ; then - # Enable colors for ls, etc. Prefer ~/.dir_colors #64489 - if type -P dircolors >/dev/null ; then - if [[ -f ~/.dir_colors ]] ; then - eval $(dircolors -b ~/.dir_colors) - elif [[ -f /etc/DIR_COLORS ]] ; then - eval $(dircolors -b /etc/DIR_COLORS) - fi - fi + # Enable colors for ls, etc. Prefer ~/.dir_colors #64489 + if type -P dircolors >/dev/null ; then + # shellcheck disable=SC2046 + if [[ -f ~/.dir_colors ]] ; then + eval $(dircolors -b ~/.dir_colors) + elif [[ -f /etc/DIR_COLORS ]] ; then + eval $(dircolors -b /etc/DIR_COLORS) + fi + fi + + # given a list of colors, make variables called `col_COLOR` + # $@ - the color list (color=number) + # $color_mod - a modifier to apply to the color (01 = bold, 00 = nothing) + make_colors() { + for a; do + local -- cname="col_${a%%=*}${modname:+_}${modname:-}" + local -- val="${a#*=}" + local -- ccode="${val}" + declare -g "$cname"='['"$ccode"m + # declare -p "$cname" + done + } + + # 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' + # shellcheck disable=SC2154 + if [[ ${EUID} == 0 ]] ; then + # shellcheck disable= + ps1_color1="$col_bold$col_red" + ps1_color2="$col_bold$col_blue" + host='\h' + else + ps1_color1="$col_bold$col_green" + ps1_color2="$col_bold$col_white" + host='\u@\h' + fi + + PS1='$ps1_color1['$host'$ps1_color2 \W$ps1_color1]$status\$$col_end ' + + # assign $status to the exit code of the previous command, color it white on failure + fmt_exit_status() { + # status=$? + case $? in + (0) + status=" " + ;; + (*) + status="$col_normal$col_white$?$ps1_color1" + ;; + esac + } + PROMPT_COMMAND+=("fmt_exit_status") - if [[ ${EUID} == 0 ]] ; then - PS1='\[\033[01;31m\][\h\[\033[01;36m\] \W\[\033[01;31m\]]\$\[\033[00m\] ' - else - PS1='\[\033[01;32m\][\u@\h\[\033[01;37m\] \W\[\033[01;32m\]]\$\[\033[00m\] ' - fi else - if [[ ${EUID} == 0 ]] ; then - # show root@ when we don't have colors - PS1='\u@\h \W \$ ' - else - PS1='\u@\h \w \$ ' - fi + if [[ ${EUID} == 0 ]] ; then + # show root@ when we don't have colors + PS1='\u@\h \W \$ ' + else + PS1='\u@\h \w \$ ' + fi fi unset use_color safe_term match_lhs sh