postmodern / chruby

Changes the current Ruby
MIT License
2.85k stars 190 forks source link

chruby does not work with iTerm2 Integration #399

Open pappalar opened 6 years ago

pappalar commented 6 years ago

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

ryanong commented 6 years ago

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
pappalar commented 6 years ago

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

FranklinYu commented 4 years ago

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.