rbenv.plugin.zsh 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. _homebrew-installed() {
  2. type brew &> /dev/null
  3. }
  4. _rbenv-from-homebrew-installed() {
  5. brew --prefix rbenv &> /dev/null
  6. }
  7. FOUND_RBENV=0
  8. rbenvdirs=("$HOME/.rbenv" "/usr/local/rbenv" "/opt/rbenv")
  9. if _homebrew-installed && _rbenv-from-homebrew-installed ; then
  10. rbenvdirs=($(brew --prefix rbenv) "${rbenvdirs[@]}")
  11. fi
  12. for rbenvdir in "${rbenvdirs[@]}" ; do
  13. if [ -d $rbenvdir/bin -a $FOUND_RBENV -eq 0 ] ; then
  14. FOUND_RBENV=1
  15. export RBENV_ROOT=$rbenvdir
  16. export PATH=${rbenvdir}/bin:$PATH
  17. eval "$(rbenv init -)"
  18. alias rubies="rbenv versions"
  19. alias gemsets="rbenv gemset list"
  20. function current_ruby() {
  21. echo "$(rbenv version-name)"
  22. }
  23. function current_gemset() {
  24. echo "$(rbenv gemset active 2&>/dev/null | sed -e ":a" -e '$ s/\n/+/gp;N;b a' | head -n1)"
  25. }
  26. function gems {
  27. local rbenv_path=$(rbenv prefix)
  28. gem list $@ | sed \
  29. -Ee "s/\([0-9\.]+( .+)?\)/$fg[blue]&$reset_color/g" \
  30. -Ee "s|$(echo $rbenv_path)|$fg[magenta]\$rbenv_path$reset_color|g" \
  31. -Ee "s/$current_ruby@global/$fg[yellow]&$reset_color/g" \
  32. -Ee "s/$current_ruby$current_gemset$/$fg[green]&$reset_color/g"
  33. }
  34. function rbenv_prompt_info() {
  35. if [[ -n $(current_gemset) ]] ; then
  36. echo "$(current_ruby)@$(current_gemset)"
  37. else
  38. echo "$(current_ruby)"
  39. fi
  40. }
  41. fi
  42. done
  43. unset rbenvdir
  44. if [ $FOUND_RBENV -eq 0 ] ; then
  45. alias rubies='ruby -v'
  46. function gemsets() { echo 'not supported' }
  47. function rbenv_prompt_info() { echo "system: $(ruby -v | cut -f-2 -d ' ')" }
  48. fi