chruby.plugin.zsh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #
  2. # INSTRUCTIONS
  3. #
  4. # With either a manual or brew installed chruby things should just work.
  5. #
  6. # If you'd prefer to specify an explicit path to load chruby from
  7. # you can set variables like so:
  8. #
  9. # zstyle :omz:plugins:chruby path /local/path/to/chruby.sh
  10. # zstyle :omz:plugins:chruby auto /local/path/to/auto.sh
  11. #
  12. # TODO
  13. # - autodetermine correct source path on non OS X systems
  14. # - completion if ruby-install exists
  15. # rvm and rbenv plugins also provide this alias
  16. alias rubies='chruby'
  17. local _chruby_path
  18. local _chruby_auto
  19. _homebrew-installed() {
  20. whence brew &> /dev/null
  21. }
  22. _chruby-from-homebrew-installed() {
  23. [ -r $(brew --prefix chruby)] &> /dev/null
  24. }
  25. _ruby-build_installed() {
  26. whence ruby-build &> /dev/null
  27. }
  28. _ruby-install-installed() {
  29. whence ruby-install &> /dev/null
  30. }
  31. # Simple definition completer for ruby-build
  32. if _ruby-build_installed; then
  33. _ruby-build() { compadd $(ruby-build --definitions) }
  34. compdef _ruby-build ruby-build
  35. fi
  36. _source_from_omz_settings() {
  37. zstyle -s :omz:plugins:chruby path _chruby_path
  38. zstyle -s :omz:plugins:chruby auto _chruby_auto
  39. if ${_chruby_path} && [[ -r ${_chruby_path} ]]; then
  40. source ${_chruby_path}
  41. fi
  42. if ${_chruby_auto} && [[ -r ${_chruby_auto} ]]; then
  43. source ${_chruby_auto}
  44. fi
  45. }
  46. _chruby_dirs() {
  47. chrubydirs=($HOME/.rubies/ $PREFIX/opt/rubies)
  48. for dir in chrubydirs; do
  49. if [[ -d $dir ]]; then
  50. RUBIES+=$dir
  51. fi
  52. done
  53. }
  54. if _homebrew-installed && _chruby-from-homebrew-installed ; then
  55. source $(brew --prefix chruby)/share/chruby/chruby.sh
  56. source $(brew --prefix chruby)/share/chruby/auto.sh
  57. _chruby_dirs
  58. elif [[ -r "/usr/local/share/chruby/chruby.sh" ]] ; then
  59. source /usr/local/share/chruby/chruby.sh
  60. source /usr/local/share/chruby/auto.sh
  61. _chruby_dirs
  62. else
  63. _source_from_omz_settings
  64. _chruby_dirs
  65. fi
  66. function ensure_chruby() {
  67. $(whence chruby)
  68. }
  69. function current_ruby() {
  70. local _ruby
  71. _ruby="$(chruby |grep \* |tr -d '* ')"
  72. if [[ $(chruby |grep -c \*) -eq 1 ]]; then
  73. echo ${_ruby}
  74. else
  75. echo "system"
  76. fi
  77. }
  78. function chruby_prompt_info() {
  79. echo "$(current_ruby)"
  80. }
  81. # complete on installed rubies
  82. _chruby() { compadd $(chruby | tr -d '* ') }
  83. compdef _chruby chruby