chruby.plugin.zsh 2.2 KB

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