Skip to content

Instantly share code, notes, and snippets.

@nbarthelemy
Last active October 23, 2018 15:40
Show Gist options
  • Save nbarthelemy/ae63a0e05874e37032a4e240d6c17470 to your computer and use it in GitHub Desktop.
Save nbarthelemy/ae63a0e05874e37032a4e240d6c17470 to your computer and use it in GitHub Desktop.
RVM or rbenv + git bash prompt config script
[[ -s ~/.bashrc ]] && . ~/.bashrc
[ -f $(brew --prefix)/etc/bash_completion ] && . $(brew --prefix)/etc/bash_completion
# This loads RVM into a shell session. ( optional )
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
# This load rbenv into the shell session ( optional )
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
source ~/.rbenv/completions/rbenv.bash
# configure bash_prompt
[[ -s ~/.bash_prompt ]] && source ~/.bash_prompt
if [ "$PS1" ]
then
__rvm_ps1 () {
local gemset=$(echo $GEM_HOME | awk -F'@' '{print $2}')
[ "$gemset" != "" ] && gemset="@$gemset"
local version=$(echo $MY_RUBY_HOME | awk -F'-' '{print $2}')
[ "$version" == "1.8.7" ] && version=""
local full="$version$gemset"
[ "$full" != "" ] && echo "$full$W"
}
__rbenv_ps1 () {
rbenv_ruby_version=`rbenv version | sed -e 's/ .*//'`
printf $rbenv_ruby_version
}
__ruby_manager_ps1 () {
if [ "$(ruby_manager_installed)" == "rvm" ]; then
echo "$(__rvm_ps1):"
elif [ "$(ruby_manager_installed)" == "rbenv" ]; then
echo "$(__rbenv_ps1):"
else
echo ""
fi
}
color_txt_blue='\e[0;34m'
color_txt_green='\e[0;32m'
color_txt_yellow='\e[0;33m'
color_txt_red='\e[0;31m'
color_txt_white='\e[0;37m'
color_txt_dark_grey='\e[90m'
color_txt_default='\e[39m'
ruby_manager_installed () {
if [[ -f `which rvm` ]]; then
echo "rvm"
elif [[ -f `which rbenv` ]]; then
echo "rbenv"
else
echo ""
fi
}
ruby_selected () {
[[ "$(ruby_manager_installed)" == "rvm" && `which ruby` != "/usr/bin/ruby" ]] ||
[[ "$(ruby_manager_installed)" == "rbenv" && `rbenv version` != "system "* ]]
}
ruby_version_manager_active () {
[ "$(ruby_manager_installed)" != "" ] && ruby_selected
}
ruby_version_name () {
if ruby_version_manager_active; then
echo "\[$color_txt_dark_grey\]$(__ruby_manager_ps1)"
else
echo ""
fi
}
git_ps1_available () {
[[ `type __git_ps1 | head -n 1` = "__git_ps1 is a function" ]]
}
git_repo_detected () {
git status &> /dev/null
}
git_dirty () {
git diff --quiet HEAD &>/dev/null
[ $? == 1 ] && echo "*"
}
git_branch_name () {
if git_ps1_available && git_repo_detected; then
echo "\[$(git_branch_color)\]$(__git_ps1 "(%s$(git_dirty))")"
fi
}
git_branch_color () {
case `git symbolic-ref --short HEAD` in
'master'|'production'|'prod')
echo $color_txt_red
;;
'development'|'dev'|'stage'|'staging')
echo $color_txt_yellow
;;
*)
echo $color_txt_green
;;
esac
}
current_working_dir () {
#echo " \[$color_txt_white\]\w"
echo "\[$color_txt_white\]\W"
}
bash_prompt_sign () {
echo "\[$color_txt_default\]\$"
}
current_user_with_host () {
if ruby_version_manager_active; then
echo ""
else
echo "\[$color_txt_dark_grey\]\u@\h:"
fi
}
set_bash_prompt () {
PS1="$(ruby_version_name)$(current_user_with_host)$(current_working_dir)$(git_branch_name) $(bash_prompt_sign) "
}
export PROMPT_COMMAND=set_bash_prompt
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment