rbenv.plugin.zsh
2016 bytes
1# This plugin loads rbenv into the current shell and provides prompt info via
2# the 'rbenv_prompt_info' function.
3
4FOUND_RBENV=$+commands[rbenv]
5
6if [[ $FOUND_RBENV -ne 1 ]]; then
7 rbenvdirs=("$HOME/.rbenv" "/usr/local/rbenv" "/opt/rbenv" "/usr/local/opt/rbenv")
8 for dir in $rbenvdirs; do
9 if [[ -d $dir/bin ]]; then
10 export PATH="$dir/bin:$PATH"
11 FOUND_RBENV=1
12 break
13 fi
14 done
15fi
16
17if [[ $FOUND_RBENV -ne 1 ]]; then
18 if (( $+commands[brew] )) && dir=$(brew --prefix rbenv 2>/dev/null); then
19 if [[ -d $dir/bin ]]; then
20 export PATH="$dir/bin:$PATH"
21 FOUND_RBENV=1
22 fi
23 fi
24fi
25
26if [[ $FOUND_RBENV -eq 1 ]]; then
27 eval "$(rbenv init --no-rehash - zsh)"
28
29 alias rubies="rbenv versions"
30 alias gemsets="rbenv gemset list"
31
32 function current_ruby() {
33 echo "$(rbenv version-name)"
34 }
35
36 function current_gemset() {
37 echo "$(rbenv gemset active 2>/dev/null)" | tr ' ' '+'
38 }
39
40 function gems() {
41 local rbenv_path=$(rbenv prefix)
42 gem list $@ | sed -E \
43 -e "s/\([0-9a-z, \.]+( .+)?\)/$fg[blue]&$reset_color/g" \
44 -e "s|$(echo $rbenv_path)|$fg[magenta]\$rbenv_path$reset_color|g" \
45 -e "s/$current_ruby@global/$fg[yellow]&$reset_color/g" \
46 -e "s/$current_ruby$current_gemset$/$fg[green]&$reset_color/g"
47 }
48
49 function rbenv_prompt_info() {
50 local ruby=${$(current_ruby):gs/%/%%} gemset=${$(current_gemset):gs/%/%%}
51 echo -n "${ZSH_THEME_RUBY_PROMPT_PREFIX}"
52 [[ -n "$gemset" ]] && echo -n "${ruby}@${gemset}" || echo -n "${ruby}"
53 echo "${ZSH_THEME_RUBY_PROMPT_SUFFIX}"
54 }
55else
56 alias rubies="ruby -v"
57 function gemsets() { echo "not supported" }
58 function current_ruby() { echo "not supported" }
59 function current_gemset() { echo "not supported" }
60 function gems() { echo "not supported" }
61 function rbenv_prompt_info() {
62 echo -n "${ZSH_THEME_RUBY_PROMPT_PREFIX}"
63 echo -n "system: $(ruby -v | cut -f-2 -d ' ' | sed 's/%/%%/g')"
64 echo "${ZSH_THEME_RUBY_PROMPT_SUFFIX}"
65 }
66fi
67
68unset FOUND_RBENV rbenvdirs dir