Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
My New Bash Prompt with Git Status
green() {
printf "\e[32m${1}\e[00m"
}
light_red() {
printf "\e[91m${1}\e[00m"
}
radioactive="\u2622"
check_mark="\u2714"
x_mark="\u2715"
prompt_if_git() {
git status > /dev/null 2> /dev/null
if [ $? -ne 0 ]; then
return
fi
printf "$1"
}
prompt_git_branch() {
git status > /dev/null 2> /dev/null
if [ $? -ne 0 ]; then
return
fi
git branch 2> /dev/null | sed -e "/^[^*]/d" -e "s/* \(.*\)/\1/"
}
prompt_git_status() {
git status > /dev/null 2> /dev/null
if [ $? -ne 0 ]; then
return
fi
git diff-index --quiet HEAD 2> /dev/null
case $? in
0) state_char=`green $radioactive`;;
*) state_char=`light_red $radioactive` ;;
esac
printf "$state_char"
}
did_last_fail () {
case $? in
0) printf "$(green $check_mark)" ;;
*) printf "$(light_red $x_mark)" ;;
esac
}
export PS1="\$(did_last_fail) \u@\h \w \$(prompt_if_git [)\$(prompt_git_branch)\$(prompt_git_status)\$(prompt_if_git ])\n$ "
export PROMPT_DIRTRIM=4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.