Skip to content

[WIP] shell completions#2411

Closed
miDeb wants to merge 101 commits intouutils:masterfrom
miDeb:completions
Closed

[WIP] shell completions#2411
miDeb wants to merge 101 commits intouutils:masterfrom
miDeb:completions

Conversation

@miDeb
Copy link
Contributor

@miDeb miDeb commented Jun 13, 2021

This is a wip pr to enable tab-completions in bash, fish and zsh for all utilities. For now, I implemented this for sort.
(For convenience this is based on #2397)

How to enable completions for a utility

  • Extract the clap::App creation into a different file called app.rs with a function get_app(app_name: &str) -> App
  • Add a build.rs to the utility with these contents:
include!("src/app.rs");
include!("../../build_completions.rs");
  • Add the dependencies needed for your app.rs file to Cargo.toml under [build-dependencies], e.g.
[build-dependencies]
clap = "2.33"

Having app.rs be a separate file reduces the amount of dependencies we have to add here, hopefully only clap is ever needed.

  • That's it! Now, when building, the files necessary for completions will be generated, and when running make install, those files will be copied to the appropriate directories.

@tertsdiepraam
Copy link
Collaborator

Amazing! Does this work for the busybox-style executable as well or only the standalone utils?

@miDeb
Copy link
Contributor Author

miDeb commented Jun 13, 2021

So far this only works for single utilities because afaik there is no single App for the multicall binary. We could combine the utilities as subcommands (clap subcommands can be Apps) inside of a big app. Then I think it could work

@tertsdiepraam
Copy link
Collaborator

Alright, thanks!

miDeb added 11 commits June 13, 2021 18:17
@miDeb
Copy link
Contributor Author

miDeb commented Jun 14, 2021

I changed a bit how it works: Instead of generating the completions in build.rs, a (hidden) subcommand is added to the coreutils binary, which generates the files (e.g. cargo run completion cp bash generates the completion file for cp on bash). This makes it a bit easier IMHO, and allows to create completions for the busybox binary as well (with cargo run completion coreutils <shell>. Utilities now must have a public app module with a get_app(app_name: &str) -> App function. From my previous try I already had separated those functions into their own files called app.rs. That's not necessary anymore, these functions could now be anywhere. I could as well change the convention and move them back to their original files if that's preferred.

Adds a (hidden) subcommand to the coreutils binary that generates
completions for a utility and shell to stdout.
use: coreutils completion <utility> <shell>

All utilities are now required to have a public `app` module with a
`get_app` function (the exact convention could still be changed,
there just needs to be a function that returns an `App` for each
utility in a place we can find).
@miDeb
Copy link
Contributor Author

miDeb commented Jun 15, 2021

I opened #2414 with the final (and hopefully simpler) version of this.

@miDeb miDeb closed this Jun 15, 2021
@miDeb miDeb deleted the completions branch June 19, 2021 17:11
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