dotfiles/homedir/.config/bash.d/prompt
fbt c78cb78ff0 init
Signed-off-by: fbt <fbt@fleshless.org>
2017-05-03 16:10:13 +03:00

76 lines
1.6 KiB
Bash

#!/bin/bash
set_prompt() {
last_exitcode="$?"
declare checkmark fancy_x timestamp git_prompt_msg git_status_short git_status_colour git_unstaged git_untracked
PS1="${c_reset}"
# Set a fancy symbol to indicate the last exitcode
if (( last_exitcode )); then
last_exitcode_indicator="${c_red}!${c_reset}"
else
last_exitcode_indicator="${c_green}.${c_reset}"
fi
# Set the username colour
if (( UID )); then
user_colour="${c_green}"
user_indicator='$'
else
user_colour="${c_red}"
user_indicator='#'
fi
user_indicator='>'
# Set the git prompt message
git rev-parse --git-dir &>/dev/null && {
git_current_branch="$(git rev-parse --abbrev-ref HEAD)"
while read; do
case "$REPLY" in
(' M'*|A*|D*) git_unstaged=1;;
(\?\?*) git_untracked=1;;
esac
done < <( git status --short )
(( git_unstaged )) && {
git_status_short+='c'
git_status_colour="${c_red}"
}
(( git_untracked )) && {
git_status_short+='f'
git_status_colour="${c_red}"
}
git_status_short=${git_status_short:-ok}
git_status_colour=${git_status_colour:-"${c_green}"}
git_prompt_msg="(${c_cyan}$git_current_branch${c_reset}[${git_status_colour}${git_status_short}${c_reset}]) "
}
set_title "${USER}@${HOSTNAME}"
if [[ "$PWD" == "$HOME" ]]; then
prompt_pwd='~'
elif [[ "$PWD" =~ ^"$HOME" ]]; then
prompt_pwd="~/${PWD##*${HOME}/}"
else
prompt_pwd="$PWD"
fi
prompt=(
"${c_reset}"
"[${last_exitcode_indicator}]"
"${user_colour}${USER}${c_reset}@${c_lightblue}${HOSTNAME}"
"$prompt_pwd"
"${git_prompt_msg}${user_colour}${user_indicator}"
"$c_reset"
)
PS1="${prompt[@]}"
}