rbenv.plugin.zsh 1.9 KB

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