Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Install bash programmble completions for bash/zsh #753

Merged
merged 1 commit into from
Nov 18, 2016
Merged

Install bash programmble completions for bash/zsh #753

merged 1 commit into from
Nov 18, 2016

Conversation

PeterDaveHello
Copy link
Collaborator

No description provided.

@@ -177,6 +177,23 @@ nvm_check_global_modules() {
fi
}

nvm_bash_completion() {
if [ -f "$HOME/.bashrc" ] || [ -f "$HOME/.bash_profile" ] || [ -f "$HOME/.zshrc" ] ; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would be more efficient to do everything in the for loop below, including downloading the bash_completion file on the first iteration?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to check the file for multiple times, do we?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true - in your current code, you're doing up to 6 checks - 3 files twice, plus 1 bash_completion. With what I'm proposing, you'd do up to 6 as well - 3 files, plus 3 bash_completion. So I suppose it'd work out to be the same. However, you could use a local variable and set it to true when the file downloads, and then you'd only ever do up to 4 checks.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, updated, though the checking here won't have any performance issue I think.

@PeterDaveHello
Copy link
Collaborator Author

Updated.

@@ -177,6 +177,26 @@ nvm_check_global_modules() {
fi
}

nvm_bash_completion() {
NVM_PROFILE=$(nvm_detect_profile)
case "$NVM_PROFILE" in \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My mistake - the \ isn't needed here, but it is needed after each of the pipes below

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same same

@PeterDaveHello
Copy link
Collaborator Author

@ljharb I guess this is a out-dated PR, and already been implemented, right?

@ljharb
Copy link
Member

ljharb commented Dec 5, 2015

It definitely hasn't been implemented - please reopen it and rebase if you're still interested in landing it :-)

@PeterDaveHello PeterDaveHello reopened this Dec 5, 2015
@PeterDaveHello
Copy link
Collaborator Author

Oh okay, there are many conflicts, need more time :)

@PeterDaveHello
Copy link
Collaborator Author

@ljharb I rewrite it, is it good for you?

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this unconditionally try to source the completion file in shells that can't handle it?

This needs to be explicitly limited to bash only (and possibly zsh).

@PeterDaveHello
Copy link
Collaborator Author

I just added zsh and bash detection now :)

@@ -298,6 +298,7 @@ nvm_do_install() {
INSTALL_DIR="$(nvm_install_dir)"

SOURCE_STR="\nexport NVM_DIR=\"$INSTALL_DIR\"\n[ -s \"\$NVM_DIR/nvm.sh\" ] && \\. \"\$NVM_DIR/nvm.sh\" # This loads nvm\n"
SOURCE_STR2="[ -s \"\$NVM_DIR/bash_completion\" ] && [ -n \"\$BASH_VERSION\" ] && [ -n \"\$ZSH_VERSION\" ] && \\. \"\$NVM_DIR/bash_completion\" # This loads nvm bash_completion\n"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding an extra line to everyone's profiles that runtime-checks their shell seems subpar - can we not determine this prior to insertion?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be, is detection with nvm_detect_profile and .bash/.zsh prefix good here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just updated it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think with your changes below, we no longer need the runtime shell checks here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean nvm_detect_profile?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If bash or zsh, then we have string2, is that correct? 😄 Thanks,

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically yes, i think we're on the same page

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I assign STR2 within the switch/case, the part we can not recognize profile wouldn't be able to print that string, what about to clear the string in switch/case for non-bash/zsh shell?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can assign to STR2 unconditionally. The parts I want to only happen conditionally are a) add the line to the profile file, and b) print out the line on stdout/stderr.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated!

@@ -302,13 +303,26 @@ nvm_do_install() {
echo " OR"
echo "=> Append the following lines to the correct file yourself:"
command printf "${SOURCE_STR}"
echo "=> Please also append the following lines to the if you are using bash/zsh shell:"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd like this line to only be output when the user is using them - in other words, in the same circumstances where "Appending bash_completion source string" happens.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ljharb I tried again and hope this would be correct now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just realize that I can't do it since this message is for the users we can't detect their shell profile, if so, how could we know they are using bash or zsh I'll just drop this part now.

@@ -323,6 +337,9 @@ nvm_do_install() {

echo "=> Close and reopen your terminal to start using nvm or run the following to use it now:"
command printf "$SOURCE_STR"
if [ -n \"\$BASH_VERSION\" ] && [ -n \"\$ZSH_VERSION\" ]; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this condition should match that on line 316

@PeterDaveHello
Copy link
Collaborator Author

Updated!

@ljharb
Copy link
Member

ljharb commented Nov 18, 2016

@PeterDaveHello would you mind checking the box for "allow maintainers to edit"?

@PeterDaveHello
Copy link
Collaborator Author

@ljharb done!

@ljharb ljharb merged commit 1ffa418 into nvm-sh:master Nov 18, 2016
@ljharb ljharb added the installing nvm Problems installing nvm itself label Nov 18, 2016
@PeterDaveHello PeterDaveHello deleted the bash_completion branch November 18, 2016 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
installing nvm Problems installing nvm itself
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants