Skip to content

Instantly share code, notes, and snippets.

@shovon
Last active December 21, 2015 13:59
Show Gist options
  • Save shovon/6316451 to your computer and use it in GitHub Desktop.
Save shovon/6316451 to your computer and use it in GitHub Desktop.
Adds a fancy theme to the default bash shell.
# Some commonly used colors.
bash_reset_color='\[\e[0m\]'
bash_red='\[\e[0;31m\]'
bash_green='\[\e[0;32m\]'
bash_yellow='\[\e[0;33m\]'
bash_blue='\[\e[0;34m\]'
bash_purple='\[\e[0;35m\]'
bash_cyan='\[\e[0;36m\]'
# Displays the top-level direcory of the currently working directory.
bpwd() {
echo `basename $(pwd)`
}
# Echoes 0 if there exists a git repo in the current working directory. A non-0
# is echoed otherwise.
git_prompt_exists() {
git status &> /dev/null
echo $?
}
# Echoes the branch of the git repo, of the current repository.
git_prompt_branch() {
echo $(git rev-parse --abbrev-ref HEAD 2> /dev/null)
}
# Echoes the status of the git repo in the current working directory.
git_prompt_status() {
INDEX=$(git status --porcelain -b)
STATUS=""
if [ "$(echo "$INDEX" | grep -E '^\?\? ')" != "" ]; then
STATUS="$BASH_THEME_GIT_PROMPT_UNTRACKED$STATUS"
fi
if [ "$(echo "$INDEX" | grep '^A ')" != "" ]; then
STATUS="$BASH_THEME_GIT_PROMPT_ADDED$STATUS"
elif [ "$(echo "$INDEX" | grep '^M ')" != "" ]; then
STATUS="$BASH_THEME_GIT_PROMPT_ADDED$STATUS"
fi
if [ "$(echo "$INDEX" | grep '^ M ')" != "" ]; then
STATUS="$BASH_THEME_GIT_PROMPT_MODIFIED$STATUS"
elif [ "$(echo "$INDEX" | grep '^AM ')" != "" ]; then
STATUS="$BASH_THEME_GIT_PROMPT_MODIFIED$STATUS"
elif [ "$(echo "$INDEX" | grep '^ T ')" != "" ]; then
STATUS="$BASH_THEME_GIT_PROMPT_MODIFIED$STATUS"
fi
if [ "$(echo "$INDEX" | grep '^R ')" != "" ]; then
STATUS="$BASH_THEME_GIT_PROMPT_RENAMED$STATUS"
fi
if [ "$(echo "$INDEX" | grep '^ D ')" != "" ]; then
STATUS="$BASH_THEME_GIT_PROMPT_DELETED$STATUS"
elif [ "$(echo "$INDEX" | grep '^D ')" != "" ]; then
STATUS="$BASH_THEME_GIT_PROMPT_DELETED$STATUS"
elif [ "$(echo "$INDEX" | grep '^AD ')" != "" ]; then
STATUS="$BASH_THEME_GIT_PROMPT_DELETED$STATUS"
fi
echo $STATUS
}
_git_prompt_status() {
local retval=`git_prompt_status`
if [ "$retval" ]; then
echo "($retval) "
fi
}
__prompt_command() {
local EXIT="$?"
BASH_THEME_GIT_PROMPT_ADDED=" ${bash_green}✚${bash_reset_color}"
BASH_THEME_GIT_PROMPT_MODIFIED=" ${bash_blue}✹${bash_reset_color}"
BASH_THEME_GIT_PROMPT_RENAMED=" ${bash_purple}➜${bash_reset_color}"
BASH_THEME_GIT_PROMPT_DELETED=" ${bash_red}✖${bash_reset_color}"
BASH_THEME_GIT_PROMPT_UNMERGED=" ${bash_yellow}═${bash_reset_color}"
BASH_THEME_GIT_PROMPT_UNTRACKED=" ${bash_cyan}✭${bash_reset_color}"
PS1="\n◦ "
if test $VIRTUAL_ENV; then
PS1+="(`basename $VIRTUAL_ENV`) "
fi
if [ "`pwd`" == $HOME ]; then
PS1+="${bash_blue}""~""${bash_reset_color} "
else
local basename=
PS1+="${bash_blue}"`basename "$(pwd)"`"${bash_reset_color} "
fi
if [ `git_prompt_exists` == 0 ]; then
PS1+="on ${bash_green}`git_prompt_branch`${bash_reset_color} `_git_prompt_status`"
fi
if [ $EXIT != 0 ]; then
PS1+="(${bash_red}$EXIT${bash_reset_color}) "
fi
PS1+="\n${bash_yellow}\$${bash_reset_color} "
}
export PROMPT_COMMAND=__prompt_command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment