dotfiles/dotlib/pre/prompt.sh

46 lines
912 B
Bash

###
# Setup and decorate the prompt.
###
prompt_symbol=λ
prompt_color_fail=(204 80 83)
prompt_color_pass=(48 92 161)
if hash git 2>/dev/null; then
prompt_has_git=true
fi
prompt_color_reset=$(tput sgr0)
symbol-decorate() {
local -n prompt_colors=$1
local r=${prompt_colors[0]} g=${prompt_colors[1]} b=${prompt_colors[2]}
printf '\e[38;2;%d;%d;%dm%s%s' "$r" "$g" "$b" "$prompt_symbol" "$(tput sgr0)"
}
prompt-decorate() {
local res=$? prompt_str
# prepend the
if [[ -v SSH_CLIENT && -v HOSTNAME ]]; then
prompt_str+=$HOSTNAME:
fi
prompt_str+="\w "
if [[ -v prompt_has_git && -d .git ]]; then
prompt_str+="$(command git branch --show-current) "
fi
if (( res )); then
prompt_str+=$(symbol-decorate prompt_color_fail)
else
prompt_str+=$(symbol-decorate prompt_color_pass)
fi
prompt_str+="$(tput sgr0) "
PS1=$prompt_str
}
PROMPT_COMMAND+=(prompt-decorate)