rbenv.plugin.zsh 1.6 KB

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