chruby icon indicating copy to clipboard operation
chruby copied to clipboard

chruby does not work with iTerm2 Integration

Open pappalar opened this issue 8 years ago • 3 comments

Hello!

If https://www.iterm2.com/3.0/documentation-shell-integration.html are installed and sourced in the bash profile:

test -e "${HOME}/.iterm2_shell_integration.bash" && source "${HOME}/.iterm2_shell_integration.bash"

chruby stops working even if .ruby-version is present in a folder

pappalar avatar Jan 16 '18 16:01 pappalar

Was trying to figure out why this didn't work.

So iterm2 includes https://github.com/rcaloras/bash-preexec which breaks the trap 'command' DEBUG that is used in auto.sh

If you are using iterm I suggest using

unset RUBY_AUTO_VERSION

function chruby_auto() {
  local dir="$PWD/" version

  until [[ -z "$dir" ]]; do
    dir="${dir%/*}"

    if { read -r version <"$dir/.ruby-version"; } 2>/dev/null || [[ -n "$version" ]]; then
      if [[ "$version" == "$RUBY_AUTO_VERSION" ]]; then return
      else
        RUBY_AUTO_VERSION="$version"
        chruby "$version"
        return $?
      fi
    fi
  done

  if [[ -n "$RUBY_AUTO_VERSION" ]]; then
    chruby_reset
    unset RUBY_AUTO_VERSION
  fi
}

if [[ "$__bp_imported" == "defined" ]]; then
  precmd() { chruby_auto; }
elif [[ -n "$ZSH_VERSION" ]]; then
  if [[ ! "$preexec_functions" == *chruby_auto* ]]; then
    preexec_functions+=("chruby_auto")
  fi
elif [[ -n "$BASH_VERSION" ]]; then
  trap '[[ "$BASH_COMMAND" != "$PROMPT_COMMAND" ]] && chruby_auto' DEBUG
fi

ryanong avatar Mar 26 '18 20:03 ryanong

OK gotcha, so you are suggesting of triggering precmd() { chruby_auto; } in case the preexec is set, and reuse that one instead of the classing trap.

Good Catch!

Would you suggest this to be part of the official chruby or is it too of a edge case to be included in auto.sh

I have to say I did not find many iTerm integration useful, so I simply disabled them and the preexec

pappalar avatar Mar 27 '18 09:03 pappalar

I think this is a general issue of compatibility with bash-preexec, not specific to iTerm2 or macOS. How about detecting whether preexec_functions exists? This way we simply make use of Bash-Preexec when it’s available.

FranklinYu avatar May 25 '20 08:05 FranklinYu