rbenv.plugin.zsh 1.7 KB

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