Páginas

Saturday, August 27, 2011

Integrating git and rvm with prompt

Here's a quick hack to your prompt, that I find helpful when using git and rvm.

Add this to you .bash_profile, .bashrc, or whatever file you use to configure your shell:

export PROMPT_COMMAND='PS1="\[\033[0;33m\][\!] \h\`
if [[ \$? = "0" ]]; then echo "\\[\\033[32m\\]";
else echo "\\[\\033[31m\\]"; fi
\` \`
if [[ `pwd|wc -c|tr -d " "` > 60 ]]; then echo ":..$(expr "$PWD" : ".*\(/.*.\{40\}\)")";
else echo ${PWD/\/Users\/$(whoami)/\~}; fi
\`\[\] $(parse_git_branch)\[\]\[\033[0m\]$(rvm_version.sh)\[\033[0m\]: ";
echo -ne "\033]0;`hostname -s`:`pwd`\007"'


Now you need the parse_git_branch and rvm_version.sh.

Parse Git Branch

parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/[\1]/'
}


RVM Version

ruby_version=$(/usr/local/rvm/bin/rvm current)
if [ -f "Gemfile" ] # If it is a rails project
then
  if [ -e "app/assets" ] # If rails version >= 3.1
  then
    if [[ $ruby_version != *1.9* ]] # If not using correct rvm version
    then
      echo -n "\\[\\033[31m\\]"
    else
      echo -n "\\[\\033[32m\\]"
    fi
  else # If rails version >= 3.1
    if [[ $ruby_version != *1.8* ]]
    then
      echo -n "\\[\\033[31m\\]"
    else
      echo -n "\\[\\033[32m\\]"
    fi
  fi
  echo -n [$(/usr/local/rvm/bin/rvm current)]
else
  echo -n "\\[\\033[32m\\]"
fi


Put them somewhere in some directory that's in your PATH, and it should work. What it does, as far as git and rvm go, is tell what branch your on, and if your using the correct rvm for your rails project (turns red if your not).

I'll probably work a little more on the rvm_version, that is the only thing that I can actually get credit for, but for now it works.

No comments: