Skip to content

Add completions for CLI - #1118

Closed
lewisacidic wants to merge 1 commit into
pre-commit:masterfrom
lewisacidic:feat/completions
Closed

lewisacidic wants to merge 1 commit into
pre-commit:masterfrom
lewisacidic:feat/completions

Conversation

@lewisacidic

Copy link
Copy Markdown

Uses argcomplete to trivially add basic completions to the cli.

This allows things like:

$ pre-commit <TAB>
--help     -V          gc                install         run            uninstall
--version  autoupdate  help              install-hooks   sample-config
-h         clean       init-templatedir  migrate-config  try-repo

And

$ pre-commit install --hook-type <TAB>
commit-msg  pre-commit  pre-push  prepare-commit-msg

Which can be tab toggled through once the correct completions are registered (at least in fish, I didn't try other shells yet).

This is pretty basic (as might be expected from adding 2 simple lines of code!!!), it would be really nice to do a bit more, such as getting a list of the available hooks and completing pre-commit run <TAB> with them.

Also, at least with click CLIs, you should be able to get help messages showing up (in fish at least) which is nicer than this. Of course, this would require a rewrite of the parser code to use click...

Any opinions about this sort of functionality?

Uses `argcomplete` to add basic completions to the cli.
@asottile

Copy link
Copy Markdown
Member

sorry, don't really want to add a dependency for this

@asottile

Copy link
Copy Markdown
Member

also the strategy used by argcomplete is too slow to be useful, it adds ~200ms per <tab> press which is well past the noticeable "slowness"

I believe the more correct way to do something like this would be to pregenerate completions as part of the packaging and provide those separately

@lewisacidic

Copy link
Copy Markdown
Author

Thanks for the quick reply.

In my opinion completions are pretty important for friendly, usable CLI tools. I understand pre-commit isn't first and foremost a CLI tool, but I found I missed completions when I was trying to remember how to install a commit-msg hook just now (typing -h is too much for me :))

I guess we could write completions by hand for the relatively small number of available commands and not need any new dependencies, but this is definitely the easiest and most integrated way to provide completions.

@asottile

Copy link
Copy Markdown
Member

it's not particularly difficult to generate completions scripts, here's some prior art: https://github.com/asottile/all-repos/blob/master/all_repos/complete.py

Would expose some command like:

eval "$(pre-commit complete)"

which you could put in your rc file

@lewisacidic

Copy link
Copy Markdown
Author

Just seen your followups, yep it's laggy (tbh I'm using WSL so I'm used to laggy terminals, didn't really notice...). I think the lag could be alleviated with a refactor to avoid importing before parsing CLIs, I've used this style of autocompletion before and it worked a charm.

In any case, I don't really have the time to invest into writing anything substantial right now, shall I close this and open an issue about completions instead?

@asottile

Copy link
Copy Markdown
Member

yeah I don't really want to play the import whackamole game -- there's not a good reason to runtime-compute completions when we can generate them all statically

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants