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

Autocomplete after the eol command #216

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

romainx
Copy link

@romainx romainx commented Jul 26, 2024

Hello,

This is a draft to resolve #125.
As the project is using argparse, I thought it was a good idea to use argcomplete.
This implementation is very simple but it seems to do the trick.

# register eol with the shell's completion framework by running register-python-argcomplete:
eval "$(register-python-argcomplete eol)"
# use it
eol ne
nextcloud  nextjs  netbsd  nexus  neo4j  neos  --    

Cheers 🍰

@hugovk hugovk added the changelog: Added For new features label Jul 26, 2024
@romainx romainx marked this pull request as ready for review July 26, 2024 07:42
@hugovk
Copy link
Owner

hugovk commented Jul 26, 2024

Thanks for this!

# register eol with the shell's completion framework by running register-python-argcomplete:
eval "$(register-python-argcomplete my-python-app)"
# use it
eol ne
nextcloud  nextjs  netbsd  nexus  neo4j  neos  --    

Is my-python-app correct here?

@romainx
Copy link
Author

romainx commented Jul 26, 2024

Hello

Is my-python-app correct here?

Sorry no 😓 , it should be replaced by eol. Fixed in the PR description.

@hugovk
Copy link
Owner

hugovk commented Jul 26, 2024

Thanks, so does the eval need to be run just once, or once per new shell or something?

@romainx
Copy link
Author

romainx commented Jul 26, 2024

@hugovk the use of eval is for one time activation (for the current shell), there are several options that are nicely described in the pytest documentation for bash completion.
I pushed a commit to support global completion.

@hugovk
Copy link
Owner

hugovk commented Jul 26, 2024

The eval works for me, but I've run activate-global-python-argcomplete (on macOS with iterm2):

activate-global-python-argcomplete
Defaulting to system-wide installation.
Installing /Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/argcomplete/bash_completion.d/_python-argcomplete to /opt/homebrew/share/zsh/site-functions/_python-argcomplete...
Installed.
Installing /Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/argcomplete/bash_completion.d/_python-argcomplete to /opt/homebrew/etc/bash_completion.d/python-argcomplete...
Installed.
Please restart your shell or source the installed file to activate it.

And opened a new shell, but it's not completing:

eol ne

Do you know why?

@romainx
Copy link
Author

romainx commented Jul 26, 2024

Works on my side on new shell but only after I added the flag # PYTHON_ARGCOMPLETE_OK in the code (cli.py). Have you pulled this commit to perform your test?

Instead, the shell will look for the string PYTHON_ARGCOMPLETE_OK in the first 1024 bytes of any executable that it's running completion for, and if it's found, follow the rest of the argcomplete protocol as described above.
--source

@hugovk
Copy link
Owner

hugovk commented Jul 27, 2024

Yes:

head --lines 2 src/norwegianblue/cli.py
#!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OKpip install -e . --quieteol ne<tab>

New terminal tab:

eol ne<tab>

@romainx
Copy link
Author

romainx commented Jul 27, 2024

Sorry for that. The script python-argcomplete is located on my side at:

  • /usr/local/share/zsh/site-functions/_python-argcomplete for zsh
  • /usr/local/etc/bash_completion.d/python-argcomplete for bash

Seems that on your side it's installed at the following locations:

  • /opt/homebrew/share/zsh/site-functions/_python-argcomplete for zsh
  • /opt/homebrew/etc/bash_completion.d/python-argcomplete for bash

Could you please try to source the one corresponding to your shell to check if it exists and if it solves the problem?

source /opt/homebrew/share/zsh/site-functions/_python-argcomplete

@hugovk
Copy link
Owner

hugovk commented Jul 27, 2024

Sorry for that. The script python-argcomplete is located on my side at:

  • /usr/local/share/zsh/site-functions/_python-argcomplete for zsh
  • /usr/local/etc/bash_completion.d/python-argcomplete for bash

Seems that on your side it's installed at the following locations:

  • /opt/homebrew/share/zsh/site-functions/_python-argcomplete for zsh
  • /opt/homebrew/etc/bash_completion.d/python-argcomplete for bash

That's right.

Could you please try to source the one corresponding to your shell to check if it exists and if it solves the problem?

source /opt/homebrew/share/zsh/site-functions/_python-argcomplete

Yes, that works.

@romainx
Copy link
Author

romainx commented Jul 27, 2024

I don't know why it is not sourced automatically by your shell 🤔?

@hugovk
Copy link
Owner

hugovk commented Jul 28, 2024

I've no idea...

python3.13 is the default interpreter on this machine, I also have:

locate _python-argcomplete
/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/argcomplete/bash_completion.d/_python-argcomplete
/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/argcomplete/bash_completion.d/_python-argcomplete
/Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/argcomplete/bash_completion.d/_python-argcomplete
/opt/homebrew/Cellar/pipx/1.6.0/libexec/lib/python3.12/site-packages/argcomplete/bash_completion.d/_python-argcomplete
/opt/homebrew/share/zsh/site-functions/_python-argcomplete

activate-global-python-argcomplete on my path is for 3.13:

which activate-global-python-argcomplete
/Library/Frameworks/Python.framework/Versions/3.13/bin/activate-global-python-argcomplete

But when I run it, it installs from 3.13 to a Homebrew path:

activate-global-python-argcomplete
Defaulting to system-wide installation.
Installing /Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/argcomplete/bash_completion.d/_python-argcomplete to /opt/homebrew/share/zsh/site-functions/_python-argcomplete...
Installed.
Installing /Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages/argcomplete/bash_completion.d/_python-argcomplete to /opt/homebrew/etc/bash_completion.d/python-argcomplete...
Installed.
Please restart your shell or source the installed file to activate it.

I don't use Homebrew Python for anything other than letting Homebrew manage it for its own use, so it feels like a bug that the 3.13 activate-global-python-argcomplete installs completion for another interpreter, let alone the Homebrew one.

Anyway, it also works if I source that 3.13 one in a new tab. What do you suggest? Should I add manual sourcing of that to my ~/.zshrc or something?

@romainx
Copy link
Author

romainx commented Jul 29, 2024

zsh

From this helper.

Completion functions for commands are stored in files with names beginning with an underscore _, and these files should be placed in a directory listed in the $fpath variable. You can add a directory to $fpath by adding a line like this to your ~/.zshrc file:

So you can check if your fpath variable contains the location of the autocomplete script installation.

echo $fpath | grep /opt/homebrew/share/zsh/site-functions  

If it's not the case you could add a line in your ~/.zshrc file.

fpath=(/opt/homebrew/share/zsh/site-functions $fpath)

bash

Based on my understanding of this answer.
A .bash_completion script should perform this kind of source.

for bcfile in ~/.bash_completion.d/* ; do
  [ -f "$bcfile" ] && . $bcfile
done

@hugovk
Copy link
Owner

hugovk commented Aug 5, 2024

So you can check if your fpath variable contains the location of the autocomplete script installation.

echo $fpath | grep /opt/homebrew/share/zsh/site-functions  

Yes, it's the first (and second) entry in my $fpath.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: Added For new features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Autocomplete after the eol command
2 participants