postmodern / chruby

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

zsh: switch rubies after changing pwd, not before #301

Closed nathanl closed 9 years ago

nathanl commented 9 years ago

I suspect this PR will be rejected as "you misunderstand" or will need improving. Here's the problem I'm trying to solve: auto.sh hooks preexec to try to set the correct ruby before running a command. If that command is something like cd, running before the command means it finds .ruby-version from the folder I'm leaving, not from the folder I'm entering.

Setup

If we also use the chpwd hook, we'll switch rubies a second time when we arrive in the new directory. It's not a perfect fix, but at least the end result is correct.

Imperfections:

The README describes auto-switching as being useful for "when you cd between your different projects". A chpwd hook handles exactly that scenario, and the preexec hook isn't great for it, because it creates the problem I've described here.

But preexec is nice for when you open a new terminal and run a command; it ensures you're on the right Ruby first.

Is that the only scenario we need it for? If so, wouldn't it be more straightforward to find a way to run chruby_auto when a new terminal is spawned? Eg, maybe we could use a precmd hook that removes itself in that shell after running chruby_auto, and subsequently leaves it to the chpwd hook?

What do you think?

postmodern commented 9 years ago

Any changes to auto-switching should be mirrored for bash as well as zsh. Also behavior should remain consistent for non-interactive as well as interactive sessions.

nathanl commented 9 years ago

@postmodern After some research, I don't see a way to mimic zsh's chpwd_functions in bash.

I understand that you want zsh and bash to work the same way out of the box, but zsh supports a usage that is (in my opinion) much nicer.

What would you think about a change like this?

nathanl commented 9 years ago

Ah, geez. Apparently "source another script in the same folder as this one" is harder than it sounds. I give up. I'm going to copy and paste auto.sh into my dotfiles and modify it as I see fit.

postmodern commented 9 years ago

Yep, I think our hands are tied by the shell. We have to maintain behavior between Bash and Zsh. OSX still ships Bash 3, so we can't even take advantage of Bash 4 features. We tried using PROMPT_COMMAND in the past, but injecting into it was error prone.