added last command error status to prompt

This commit is contained in:
Guillaume Raffy 2024-05-31 14:54:41 +02:00
parent bbee157c71
commit d3985db075
1 changed files with 53 additions and 1 deletions

View File

@ -66,7 +66,7 @@ if [ -n "$force_color_prompt" ]; then
fi fi
if [ "$color_prompt" = yes ]; then if [ "$color_prompt" = yes ]; then
PS1=' \[\033[01;36m\]\D{%Y%m%d-%H:%M:%S}\[\033[00m\] ${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' PS1='\[\033[01;36m\]\D{%Y%m%d-%H:%M:%S}\[\033[00m\] ${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi fi
@ -81,6 +81,58 @@ xterm*|rxvt*)
;; ;;
esac esac
PROMPT_COMMAND=__prompt_command # Function to generate PS1 after CMDs
__prompt_command() {
local exit_code="$?" # This needs to be first
PS1=""
local RCol='\[\e[0m\]'
local Red='\[\e[0;31m\]'
local Gre='\[\e[0;32m\]'
local BYel='\[\e[1;33m\]'
local BBlu='\[\e[1;34m\]'
local Pur='\[\e[0;35m\]'
local last_command_status=''
local last_command_status_color=''
local status_prompt_mode='exit-code'
case "$status_prompt_mode" in
'exit-code')
last_command_status="${exit_code}"
if [ $exit_code = 0 ]; then
last_command_status_color="${Gre}"
else
last_command_status_color="${Red}"
fi
;;
'pipe-status')
# warning : this mode doesn't seem to be working
# Bash variable PIPESTATUS provides more information about the last executed command or pipe. ${PIPESTATUS[@]} will be a string of zeroes like 0 0 0 in case all commands were executed correctly, otherwise it will have non zeroes like 0 127 1. One can check for success with if $(echo ${PIPESTATUS[@]} | grep -qEe '^0( 0)*$'); then echo "good"; else echo "bad"; fi.
last_command_status="${PIPESTATUS[@]}"
if $(echo ${PIPESTATUS[@]} | grep -qEe '^0( 0)*$')
then
# echo "good"
last_command_status_color="${Gre}"
else
# echo "bad"
last_command_status_color="${Red}"
fi
;;
*)
last_command_status_color="${Red}"
last_command_status="unexpected value for status_prompt_mode: $status_prompt_mode"
;;
esac
PS1+="[${last_command_status_color}${last_command_status}${RCol}]"
PS1+='\[\033[01;36m\]\D{%Y%m%d-%H:%M:%S}\[\033[00m\] ${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
}
# enable color support of ls and also add handy aliases # enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)" test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"