From d3985db075fca051a2060380a2dbf3f8df390d0f Mon Sep 17 00:00:00 2001 From: Guillaume Raffy Date: Fri, 31 May 2024 14:54:41 +0200 Subject: [PATCH] added last command error status to prompt --- home/.bashrc | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/home/.bashrc b/home/.bashrc index 0734f81..271a50c 100644 --- a/home/.bashrc +++ b/home/.bashrc @@ -66,7 +66,7 @@ if [ -n "$force_color_prompt" ]; then fi 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 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' fi @@ -81,6 +81,58 @@ xterm*|rxvt*) ;; 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 if [ -x /usr/bin/dircolors ]; then test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"