Skip to content

Instantly share code, notes, and snippets.

@Ivoz

Ivoz/prompt.sh

Created Mar 12, 2014
Embed
What would you like to do?
#!/usr/bin/bash
# Reset
Reset='\[\e[0m\]'
# Regular Colors
Black='\[\e[0;30m\]'
Red='\[\e[0;31m\]'
Green='\[\e[0;32m\]'
Yellow='\[\e[0;33m\]'
Blue='\[\e[0;34m\]'
Purple='\[\e[0;35m\]'
Cyan='\[\e[0;36m\]'
White='\[\e[0;37m\]'
# Bold
BBlack='\[\e[1;30m\]'
BRed='\[\e[1;31m\]'
BGreen='\[\e[1;32m\]'
BYellow='\[\e[1;33m\]'
BBlue='\[\e[1;34m\]'
BPurple='\[\e[1;35m\]'
BCyan='\[\e[1;36m\]'
BWhite='\[\e[1;37m\]'
# High Intensity
IBlack='\[\e[0;90m\]'
IRed='\[\e[0;91m\]'
IGreen='\[\e[0;92m\]'
IYellow='\[\e[0;93m\]'
IBlue='\[\e[0;94m\]'
IPurple='\[\e[0;95m\]'
ICyan='\[\e[0;96m\]'
IWhite='\[\e[0;97m\]'
function set_ret_val() {
if [[ $1 == 0 ]]; then
P_RET_VAL=""
else
P_RET_VAL="${BRed}$1${Reset} "
fi
}
function set_user_id() {
P_USER="${Cyan}\u${Reset}${Yellow}·${Blue}\h${Reset} "
}
function set_virtualenv() {
if test -z "$VIRTUAL_ENV" ; then
P_VENV=""
else
P_VENV="${BBlue}`basename \"$VIRTUAL_ENV\"`${Reset} "
fi
}
function is_git_repo() {
git branch > /dev/null 2>&1
}
function is_hg_repo() {
hg status > /dev/null 2>&1
}
function set_vcs() {
if is_git_repo; then
git_status=$(git status 2> /dev/null)
# Set color based on clean/staged/dirty.
if [[ ${git_status} =~ "working directory clean" ]]; then
state="${BGreen}"
elif [[ ${git_status} =~ "Changes to be committed" ]]; then
state="${BYelloW}"
else
state="${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
branch_pattern="(# )?On branch ([^${IFS}]*)"
if [[ ${git_status} =~ ${branch_pattern} ]]; then
branch=${BASH_REMATCH[2]}
fi
P_VCS="${state}${branch}${remote}${Reset} "
eval "$1='±'"
elif is_hg_repo; then
hg_status=$(hg summary 2> /dev/null)
state="${BGreen}"
branch_pattern="branch: ([^${IFS}]*)"
if [[ ${hg_status} =~ ${branch_pattern} ]]; then
branch=${BASH_REMATCH[1]}
fi
P_VCS="${state}${branch}${Reset} "
eval "$1='${BWhite}${Reset}'"
else
P_VCS=""
eval "$1='»'"
fi
}
function set_prompt_symbol() {
if [[ $UID == 0 ]]; then
P_SYMBOL="# "
else
P_SYMBOL="$1 "
fi
}
function bash_prompt() {
set_ret_val $?
set_user_id
set_virtualenv
set_vcs vcs_symbol
set_prompt_symbol $vcs_symbol
P_WORKDIR="$Yellow\w$Reset "
PS1=" ${P_RET_VAL}${P_VENV}${P_USER}${P_WORKDIR}${P_VCS}${P_SYMBOL}"
}
PROMPT_COMMAND=bash_prompt
VIRTUAL_ENV_DISABLE_PROMPT=True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment