Skip to content

Instantly share code, notes, and snippets.

@adamkittelson
Last active December 31, 2015 21:39
Show Gist options
  • Save adamkittelson/8048654 to your computer and use it in GitHub Desktop.
Save adamkittelson/8048654 to your computer and use it in GitHub Desktop.
# Set the shell format to:
# /path/to/pwd (git branch) $ <input>
RED="\[\033[0;31m\]"
GREEN="\[\033[0;32m\]"
PURPLE="\[\033[0;34m\]"
YELLOW="\[\033[1;33m\]"
PINK="\[\033[0;35m\]"
CYAN="\[\033[0;36m\]"
WHITE="\[\033[1;37m\]"
NORM="\[\033[0m\]"
PATH_COLOR=$PINK
BRANCH_COLOR=$CYAN
BRANCH_STATE_COLOR=$RED
REPOSITORY_REMOTE_COLOR=$RED
PROMPT_COLOR=$GREEN
# parse_git_branch() {
# branch=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1/"`
# if [ -n "$branch" ]
# then
# echo "($branch) "
# fi
# }
parse_git_status(){
[ -d .git ] && echo $(git status 2> /dev/null)
}
git_branch_opening_paren() {
[ -d .git ] && echo " ("
}
git_branch_closing_paren() {
[ -d .git ] && echo ")"
}
parse_git_branch() {
[ -d .git ] || return 1
ref=$(git symbolic-ref HEAD 2> /dev/null) || return
echo ${ref#refs/heads/}
}
parse_git_remote_status() {
[ -d .git ] || return 1
git_status=$(parse_git_status)
remote_pattern="# Your branch is (.*) of"
diverge_pattern="# Your branch and (.*) have diverged"
if [[ ${git_status} =~ ${remote_pattern} ]]; then
if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
echo " ↑"
else
echo " ↓"
fi
else
if [[ ${git_status} =~ ${diverge_pattern} ]]; then
echo " ↕"
fi
fi
}
parse_git_state() {
[ -d .git ] || return 1
if [[ ! $(parse_git_status) =~ "working directory clean" ]]; then
echo " *"
fi
}
# export PS1="\w\$(parse_git_branch) ➡ "
PS1="\n$PATH_COLOR\w$BRANCH_COLOR\$(git_branch_opening_paren)\$(parse_git_branch)$BRANCH_STATE_COLOR\$(parse_git_state)$REPOSITORY_REMOTE_COLOR\$(parse_git_remote_status)$BRANCH_COLOR\$(git_branch_closing_paren) $PROMPT_COLOR\$$NORM "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment