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

Added TAB Completion in Command Line Tool Using shtab #118

Merged
merged 1 commit into from
Aug 28, 2022
Merged

Added TAB Completion in Command Line Tool Using shtab #118

merged 1 commit into from
Aug 28, 2022

Conversation

Freed-Wu
Copy link
Contributor

However, import a new independence. If you don't mind, merge this PR and close
another PR.

@piccolomo
Copy link
Owner

I will test this later and report... thanks a lot for the contribution!!! Highly appreciated.

@Freed-Wu
Copy link
Contributor Author

😄 You can change shtab to an optional depend, such as pip install plotext[completion], that is when shtab is installed, plotext supports the generation of shell completions.

@piccolomo
Copy link
Owner

Hi @Freed-Wu,

I am in the process of upgrading plotext and integrating your changes. I have tried the packed as you have changed it, but when i press TAB after plotext command on any sub-command nothing happens. I have installed shtab.

Does it work for you? Any help?

@piccolomo
Copy link
Owner

To be precise, when I press TAB (eg after plotext or after plotex scatter) a series of options not connected to plotext appear, like directories and files.

@Freed-Wu
Copy link
Contributor Author

Freed-Wu commented Sep 1, 2022

Which shell do you use? Take zsh as an example:

plotext --print-completion zsh| sudo tee /usr/share/zsh/site-functions/_plotext
# maybe
plotext --print-completion zsh| sudo tee /usr/local/share/zsh/site-functions/_plotext

Important

Then compinit to generate ~/.zompdump

❯ grep _plotext ~/.zcompdump
'plotext' '_plotext'

That means plotext has a completion named _plotext.

If don't have _plotext, you can rm ~/.zompdump then retry compinit

Then it should work.

❯ plotext <TAB>
plotext commands
bar       builds a bar plot
gif       plays a gif image from path
hist      builds a histogram plot
image     plots an image from path
plot      plots lines between consecutive data points
plotter   plots a series of data points and the lines between consecutive ones
scatter   plots a series of data points
video     plays a video from path
youtube   plays a youtube video from url

@piccolomo
Copy link
Owner

piccolomo commented Sep 1, 2022

Hi thanks for the message, I find it hard to understand it as I am fresh and new to completions.

I am using bash. And cannot find the _plotext file. I have tried around here:

plotext --print-completion bash | sudo tee /usr/share/bash-completion/completions/

Also the command compinit does not compute in my system. Would this be something that the user would have to do? in which case it could be tricky on them.

@piccolomo
Copy link
Owner

Also here is the output for just plotext --print-completion bash
image
it's quite long and it goes for quite awhile after.

@Freed-Wu
Copy link
Contributor Author

Freed-Wu commented Sep 1, 2022

For bash:

plotext --print-completion bash|sudo tee /usr/share/bash-completion/completions/plotext

I test it can work (although don't have too many functions as zsh)

Screenshot from 2022-09-01 23-43-56

@Freed-Wu
Copy link
Contributor Author

Freed-Wu commented Sep 1, 2022

Then compinit to generate ~/.zompdump

compinit is a zsh command, zsh will generate a cache to help fasten the completion. if the cache has wrong information, it cannot work. bash does not use any cache.

@piccolomo
Copy link
Owner

I run the following
image
with no result in terms of tab completion

@Freed-Wu
Copy link
Contributor Author

Freed-Wu commented Sep 1, 2022

Can you cat /usr/share/bash-completion/completions/plotext?

@piccolomo
Copy link
Owner

yes
image

@Freed-Wu
Copy link
Contributor Author

Freed-Wu commented Sep 1, 2022

Have you installed bash-completion? Can you try other program's completion can work? Such as x264 --<TAB>
Screenshot from 2022-09-01 23-57-37

@piccolomo
Copy link
Owner

piccolomo commented Sep 1, 2022

I have just now installed bash-completion and then x264, but when I press x264 -- nothing happens... no options displayed.

@piccolomo
Copy link
Owner

piccolomo commented Sep 1, 2022

Ok I have tried a new terminal called Konsole and I have completion but the only marker it completes is sd: it does not display a list of possibilities
image

@Freed-Wu
Copy link
Contributor Author

Freed-Wu commented Sep 1, 2022

I guess your bashrc don't load bash-completion. Can you add [ -r /usr/share/bash-completion/bash_completion ] && . /usr/share/bash-completion/bash_completion to your bashrc and retry?

@piccolomo
Copy link
Owner

Ok now it works also in my normal ubuntu terminal but it has the same limitations as Konsole... completion yes but no list of possibilities

@Freed-Wu
Copy link
Contributor Author

Freed-Wu commented Sep 1, 2022

I have completion but the only marker it completes is sd: it does not display a list of possibilities

See iterative/shtab#86, it is a bug imported from iterative/shtab#81

only expands the first value of the _choices array.

@piccolomo
Copy link
Owner

Mmm, ok thanks then. It seems we could only wait and hope the issue gets solved. Thanks btw

@Freed-Wu
Copy link
Contributor Author

Freed-Wu commented Sep 1, 2022

You are welcome! 😄

@piccolomo
Copy link
Owner

Just to add a final note: markers and colors are completed only in their first element and the code works the same as when i remove the .complete part... so I take it away for now and comment the function dict_to_complete

@piccolomo piccolomo changed the title Add bash/zsh/tcsh completion by shtab Added TAB Completion in Command Line Tool Using shtab Sep 2, 2022
@piccolomo
Copy link
Owner

Hi @Freed-Wu , I finally updated plotext including the changes you asked for.

The new version is available on GitHib for now and soon also on PyPi. To install follow the indications here.

Your changes are documented here and you have been credited here.

Any feedback is welcomed.

Thanks a lot for your inputs and all the best,
Savino

@piccolomo
Copy link
Owner

Sorry @Freed-Wu , as I have been warned, I had to removed the shtab dependency because it cannot be made optional, since it is called once plotext.cli.py is loaded.

If you have any other idea, very welcomed. Thanks

@Freed-Wu
Copy link
Contributor Author

Freed-Wu commented Sep 3, 2022

because it cannot be made optional, since it is called once plotext.cli.py is loaded.

I think

try:
    import shtab
except ImportError:
    from . import _shtab as shtab

Then realize a internal module named _shtab.py which just provide a same API
with shtab

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants