summaryrefslogtreecommitdiff
path: root/oh-my-zsh/plugins/chruby
diff options
context:
space:
mode:
Diffstat (limited to 'oh-my-zsh/plugins/chruby')
-rw-r--r--oh-my-zsh/plugins/chruby/README.md21
-rw-r--r--oh-my-zsh/plugins/chruby/chruby.plugin.zsh94
2 files changed, 115 insertions, 0 deletions
diff --git a/oh-my-zsh/plugins/chruby/README.md b/oh-my-zsh/plugins/chruby/README.md
new file mode 100644
index 0000000..19eb15a
--- /dev/null
+++ b/oh-my-zsh/plugins/chruby/README.md
@@ -0,0 +1,21 @@
+# chruby plugin
+
+This plugin loads [chruby](https://github.com/postmodern/chruby), a tool that changes the
+current Ruby version, and completion and a prompt function to display the Ruby version.
+Supports brew and manual installation of chruby.
+
+To use it, add `chruby` to the plugins array in your zshrc file:
+
+```zsh
+plugins=(... chruby)
+```
+
+## Usage
+
+If you'd prefer to specify an explicit path to load chruby from
+you can set variables like so:
+
+```zsh
+zstyle :omz:plugins:chruby path /local/path/to/chruby.sh
+zstyle :omz:plugins:chruby auto /local/path/to/auto.sh
+```
diff --git a/oh-my-zsh/plugins/chruby/chruby.plugin.zsh b/oh-my-zsh/plugins/chruby/chruby.plugin.zsh
new file mode 100644
index 0000000..d7a28d4
--- /dev/null
+++ b/oh-my-zsh/plugins/chruby/chruby.plugin.zsh
@@ -0,0 +1,94 @@
+## load chruby from different locations
+
+_source-from-omz-settings() {
+ local _chruby_path _chruby_auto
+
+ zstyle -s :omz:plugins:chruby path _chruby_path || return 1
+ zstyle -s :omz:plugins:chruby auto _chruby_auto || return 1
+
+ if [[ -r ${_chruby_path} ]]; then
+ source ${_chruby_path}
+ fi
+
+ if [[ -r ${_chruby_auto} ]]; then
+ source ${_chruby_auto}
+ fi
+}
+
+_source-from-homebrew() {
+ (( $+commands[brew] )) || return 1
+
+ local _brew_prefix
+ # check default brew prefix
+ if [[ -h /usr/local/opt/chruby ]];then
+ _brew_prefix="/usr/local/opt/chruby"
+ else
+ # ok , it is not default prefix
+ # this call to brew is expensive ( about 400 ms ), so at least let's make it only once
+ _brew_prefix=$(brew --prefix chruby)
+ fi
+
+ [[ -r "$_brew_prefix" ]] || return 1
+
+ source $_brew_prefix/share/chruby/chruby.sh
+ source $_brew_prefix/share/chruby/auto.sh
+}
+
+_load-chruby-dirs() {
+ local dir
+ for dir in "$HOME/.rubies" "$PREFIX/opt/rubies"; do
+ if [[ -d "$dir" ]]; then
+ RUBIES+=("$dir")
+ fi
+ done
+}
+
+# Load chruby
+if _source-from-omz-settings; then
+ _load-chruby-dirs
+elif [[ -r "/usr/local/share/chruby/chruby.sh" ]] ; then
+ source /usr/local/share/chruby/chruby.sh
+ source /usr/local/share/chruby/auto.sh
+ _load-chruby-dirs
+elif _source-from-homebrew; then
+ _load-chruby-dirs
+fi
+
+unfunction _source-from-homebrew _source-from-omz-settings _load-chruby-dirs
+
+
+## chruby utility functions and aliases
+
+# rvm and rbenv plugins also provide this alias
+alias rubies='chruby'
+
+function current_ruby() {
+ local ruby
+ ruby="$(chruby | grep \* | tr -d '* ')"
+ if [[ $(chruby | grep -c \*) -eq 1 ]]; then
+ echo ${ruby}
+ else
+ echo "system"
+ fi
+}
+
+function chruby_prompt_info() {
+ echo "${$(current_ruby):gs/%/%%}"
+}
+
+# Complete chruby command with installed rubies
+_chruby() {
+ compadd $(chruby | tr -d '* ')
+ if PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" command ruby &>/dev/null; then
+ compadd system
+ fi
+}
+
+compdef _chruby chruby
+
+
+# Simple definition completer for ruby-build
+if command ruby-build &> /dev/null; then
+ _ruby-build() { compadd $(ruby-build --definitions) }
+ compdef _ruby-build ruby-build
+fi