rbenv.plugin.zsh 1.6 KB

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