Last active
July 2, 2020 20:42
-
-
Save maumercado/3354613 to your computer and use it in GitHub Desktop.
Git branch in prompt
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
RED="\[\033[0;31m\]" | |
YELLOW="\[\033[0;33m\]" | |
L_YELLOW="\[\033[1;33m\]" | |
GREEN="\[\033[0;32m\]" | |
BLUE="\[\033[1;34m\]" | |
NO_COLOUR="\[\033[0m\]" | |
CYAN="\[\033[0;36m\]" | |
PURPLE="\[\033[0;35m\]" | |
# Determine active Python virtualenv details. | |
function set_virtualenv () { | |
if test -z "$VIRTUAL_ENV" ; then | |
PYTHON_VIRTUALENV="" | |
else | |
PYTHON_VIRTUALENV="$CYAN(`basename \"$VIRTUAL_ENV\"`)${NO_COLOUR} " | |
fi | |
} | |
function set_rvm_prompt () { | |
local gemset=$(echo $GEM_HOME | awk -F'@' '{print $2}') | |
[ "$gemset" != "" ] && echo "(@$gemset) " | |
} | |
function parse_git_branch () { | |
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/' | |
} | |
function set_git_branch () { | |
# Capture the output of the "git status" command. | |
git_status="$(git status 2> /dev/null)" | |
# Set color based on clean/staged/dirty. | |
if [[ ${git_status} =~ .*"working directory clean".* ]]; then | |
B_STATE="${GREEN}" | |
elif [[ ${git_status} =~ .*"Changes to be committed".* ]]; then | |
B_STATE="${YELLOW}" | |
else | |
B_STATE="${RED}" | |
fi | |
} | |
prompt_cmd () { | |
set_virtualenv | |
set_git_branch | |
PS1="┌─\u@\h $L_YELLOW[\w]: $CYAN\${PYTHON_VIRTUALENV}$CYAN\$(set_rvm_prompt)${B_STATE}\$(parse_git_branch)$NO_COLOUR\n└─> " | |
} | |
PROMPT_COMMAND=prompt_cmd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment