Skip to content

Instantly share code, notes, and snippets.

@mortik
Last active December 17, 2015 16:30
Show Gist options
  • Save mortik/5639680 to your computer and use it in GitHub Desktop.
Save mortik/5639680 to your computer and use it in GitHub Desktop.
My prompt for bash
DEFAULT_COLOR="[00m"
GRAY_COLOR="[37m"
PINK_COLOR="[35m"
GREEN_COLOR="[32m"
ORANGE_COLOR="[33m"
RED_COLOR="[31m"
if [ `id -u` == '0' ]; then
USER_COLOR=$RED_COLOR
else
USER_COLOR=$PINK_COLOR
fi
set_color() {
echo -n $' \e'
echo -n $1
}
ruby_version() {
ruby -v | awk '{print $2}'
}
parse_ruby() {
echo -n $' '
echo -n `set_color $RED_COLOR`
echo -n `ruby_version`
echo -n `set_color $DEFAULT_COLOR`
}
export RAILS_ENV=development
parse_rails_env() {
echo -n $' '
echo -n `set_color $PINK_COLOR`
echo -n ${RAILS_ENV:0:3}
echo -n `set_color $DEFAULT_COLOR`
}
git_branch() {
echo -n `git rev-parse --abbrev-ref HEAD`
}
git_remote() {
echo -n `git config branch.$git_branch.remote`
}
parse_git_upstream_dirty() {
git_remote=`git_remote $1`
git_status=`git status 2> /dev/null | tail -n1`
echo -n `set_color $RED_COLOR`
if [[ $(git log $git_remote/$1..$1 2> /dev/null) ]]; then
echo -n $'▵'
elif [[ ! "$git_status" =~ "nothing to commit" ]]; then
echo -n $'*'
fi
echo -n `set_color $DEFAULT_COLOR`
}
parse_git_branch() {
if [ -d .git ]; then
git_branch=`git_branch`
echo -n $' '
echo -n `set_color $ORANGE_COLOR`
echo -n $git_branch
echo -n `parse_git_upstream_dirty $git_branch`
echo -n `set_color $DEFAULT_COLOR`
fi
}
parse_virtualenv() {
if [ x$VIRTUAL_ENV != x ]; then
echo -n $' '
if [[ $VIRTUAL_ENV == *.virtualenvs/* ]]; then
ENV_NAME=`basename "${VIRTUAL_ENV}"`
else
folder=`dirname "${VIRTUAL_ENV}"`
ENV_NAME=`basename "$folder"`
fi
echo -n `set_color $RED_COLOR`
echo -n $ENV_NAME
echo -n `set_color $DEFAULT_COLOR`
fi
}
export BASEPROMPT='\n\e${USER_COLOR}\u\
\e${DEFAULT_COLOR} at \e${ORANGE_COLOR}\h \
\e${GREEN_COLOR}\w\
\e${DEFAULT_COLOR}\
`parse_ruby`\
`parse_git_branch`\
`parse_rails_env`\
`parse_virtualenv`'
export PS1="${BASEPROMPT}
[\t] $ "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment