rbenv.plugin.zsh 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. local ruby=$(current_ruby) gemset=$(current_gemset)
  42. echo -n "${ZSH_THEME_RUBY_PROMPT_PREFIX}"
  43. [[ -n "$gemset" ]] && echo -n "${ruby}@${gemset}" || echo -n "${ruby}"
  44. echo "${ZSH_THEME_RUBY_PROMPT_SUFFIX}"
  45. }
  46. else
  47. alias rubies="ruby -v"
  48. function gemsets() { echo "not supported" }
  49. function current_ruby() { echo "not supported" }
  50. function current_gemset() { echo "not supported" }
  51. function gems() { echo "not supported" }
  52. function rbenv_prompt_info() {
  53. echo -n "${ZSH_THEME_RUBY_PROMPT_PREFIX}"
  54. echo -n "system: $(ruby -v | cut -f-2 -d ' ')"
  55. echo "${ZSH_THEME_RUBY_PROMPT_SUFFIX}"
  56. }
  57. fi
  58. unset FOUND_RBENV rbenvdirs dir