Supplement
Shell-agnostic, extensible CLI completion for Rust 💊
supplement is a Rust library that generates completion scaffold as Rust code.
Give it a clap object, and instead of spitting out shell files that you later have to manually edit, it spits out Rust! supplement is:
- Shell-agnostic
- Powerful - Some features are not widely supported in every shell, and
supplementcomes to rescue - Stop modifying generated files - Instead, extend it with Rust's enum system
- Easy to test - Functions and objects in a modern programming language, instead of some shell script black sorcery.
- It's Rust 🦀
Install
Add one line in Cargo.toml. By default, it uses clap 4, but you can make it use clap 3 with feature.
[]
= "0.1"
# Or, to use clap 3
= { = "0.1", = false, = ["clap-3"] }
# Or, disable the code generate feature completely
= { = "0.1", = false }
Get started
- Have a
clapdefinition - Generate the
supplementdefinition (preferably inbuild.rs) - Import and use the generated code
- Compile the binary
- Put a simple shell script in place to tell the shell how to use your binary
Let's break it down step by step. Say you have this awesome clap definition, and want to use supplement to make it even more awsome.
use ;
You can now edit the build.rs to generate the definition file:
use CommandFactory;
use generate;
And use it in main.rs:
use *;
Note that, if you missed some implementation, it's a compile time error. So just relex and let Rust get your back 💪
And after implementing everything, compile it to binary file and create a shell completion file to tell the shell how to use the binary. For example, in fish shell you should have:
# Put this to /usr/share/fish/completions/git.fish or ~/.config/fish/completions/git.fish
function __do_completion
set cmd (commandline -j)
set cmd_arr (string split ' ' $cmd)
if [ -z "$cmd_arr[-1]" ]
# preserve the last white space
echo fish $cmd "''" | xargs path/to/your/binary
else
echo fish $cmd | xargs path/to/your/binary
end
end
complete -k -c git -x -a "(__do_completion)"
The scripts for other shells can be found in supplement-example/shell
A complete example can be found in supplement-example