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