Skip to content

Instantly share code, notes, and snippets.

@lenary
Last active October 31, 2023 03:19
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lenary/7df679b241b1331ae6b3318136dffa0e to your computer and use it in GitHub Desktop.
Save lenary/7df679b241b1331ae6b3318136dffa0e to your computer and use it in GitHub Desktop.
Last command's exit status, for your prompt. Put ${last_exit} into your prompt somewhere. It uses zsh's prompt escapes.
precmd_functions=("_exit_status" ${precmd_functions[@]})
function _exit_status() {
local last_exit_status=$?
export last_exit=""
if [ -z "${last_exit_status}" ]; then
last_exit=""
elif (( last_exit_status != 0 )); then
local description
# Let's be comprehensive
case $last_exit_status in
# Signals are 128 + signal value, we translate back to the signal name
$((128 + 1)) ) description="SIGHUP";;
$((128 + 2)) ) description="SIGINT";;
$((128 + 3)) ) description="SIGQUIT";;
$((128 + 4)) ) description="SIGILL";;
$((128 + 5)) ) description="SIGTRAP";;
$((128 + 6)) ) description="SIGABRT";;
$((128 + 7)) ) description="SIGEMT";;
$((128 + 8)) ) description="SIGFPE";;
$((128 + 9)) ) description="SIGKILL";;
$((128 + 10)) ) description="SIGBUS";;
$((128 + 11)) ) description="SIGSEGV";;
$((128 + 12)) ) description="SIGSYS";;
$((128 + 13)) ) description="SIGPIPE";;
$((128 + 14)) ) description="SIGALRM";;
$((128 + 15)) ) description="SIGTERM";;
$((128 + 16)) ) description="SIGURG";;
$((128 + 17)) ) description="SIGSTOP";;
$((128 + 18)) ) description="SIGTSTP";;
$((128 + 19)) ) description="SIGCONT";;
$((128 + 20)) ) description="SIGCHLD";;
$((128 + 21)) ) description="SIGTTIN";;
$((128 + 22)) ) description="SIGTTOU";;
$((128 + 23)) ) description="SIGIO";;
$((128 + 24)) ) description="SIGXCPU";;
$((128 + 25)) ) description="SIGXFSZ";;
$((128 + 26)) ) description="SIGVTALRM";;
$((128 + 27)) ) description="SIGPROF";;
$((128 + 28)) ) description="SIGWINCH";;
$((128 + 29)) ) description="SIGINFO";;
$((128 + 30)) ) description="SIGUSR1";;
$((128 + 31)) ) description="SIGUSR2";;
# All other cases. While ZSH may have specific errors for lack of permissions
# the script itself may define these, to be conservative, just output the
# number.
*) description="${last_exit_status}";;
esac
last_exit="%F{red}${description}%f "
fi;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment