-
-
Save mkohler/4597990 to your computer and use it in GitHub Desktop.
bash prompt indicating git status, git branch, active virtualenv, host, working directory, and exit code of last command.
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
#!/bin/bash | |
# | |
# DESCRIPTION: | |
# | |
# Set the bash prompt according to: | |
# * the active virtualenv | |
# * the branch/status of the current git repository | |
# | |
# USAGE: | |
# | |
# 1. Save this file as ~/.bash_prompt | |
# 2. Add the following line to the end of your ~/.bashrc or ~/.bash_profile: | |
# . ~/.bash_prompt | |
# | |
# LINEAGE: | |
# | |
# Based on: | |
# | |
# http://opinionated-programmer.com/2011/01/colorful-bash-prompt-reflecting-git-status/ | |
# https://gist.github.com/4597990 | |
# The various escape codes that we can use to color our prompt. | |
RED="\[\e[0;31m\]" | |
GREEN="\[\e[0;32m\]" | |
YELLOW="\[\e[0;33m\]" | |
BLUE="\[\e[0;34m\]" | |
MAGENTA="\[\e[0;35m\]" | |
CYAN="\[\e[0;36m\]" | |
GRAY="\[\e[0;37m\]" | |
LIGHT_RED="\[\e[1;31m\]" | |
LIGHT_GREEN="\[\e[1;32m\]" | |
LIGHT_YELLOW="\[\e[1;33m\]" | |
LIGHT_BLUE="\[\e[1;34m\]" | |
LIGHT_MAGENTA="\[\e[1;35m\]" | |
LIGHT_CYAN="\[\e[1;36m\]" | |
WHITE="\[\e[1;37m\]" | |
WHITE_ON_GREEN="\[\e[42;1;37m\]" | |
WHITE_ON_YELLOW="\[\e[43;1;37m\]" | |
WHITE_ON_MAGENTA="\[\e[45;1;37m\]" | |
WHITE_ON_RED="\[\e[41;1;37m\]" | |
COLOR_NONE="\[\e[0m\]" | |
# Determine the branch/state information for this git repository. | |
function set_git_branch { | |
# Capture the output of the "git status" command. | |
local git_status="$(git status -unormal 2>&1)" | |
if [[ "$git_status" =~ Not\ a\ git\ repo ]]; then | |
BRANCH='' | |
return | |
fi | |
if [[ ${git_status} =~ "working directory clean" ]]; then | |
state="${WHITE_ON_GREEN}" | |
elif [[ ${git_status} =~ "nothing added to commit but untracked files present" ]]; then | |
state="${WHITE_ON_YELLOW}" | |
elif [[ ${git_status} =~ "Changes to be committed" ]]; then | |
state="${WHITE_ON_MAGENTA}" | |
else | |
state="${WHITE_ON_RED}" | |
fi | |
# Set arrow icon based on status against remote. | |
remote_pattern="# Your branch is (.*) of" | |
if [[ ${git_status} =~ ${remote_pattern} ]]; then | |
if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then | |
remote="↑" | |
else | |
remote="↓" | |
fi | |
else | |
remote="" | |
fi | |
diverge_pattern="# Your branch and (.*) have diverged" | |
if [[ ${git_status} =~ ${diverge_pattern} ]]; then | |
remote="↕" | |
fi | |
# Get the name of the branch. | |
branch_pattern="^# On branch ([^${IFS}]*)" | |
if [[ ${git_status} =~ ${branch_pattern} ]]; then | |
branch=${BASH_REMATCH[1]} | |
fi | |
# If branch is master, just use a space. | |
if [[ ${branch} = "master" ]]; then | |
branch=' ' | |
else | |
branch=(${branch}) | |
fi | |
# Set the final branch string. | |
BRANCH="${state}${branch}${remote}${COLOR_NONE} " | |
} | |
# Return the prompt symbol to use, colorized based on the return value of the | |
# previous command. | |
function set_prompt_symbol () { | |
if test $1 -eq 0 ; then | |
PROMPT_SYMBOL="\$" | |
else | |
PROMPT_SYMBOL="${WHITE_ON_RED}!${COLOR_NONE}" | |
fi | |
} | |
# Determine active Python virtualenv details. | |
function set_virtualenv () { | |
if test -z "$VIRTUAL_ENV" ; then | |
PYTHON_VIRTUALENV="" | |
else | |
PYTHON_VIRTUALENV="[`basename \"$VIRTUAL_ENV\"`]" | |
fi | |
} | |
# Set the full bash prompt. | |
function set_bash_prompt () { | |
# Set the PROMPT_SYMBOL variable. We do this first so we don't lose the | |
# return value of the last command. | |
set_prompt_symbol $? | |
# Set the PYTHON_VIRTUALENV variable. | |
set_virtualenv | |
# Set the BRANCH variable. | |
set_git_branch | |
# Set the bash prompt variable. | |
PS1="${BRANCH}${PYTHON_VIRTUALENV}${COLOR_NONE}\h:\w${PROMPT_SYMBOL} " | |
} | |
# Tell bash to execute this function just before displaying its prompt. | |
PROMPT_COMMAND=set_bash_prompt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment