Created
October 23, 2019 18:59
-
-
Save navarroaxel/63d08513797f31281c6ea491b3def5a0 to your computer and use it in GitHub Desktop.
PS1 with git
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# set a fancy prompt (non-color, unless we know we "want" color) | |
case "$TERM" in | |
xterm-color|*-256color) color_prompt=yes;; | |
esac | |
# uncomment for a colored prompt, if the terminal has the capability; turned | |
# off by default to not distract the user: the focus in a terminal window | |
# should be on the output of commands, not on the prompt | |
#force_color_prompt=yes | |
if [ -n "$force_color_prompt" ]; then | |
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then | |
# We have color support; assume it's compliant with Ecma-48 | |
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such | |
# a case would tend to support setf rather than setaf.) | |
color_prompt=yes | |
else | |
color_prompt= | |
fi | |
fi | |
# Add git status to the PS1 | |
ps1_git_status() { | |
branch=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/') | |
if [[ ! $branch ]]; then | |
exit 0; | |
fi | |
# Choose branch name color | |
if [[ $branch == "master" ]]; then | |
psgit=' (\e[1;30m'; | |
elif [[ $branch =~ "develop" ]]; then | |
psgit=" (\e[1;33m"; | |
elif [[ $branch =~ "feat" ]]; then | |
psgit=" (\e[1;35m"; | |
elif [[ $branch =~ "fix" ]]; then | |
psgit=" (\e[1;31m"; | |
elif [[ ! -z $branch ]]; then | |
psgit=" (\e[0;33m"; | |
fi | |
psgit+="$branch" | |
changes=$(git status --short --branch | wc -l) | |
if [[ "$changes" > 1 ]]; then | |
psgit+='*' | |
fi | |
psgit+='\033[00m)' | |
status=$(git status --short --branch | head -n 1) | |
ahead=$(echo $status | sed -n 's/^.*ahead \+\([0-9]\+\).*$/\1/p') | |
if [[ ! $ahead ]] ; then | |
ahead=0 | |
fi | |
behind=$(echo $status | sed -n 's/^.*behind \+\([0-9]\+\).*$/\1/p') | |
if [[ ! $behind ]] ; then | |
behind=0 | |
fi | |
if [[ "$ahead" > 0 || "$behind" > 0 ]] ; then | |
psgit+="[\e[0;32m$ahead\033[00m,\e[0;31m$behind\033[00m]" | |
fi | |
echo -e "$psgit" | |
} | |
if [ "$color_prompt" = yes ]; then | |
PS1='\[\033[1;32m\]\u@\h\[\033[00m\] \[\033[1;34m\]\w\[\033[00m\]$(ps1_git_status)\$ ' | |
else | |
PS1='\u@\h:\w\$ ' | |
fi | |
unset color_prompt force_color_prompt | |
# If this is an xterm set the title to user@host:dir | |
case "$TERM" in | |
xterm*|rxvt*) | |
PS1="\[\e]0;\u@\h: \w\a\]$PS1" | |
;; | |
*) | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment