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