Closed nathanl closed 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.
@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?
auto.sh
, extract auto_function.sh
. Then auto.sh
would source ./auto_function.sh
before setting up its hooks and would continue to work the same way.source auto_function.sh
and either 1) run it manually as desired or 2) set up alternate hooks, like chpwd_functions
in zsh.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.
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.
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
hookspreexec
to try to set the correct ruby before running a command. If that command is something likecd
, running before the command means it finds.ruby-version
from the folder I'm leaving, not from the folder I'm entering.Setup
mkdir -p switching_test/sub
echo 'ruby-top' > switching_test/.ruby-version
echo 'ruby-sub' > switching_test/sub/.ruby-version
Tests
cd switching_test
chruby: unknown Ruby: ruby-top
cd sub
chruby: unknown Ruby: ruby-sub
chruby: unknown Ruby: ruby-top
This fix
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:
chruby: unknown Ruby
, even though we immediately afterwards switch to the right version.Misgivings
pushd
andpopd
.test/runner
gives me a bunch of errors with backtraces like/usr/share/shunit2/shunit2:.:129: no such file or directory: /usr/share/shunit2/shunit2 \n test/chruby_auto_test.sh:zsh:130: use 'exit' to exit.
I installedchruby
with homebrew and it appears to be looking in the installation folder forshunit2
, but I don't know why.Alternate idea
The README describes auto-switching as being useful for "when you
cd
between your different projects". Achpwd
hook handles exactly that scenario, and thepreexec
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 aprecmd
hook that removes itself in that shell after runningchruby_auto
, and subsequently leaves it to thechpwd
hook?What do you think?