rbenv.plugin.zsh 1.93 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# This plugin loads rbenv into the current shell and provides prompt info via
# the 'rbenv_prompt_info' function.

FOUND_RBENV=$+commands[rbenv]

if [[ $FOUND_RBENV -ne 1 ]]; then
    rbenvdirs=("$HOME/.rbenv" "/usr/local/rbenv" "/opt/rbenv" "/usr/local/opt/rbenv")
    for dir in $rbenvdirs; do
        if [[ -d $dir/bin ]]; then
            export PATH="$dir/bin:$PATH"
            FOUND_RBENV=1
            break
        fi
    done
15
16
fi

17
18
19
20
21
22
if [[ $FOUND_RBENV -ne 1 ]]; then
    if (( $+commands[brew] )) && dir=$(brew --prefix rbenv 2>/dev/null); then
        if [[ -d $dir/bin ]]; then
            export PATH="$dir/bin:$PATH"
            FOUND_RBENV=1
        fi
23
    fi
24
25
26
fi

if [[ $FOUND_RBENV -eq 1 ]]; then
27
    eval "$(rbenv init --no-rehash - zsh)"
Peter Hoeg's avatar
Peter Hoeg committed
28
29
30
31
32

    alias rubies="rbenv versions"
    alias gemsets="rbenv gemset list"

    function current_ruby() {
33
        echo "$(rbenv version-name)"
Peter Hoeg's avatar
Peter Hoeg committed
34
35
36
    }

    function current_gemset() {
37
        echo "$(rbenv gemset active 2&>/dev/null | sed -e ":a" -e '$ s/\n/+/gp;N;b a' | head -n1)"
Peter Hoeg's avatar
Peter Hoeg committed
38
39
    }

40
41
42
43
44
45
46
    function gems() {
        local rbenv_path=$(rbenv prefix)
        gem list $@ | sed -E \
          -e "s/\([0-9a-z, \.]+( .+)?\)/$fg[blue]&$reset_color/g" \
          -e "s|$(echo $rbenv_path)|$fg[magenta]\$rbenv_path$reset_color|g" \
          -e "s/$current_ruby@global/$fg[yellow]&$reset_color/g" \
          -e "s/$current_ruby$current_gemset$/$fg[green]&$reset_color/g"
Peter Hoeg's avatar
Peter Hoeg committed
47
48
49
    }

    function rbenv_prompt_info() {
50
51
52
53
54
        if [[ -n $(current_gemset) ]] ; then
            echo "$(current_ruby)@$(current_gemset)"
        else
            echo "$(current_ruby)"
        fi
Peter Hoeg's avatar
Peter Hoeg committed
55
    }
56
57
58
59
60
61
62
else
    alias rubies="ruby -v"
    function gemsets() { echo "not supported" }
    function current_ruby() { echo "not supported" }
    function current_gemset() { echo "not supported" }
    function gems() { echo "not supported" }
    function rbenv_prompt_info() { echo "system: $(ruby -v | cut -f-2 -d ' ')" }
Peter Hoeg's avatar
Peter Hoeg committed
63
fi
64
65

unset FOUND_RBENV rbenvdirs dir