Last active
June 2, 2022 13:18
-
-
Save vikalpj/e5c438631f4868d56ea3a1c64b392d2e to your computer and use it in GitHub Desktop.
Terminal promt to show python env and git branches states
This file contains 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
# Add this to your bash profile. | |
RED="\[\033[0;31m\]" | |
YELLOW="\[\033[0;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 tree 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="${PYTHON_VIRTUALENV}$PURPLE\$(set_rvm_prompt)$NO_COLOUR\u@\h:[\W]${B_STATE}\$(parse_git_branch)$NO_COLOUR\$ " | |
} | |
if [ -z "$PROMPT_COMMAND" ]; then | |
PROMPT_COMMAND=prompt_cmd | |
else | |
# Make compatible with mac terminal | |
PROMPT_COMMAND="${PROMPT_COMMAND}; prompt_cmd;" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment