chruby.plugin.zsh 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. ## load chruby from different locations
  2. _source-from-omz-settings() {
  3. local _chruby_path _chruby_auto
  4. zstyle -s :omz:plugins:chruby path _chruby_path || return 1
  5. zstyle -s :omz:plugins:chruby auto _chruby_auto || return 1
  6. if [[ -r ${_chruby_path} ]]; then
  7. source ${_chruby_path}
  8. fi
  9. if [[ -r ${_chruby_auto} ]]; then
  10. source ${_chruby_auto}
  11. fi
  12. }
  13. _source-from-homebrew() {
  14. (( $+commands[brew] )) || return 1
  15. local _brew_prefix
  16. # check default brew prefix
  17. if [[ -h /usr/local/opt/chruby ]];then
  18. _brew_prefix="/usr/local/opt/chruby"
  19. else
  20. # ok , it is not default prefix
  21. # this call to brew is expensive ( about 400 ms ), so at least let's make it only once
  22. _brew_prefix=$(brew --prefix chruby)
  23. fi
  24. [[ -r "$_brew_prefix" ]] || return 1
  25. source $_brew_prefix/share/chruby/chruby.sh
  26. source $_brew_prefix/share/chruby/auto.sh
  27. }
  28. _load-chruby-dirs() {
  29. local dir
  30. for dir in "$HOME/.rubies" "$PREFIX/opt/rubies"; do
  31. if [[ -d "$dir" ]]; then
  32. RUBIES+=("$dir")
  33. fi
  34. done
  35. }
  36. # Load chruby
  37. if _source-from-omz-settings; then
  38. _load-chruby-dirs
  39. elif [[ -r "/usr/local/share/chruby/chruby.sh" ]] ; then
  40. source /usr/local/share/chruby/chruby.sh
  41. source /usr/local/share/chruby/auto.sh
  42. _load-chruby-dirs
  43. elif _source-from-homebrew; then
  44. _load-chruby-dirs
  45. fi
  46. unfunction _source-from-homebrew _source-from-omz-settings _load-chruby-dirs
  47. ## chruby utility functions and aliases
  48. # rvm and rbenv plugins also provide this alias
  49. alias rubies='chruby'
  50. function current_ruby() {
  51. local ruby
  52. ruby="$(chruby | grep \* | tr -d '* ')"
  53. if [[ $(chruby | grep -c \*) -eq 1 ]]; then
  54. echo ${ruby}
  55. else
  56. echo "system"
  57. fi
  58. }
  59. function chruby_prompt_info() {
  60. echo "${$(current_ruby):gs/%/%%}"
  61. }
  62. # Complete chruby command with installed rubies
  63. _chruby() {
  64. compadd $(chruby | tr -d '* ')
  65. if PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" command ruby &>/dev/null; then
  66. compadd system
  67. fi
  68. }
  69. compdef _chruby chruby
  70. # Simple definition completer for ruby-build
  71. if command ruby-build &> /dev/null; then
  72. _ruby-build() { compadd $(ruby-build --definitions) }
  73. compdef _ruby-build ruby-build
  74. fi