8000 feat: add the ability to generate shell completions by fgimian · Pull Request #11 · bircni/git-statuses · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add the ability to generate shell completions #11

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

Merged
merged 1 commit into from
Jul 10, 2025

Conversation

fgimian
Copy link
Contributor
@fgimian fgimian commented Jul 8, 2025

Hey again,

This PR adds the ability to generate shell completions for common shells. If you also are interested in Nushell completions, an additional crate and some extra code is required. Please let me know. 😄

I've ensured the tests pass this time too, sorry about last time.

Cheers
Fotis

@fgimian fgimian force-pushed the feat/completions branch from 660329a to aefcdbc Compare July 8, 2025 01:51
@bircni
Copy link
Owner
bircni commented Jul 8, 2025

Can't we do this somehow dynamically?

@fgimian
Copy link
Contributor Author
fgimian commented Jul 8, 2025

Do you mean at build time? The advantage of the approach I've used is that it's easy to generate completions regardless of how the tool is installed.

Most other Rust CLI tools seem to use a similar strategy based on my own experience.

Feel free to let me know what you have in mind.

Cheers
Fotis

@bircni
Copy link
Owner
bircni commented Jul 8, 2025

I thought about initializing them on first usage or something like that

@fgimian
Copy link
Contributor Author
fgimian commented Jul 8, 2025

I thought about initializing them on first usage or something like that

Ah gotcha. Tomorrow when I'm back on my PC, I'll respond in more detail with examples of what other popular projects do so we can figure out next steps.

The vast majority now offer a way to generate completions for your chosen shell using the tool itself. Some generate them at build time and package them, which I personally don't prefer as they don't agree with tools like cargo binstall.

More to follow tomorrow 😊

Cheers
Fotis

@fgimian
Copy link
Contributor Author
fgimian commented Jul 9, 2025

Okies, so here's a view of how some popular Rust tools handle completion generation.

At Build Time

hyperfine uses build.rs and generates completions at compile time in the directory specified in the environment variable SHELL_COMPLETIONS_DIR or OUT_DIR if that's not specified.

bottom uses build.rs and generates completions at compile time in the directory specified in environment variable COMPLETION_DIR or a default location. I implemented this ability which was recently merged as there was no way to specify a custom location previously.

dust uses build.rs and generates completes at compile time and places them in a completions sub-directory near the source in the local Cargo registry cache.

At Runtime

cargo (nightly) generates completions when the CARGO_COMPLETE environment variable is set to the desired shell (e.g. "powershell").

And here are the commands for generating PowerShell completions at runtime for various other popular Rust CLI tools:

bat --completion ps1
dprint completions powershell
fd --gen-completions powershell
procs --gen-completion-out powershell
rg --generate complete-powershell
rustup completions powershell
starship completions power-shell

# Implemented by me and merged by the project authors. 😄 
dua completions powershell
xh --generate complete-powershell

I think it's worth talking about pros and cons of both approaches.

Pros at Runtime:

  • Only requires the binary to generate completions, which makes it easier to distribute your application. Popular install tools like cargo-binstall also work well with this approach.
  • It is always easy to be certain you have the corresponding completions for the version of the binary you are using.

Pros at Build Time:

  • Possibly a slightly smaller binary due to the exclusion of the code required to generate completions.

Personally, I feel that generating completions at runtime is the vastly superior choice and is fool-proof and more reliable.

Kindly let me know your thoughts 😄
Fotis

@bircni
Copy link
Owner
bircni commented Jul 9, 2025

yeah you are right at runtime is I think the best option
there is no option to generate them at first run?

@fgimian
Copy link
Contributor Author
fgimian commented Jul 10, 2025

yeah you are right at runtime is I think the best option there is no option to generate them at first run?

I must admit that I'm still not quite sure how you see that working. Do you mean that upon first running the tool, it'll detect your shell and automatically place completions in the relevant directory for you? If so, this is definitely a big no-no for the following reasons:

  • Detecting the user's shell would be challenging
  • How would one know if a user has multiple shells and wishes to deploy multiple sets of completions
  • Global complet 8000 ions on Linux systems are placed in a location only writable by root
  • Local user completions may reside anywhere depending on how the user has configured their shell profile
  • Writing a file without a user's consent is not great from a security standpoint

Based on my own experience with both Rust projects and those in other languages, completions are made available only using the methods described above. Either they are embedded into the binary and can be displayed to stdout (most common) or written to a chosen path (much less common), or they are provided during build time or distribution of the application as files.

Kindly let me know how you wish to proceed 😄

Cheers
Fotis

@bircni
Copy link
Owner
bircni commented Jul 10, 2025

I was thinking about the same problems but was unsure if there is still a correct solution
I will merge this PR now as I personally am happy with how it works then now :-)

@bircni bircni merged commit 46fc9da into bircni:main Jul 10, 2025
5 checks passed
@fgimian
Copy link
Contributor Author
fgimian commented Jul 10, 2025

I was thinking about the same problems but was unsure if there is still a correct solution

I will merge this PR now as I personally am happy with how it works then now :-)

Thank you so much! 😊

@fgimian fgimian deleted the feat/completions branch July 11, 2025 00:53
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
0