Skip to content

Instantly share code, notes, and snippets.

@benbenmushi
Last active January 20, 2021 15:37
Show Gist options
  • Save benbenmushi/c7c15a7aa254bc2c696329155428d815 to your computer and use it in GitHub Desktop.
Save benbenmushi/c7c15a7aa254bc2c696329155428d815 to your computer and use it in GitHub Desktop.
Bash prompt git colorized
# display git branch green or orange if some files are modified or staged
changed_file_count() {
git status --porcelain 2> /dev/null | wc -l
}
branch_color() {
fc=$(changed_file_count)
if [ $fc -eq 0 ]; then
printf "\033[38;5;34m"
else
printf "\033[38;5;166m"
fi
}
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[$(branch_color)\] $(parse_git_branch)\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$ '
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment