From 5c14474eb2a252ad61e55cf084c5bbe6c1c934b9 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Mon, 13 Dec 2021 17:37:44 +0100 Subject: style(chruby): fix plugin code style and loading process --- plugins/chruby/README.md | 3 +- plugins/chruby/chruby.plugin.zsh | 165 ++++++++++++++++----------------------- 2 files changed, 71 insertions(+), 97 deletions(-) (limited to 'plugins/chruby') diff --git a/plugins/chruby/README.md b/plugins/chruby/README.md index d373006a5..19eb15a8d 100644 --- a/plugins/chruby/README.md +++ b/plugins/chruby/README.md @@ -5,6 +5,7 @@ current Ruby version, and completion and a prompt function to display the Ruby v Supports brew and manual installation of chruby. To use it, add `chruby` to the plugins array in your zshrc file: + ```zsh plugins=(... chruby) ``` @@ -14,7 +15,7 @@ plugins=(... chruby) 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/plugins/chruby/chruby.plugin.zsh b/plugins/chruby/chruby.plugin.zsh index 32f0525aa..61ded3b73 100644 --- a/plugins/chruby/chruby.plugin.zsh +++ b/plugins/chruby/chruby.plugin.zsh @@ -1,121 +1,94 @@ -# -# INSTRUCTIONS -# -# With either a manual or brew installed chruby things should just work. -# -# If you'd prefer to specify an explicit path to load chruby from -# you can set variables like so: -# -# zstyle :omz:plugins:chruby path /local/path/to/chruby.sh -# zstyle :omz:plugins:chruby auto /local/path/to/auto.sh -# -# TODO -# - autodetermine correct source path on non OS X systems -# - completion if ruby-install exists +## load chruby from different locations -# rvm and rbenv plugins also provide this alias -alias rubies='chruby' +_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 -_homebrew-installed() { - whence brew &> /dev/null - _xit=$? - if [ $_xit -eq 0 ];then - # ok , we have brew installed - # speculatively we 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 - return 0 - else - return $_xit - fi + if [[ -r ${_chruby_auto} ]]; then + source ${_chruby_auto} + fi } -_chruby-from-homebrew-installed() { - [ -r $_brew_prefix ] &> /dev/null -} +_source-from-homebrew() { + (( $+commands[brew] )) || return 1 -_ruby-build_installed() { - whence ruby-build &> /dev/null -} + 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 -_ruby-install-installed() { - whence ruby-install &> /dev/null -} + [[ -r "$_brew_prefix" ]] || return 1 -# Simple definition completer for ruby-build -if _ruby-build_installed; then - _ruby-build() { compadd $(ruby-build --definitions) } - compdef _ruby-build ruby-build -fi - -_source_from_omz_settings() { - local _chruby_path - local _chruby_auto - - zstyle -s :omz:plugins:chruby path _chruby_path - zstyle -s :omz:plugins:chruby auto _chruby_auto - - if [[ -r ${_chruby_path} ]]; then - source ${_chruby_path} - fi - - if [[ -r ${_chruby_auto} ]]; then - source ${_chruby_auto} - fi + source $_brew_prefix/share/chruby/chruby.sh + source $_brew_prefix/share/chruby/auto.sh } -_chruby_dirs() { - chrubydirs=($HOME/.rubies/ $PREFIX/opt/rubies) - for dir in chrubydirs; do - if [[ -d $dir ]]; then - RUBIES+=$dir - fi - done +_load-chruby-dirs() { + local dir + for dir in "$HOME/.rubies" "$PREFIX/opt/rubies"; do + if [[ -d "$dir" ]]; then + RUBIES+=("$dir") + fi + done } -if _homebrew-installed && _chruby-from-homebrew-installed ; then - source $_brew_prefix/share/chruby/chruby.sh - source $_brew_prefix/share/chruby/auto.sh - _chruby_dirs +# 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 - _chruby_dirs -else - _source_from_omz_settings - _chruby_dirs + 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 -function ensure_chruby() { - $(whence chruby) -} +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 + 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)" + echo "$(current_ruby)" } -# complete on installed rubies +# Complete chruby command with installed rubies _chruby() { - compadd $(chruby | tr -d '* ') - local default_path='/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin' - if PATH=${default_path} type ruby &> /dev/null; then - compadd system - fi + 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 -- cgit v1.2.3-70-g09d2 From 9a3d853481645ae0f961e9cc8421fc5d84e2c3c3 Mon Sep 17 00:00:00 2001 From: Marc Cornellà Date: Mon, 13 Dec 2021 17:43:32 +0100 Subject: fix: quote % characters in ruby prompt info functions --- lib/prompt_info_functions.zsh | 2 +- plugins/chruby/chruby.plugin.zsh | 2 +- plugins/rbenv/rbenv.plugin.zsh | 4 ++-- plugins/rbfu/rbfu.plugin.zsh | 3 ++- 4 files changed, 6 insertions(+), 5 deletions(-) (limited to 'plugins/chruby') diff --git a/lib/prompt_info_functions.zsh b/lib/prompt_info_functions.zsh index 48f033da6..e5535848b 100644 --- a/lib/prompt_info_functions.zsh +++ b/lib/prompt_info_functions.zsh @@ -30,7 +30,7 @@ function rvm_prompt_info() { local rvm_prompt rvm_prompt=$($HOME/.rvm/bin/rvm-prompt ${=ZSH_THEME_RVM_PROMPT_OPTIONS} 2>/dev/null) [[ -z "${rvm_prompt}" ]] && return 1 - echo "${ZSH_THEME_RUBY_PROMPT_PREFIX}${rvm_prompt}${ZSH_THEME_RUBY_PROMPT_SUFFIX}" + echo "${ZSH_THEME_RUBY_PROMPT_PREFIX}${rvm_prompt:gs/%/%%}${ZSH_THEME_RUBY_PROMPT_SUFFIX}" } ZSH_THEME_RVM_PROMPT_OPTIONS="i v g" diff --git a/plugins/chruby/chruby.plugin.zsh b/plugins/chruby/chruby.plugin.zsh index 61ded3b73..d7a28d4e2 100644 --- a/plugins/chruby/chruby.plugin.zsh +++ b/plugins/chruby/chruby.plugin.zsh @@ -73,7 +73,7 @@ function current_ruby() { } function chruby_prompt_info() { - echo "$(current_ruby)" + echo "${$(current_ruby):gs/%/%%}" } # Complete chruby command with installed rubies diff --git a/plugins/rbenv/rbenv.plugin.zsh b/plugins/rbenv/rbenv.plugin.zsh index d36d4922c..d758aebae 100644 --- a/plugins/rbenv/rbenv.plugin.zsh +++ b/plugins/rbenv/rbenv.plugin.zsh @@ -47,7 +47,7 @@ if [[ $FOUND_RBENV -eq 1 ]]; then } function rbenv_prompt_info() { - local ruby=$(current_ruby) gemset=$(current_gemset) + local ruby=${$(current_ruby):gs/%/%%} gemset=${$(current_gemset):gs/%/%%} echo -n "${ZSH_THEME_RUBY_PROMPT_PREFIX}" [[ -n "$gemset" ]] && echo -n "${ruby}@${gemset}" || echo -n "${ruby}" echo "${ZSH_THEME_RUBY_PROMPT_SUFFIX}" @@ -60,7 +60,7 @@ else function gems() { echo "not supported" } function rbenv_prompt_info() { echo -n "${ZSH_THEME_RUBY_PROMPT_PREFIX}" - echo -n "system: $(ruby -v | cut -f-2 -d ' ')" + echo -n "system: $(ruby -v | cut -f-2 -d ' ' | sed 's/%/%%/g')" echo "${ZSH_THEME_RUBY_PROMPT_SUFFIX}" } fi diff --git a/plugins/rbfu/rbfu.plugin.zsh b/plugins/rbfu/rbfu.plugin.zsh index c973fbf9f..27dc3eec6 100644 --- a/plugins/rbfu/rbfu.plugin.zsh +++ b/plugins/rbfu/rbfu.plugin.zsh @@ -45,4 +45,5 @@ function rbfu-rubies() { # Public: Create rvm_prompt_info command for themes compatibility, unless # it has already been defined. -[ ! -x rvm_prompt_info ] && function rvm_prompt_info() { echo "${RBFU_RUBY_VERSION:=system}" } +(( ${+functions[rvm_prompt_info]} )) || \ +function rvm_prompt_info() { echo "${${RBFU_RUBY_VERSION:=system}:gs/%/%%}" } -- cgit v1.2.3-70-g09d2