Skip to content

Instantly share code, notes, and snippets.

@robbielamb
Created December 31, 2009 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robbielamb/266843 to your computer and use it in GitHub Desktop.
Save robbielamb/266843 to your computer and use it in GitHub Desktop.
# rvm-install added line:
if [[ -s /Users/lamb/.rvm/scripts/rvm ]] && [[ $rvm_loaded_flag != 1 ]] ; then source /Users/lamb/.rvm/scripts/rvm ; fi
# http://henrik.nyh.se/2008/12/git-dirty-prompt
# http://www.simplisticcomplexity.com/2008/03/13/show-your-git-branch-name-in-your-prompt/
# username@Machine ~/dev/dir[master]$ # clean working directory
# username@Machine ~/dev/dir[master*]$ # dirty working directory
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
BLUE="\[\033[0;34m\]"
GRAY="\[\033[1;30m\]"
CYAN="\[\033[1;36m\]"
LIGHT_RED="\[\033[1;31m\]"
LIGHT_GREEN="\[\033[1;32m\]"
WHITE="\[\033[1;37m\]"
LIGHT_GRAY="\[\033[0;37m\]"
COLOR_NONE="\[\e[0m\]"
COLOR_ARRAY=( $RED $YELLOW $GREEN $BLUE $GRAY $CYAN $LIGHT_RED $LIGHT_GREEN $WHITE $LIGHT_GRAY $COLOR_NONE )
ARRAY_LENGTH=11
function parse_git_branch {
git rev-parse --git-dir &> /dev/null
git_status="$(git status 2> /dev/null)"
branch_pattern="^# On branch ([^${IFS}]*)"
remote_pattern="# Your branch is (.*) of"
diverge_pattern="# Your branch and (.*) have diverged"
if [[ ! ${git_status}} =~ "working directory clean" ]]; then
state="${RED}⚡"
fi
# add an else if or two here if you want to get more specific
if [[ ${git_status} =~ ${remote_pattern} ]]; then
if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
remote="${YELLOW}↑"
else
remote="${YELLOW}↓"
fi
fi
if [[ ${git_status} =~ ${diverge_pattern} ]]; then
remote="${YELLOW}↕"
fi
if [[ ${git_status} =~ ${branch_pattern} ]]; then
branch=${BASH_REMATCH[1]}
echo "${LIGHT_RED}[${GREEN}${branch}${remote}${state}${LIGHT_RED}]"
fi
}
add(){
token="$1"
eval "${token}_flag=1" ; shift
if [[ ! -z "$format" ]] ; then
format="${format}-\$${token}"
else
format="\$${token}"
fi
}
function parse_ruby_version {
if [[ ! -z "$(which ruby 2>/dev/null | awk '/rvm/{print}')" ]] ; then
unset format
while [[ $# -gt 0 ]] ; do
token="$1" ; shift
case "$token" in
i|interpreter) add "interpreter" ;;
v|version) add "version" ;;
p|patchlevel) add "patchlevel" ;;
r|revision) add "revision" ;;
a|architecture) add "architecture" ;;
*) echo "Unrecognized command line option '$token' for $0" ; exit 1 ;;
esac
done
if [[ -z "$format" ]] ; then
add "interpreter"
add "version"
# add "patchlevel"
fi
ruby_string=$(dirname "$(which ruby 2>/dev/null)" | xargs dirname | xargs basename)
if [[ ! -z "$interpreter_flag" ]] ; then
interpreter="$(echo $ruby_string | awk -F'-' '{print $1}')"
fi
if [[ ! -z "$version_flag" ]] ; then
version="$(echo $ruby_string | awk -F'-' '{print $2}')"
fi
if [[ ! -z "$patchlevel_flag" ]] ; then
patchlevel=$(echo $ruby_string | awk -F'-' '{print $3}')
fi
if [[ ! -z "$architecture_flag" ]] ; then
architecture="$(echo "$(ruby -v)" | sed 's/^.*\[//' | sed 's/\].*$//')"
fi
command="prompt=\"${RED}[${COLOR_NONE}$format${RED}]${COLOR_NONE}\""
eval "$command"
echo "${prompt/-]/]}"
fi
}
function prompt_func() {
previous_return_value=$?;
rnd_color=$RANDOM%ARRAY_LENGTH
prompt="$(parse_ruby_version) $(parse_git_branch)${COLOR_NONE}\n${CYAN}<${COLOR_NONE}\u@\h${CYAN}> ${YELLOW}\w ${COLOR_NONE}"
# prompt="\u@\h ${YELLOW}\w $(parse_git_branch)${COLOR_NONE}"
# unicorn
if test $previous_return_value -eq 0
then
PS1="${prompt}➔ "
else
PS1="${prompt}${RED}➔${COLOR_NONE} "
fi
}
PROMPT_COMMAND=prompt_func
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment