From da1b6d8f409d8c7894872dab84e28cb8d1814fab Mon Sep 17 00:00:00 2001 From: Chawye Hsu Date: Mon, 9 Dec 2024 19:50:23 +0800 Subject: [PATCH] feat(hok): add `hok completions` command to generate shell completion Signed-off-by: Chawye Hsu --- Cargo.lock | 123 +++++++++++++++++------------------------ Cargo.toml | 3 +- src/cmd/completions.rs | 22 ++++++++ src/cmd/mod.rs | 3 + 4 files changed, 79 insertions(+), 72 deletions(-) create mode 100644 src/cmd/completions.rs diff --git a/Cargo.lock b/Cargo.lock index e26500c..2dd49cf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -34,24 +34,24 @@ checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" [[package]] name = "anstream" -version = "0.3.2" +version = "0.6.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ca84f3628370c59db74ee214b3263d58f9aadd9b4fe7e711fd87dc452b7f163" +checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" dependencies = [ "anstyle", "anstyle-parse", "anstyle-query", "anstyle-wincon", "colorchoice", - "is-terminal", + "is_terminal_polyfill", "utf8parse", ] [[package]] name = "anstyle" -version = "1.0.1" +version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3a30da5c5f2d5e72842e00bcb57657162cdabef0931f40e2deb9b4140440cecd" +checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" [[package]] name = "anstyle-parse" @@ -73,12 +73,12 @@ dependencies = [ [[package]] name = "anstyle-wincon" -version = "1.0.1" +version = "3.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "180abfa45703aebe0093f79badacc01b8fd4ea2e35118747e5811127f926e188" +checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125" dependencies = [ "anstyle", - "windows-sys 0.48.0", + "windows-sys 0.59.0", ] [[package]] @@ -209,34 +209,41 @@ dependencies = [ [[package]] name = "clap" -version = "4.3.21" +version = "4.5.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c27cdf28c0f604ba3f512b0c9a409f8de8513e4816705deb0498b627e7c3a3fd" +checksum = "3135e7ec2ef7b10c6ed8950f0f792ed96ee093fa088608f1c76e569722700c84" dependencies = [ "clap_builder", "clap_derive", - "once_cell", ] [[package]] name = "clap_builder" -version = "4.3.21" +version = "4.5.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08a9f1ab5e9f01a9b81f202e8562eb9a10de70abf9eaeac1be465c28b75aa4aa" +checksum = "30582fc632330df2bd26877bde0c1f4470d57c582bbc070376afcd04d8cb4838" dependencies = [ "anstream", "anstyle", - "clap_lex 0.5.0", - "once_cell", + "clap_lex 0.7.4", "strsim", "terminal_size", ] +[[package]] +name = "clap_complete" +version = "4.5.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9647a559c112175f17cf724dc72d3645680a883c58481332779192b0d8e7a01" +dependencies = [ + "clap 4.5.23", +] + [[package]] name = "clap_derive" -version = "4.3.12" +version = "4.5.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "54a9bb5758fc5dfe728d1019941681eccaf0cf8a4189b692a0ee2f2ecf90a050" +checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab" dependencies = [ "heck", "proc-macro2", @@ -255,9 +262,9 @@ dependencies = [ [[package]] name = "clap_lex" -version = "0.5.0" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2da6da31387c7e4ef160ffab6d5e7f00c42626fe39aea70a7b0f1773f7dd6c1b" +checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" [[package]] name = "colorchoice" @@ -382,7 +389,7 @@ dependencies = [ "crossterm_winapi", "mio", "parking_lot", - "rustix 0.38.37", + "rustix", "signal-hook", "signal-hook-mio", "winapi", @@ -682,9 +689,9 @@ checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" [[package]] name = "heck" -version = "0.4.1" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" [[package]] name = "hermit-abi" @@ -706,7 +713,8 @@ name = "hok" version = "0.1.0-beta.7" dependencies = [ "anyhow", - "clap 4.3.21", + "clap 4.5.23", + "clap_complete", "crossterm", "env_logger", "indicatif", @@ -787,26 +795,10 @@ dependencies = [ ] [[package]] -name = "io-lifetimes" -version = "1.0.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2" -dependencies = [ - "hermit-abi 0.3.9", - "libc", - "windows-sys 0.48.0", -] - -[[package]] -name = "is-terminal" -version = "0.4.9" +name = "is_terminal_polyfill" +version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" -dependencies = [ - "hermit-abi 0.3.9", - "rustix 0.38.37", - "windows-sys 0.48.0", -] +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" [[package]] name = "itertools" @@ -928,12 +920,6 @@ dependencies = [ "vcpkg", ] -[[package]] -name = "linux-raw-sys" -version = "0.3.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519" - [[package]] name = "linux-raw-sys" version = "0.4.14" @@ -1217,9 +1203,9 @@ checksum = "f32154ba0af3a075eefa1eda8bb414ee928f62303a54ea85b8d6638ff1a6ee9e" [[package]] name = "proc-macro2" -version = "1.0.66" +version = "1.0.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" +checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" dependencies = [ "unicode-ident", ] @@ -1326,20 +1312,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "rustix" -version = "0.37.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d69718bf81c6127a49dc64e44a742e8bb9213c0ff8869a22c308f84c1d4ab06" -dependencies = [ - "bitflags 1.3.2", - "errno", - "io-lifetimes", - "libc", - "linux-raw-sys 0.3.8", - "windows-sys 0.48.0", -] - [[package]] name = "rustix" version = "0.38.37" @@ -1349,7 +1321,7 @@ dependencies = [ "bitflags 2.6.0", "errno", "libc", - "linux-raw-sys 0.4.14", + "linux-raw-sys", "windows-sys 0.52.0", ] @@ -1512,9 +1484,9 @@ dependencies = [ [[package]] name = "strsim" -version = "0.10.0" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "syn" @@ -1553,12 +1525,12 @@ dependencies = [ [[package]] name = "terminal_size" -version = "0.2.6" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e6bf6f19e9f8ed8d4048dc22981458ebcf406d67e94cd422e5ecd73d63b3237" +checksum = "5352447f921fda68cf61b4101566c0bdb5104eff6804d0678e5227580ab6a4e9" dependencies = [ - "rustix 0.37.23", - "windows-sys 0.48.0", + "rustix", + "windows-sys 0.59.0", ] [[package]] @@ -1838,6 +1810,15 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets 0.52.6", +] + [[package]] name = "windows-targets" version = "0.42.2" diff --git a/Cargo.toml b/Cargo.toml index 1cf8e9c..de5ff47 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,8 @@ path = "src/lib.rs" [dependencies] anyhow = "1.0" -clap = { version = "4.3", features = ["wrap_help", "cargo", "derive"] } +clap = { version = "4.5", features = ["wrap_help", "cargo", "derive"] } +clap_complete = "4.5.38" crossterm = "0.28" env_logger = "0.8.3" indicatif = "0.17.5" diff --git a/src/cmd/completions.rs b/src/cmd/completions.rs new file mode 100644 index 0000000..a20bb85 --- /dev/null +++ b/src/cmd/completions.rs @@ -0,0 +1,22 @@ +use clap::{crate_name, CommandFactory, Parser}; +use clap_complete::Shell; + +use crate::{cmd::Cli, Result}; + +/// Generate shell completions +#[derive(Parser, Debug)] +#[clap(arg_required_else_help = true)] +pub struct Args { + /// The shell type + shell: Shell, +} + +pub fn execute(args: Args) -> Result<()> { + let mut buf = vec![]; + clap_complete::generate(args.shell, &mut Cli::command(), crate_name!(), &mut buf); + println!( + "{}", + String::from_utf8(buf).expect("clap_complete did not generate valid shell script") + ); + Ok(()) +} diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index d28077b..ced9760 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -4,6 +4,7 @@ mod bucket; mod cache; mod cat; mod cleanup; +mod completions; mod config; mod hold; mod home; @@ -43,6 +44,7 @@ pub enum Command { Cache(cache::Args), Cat(cat::Args), Cleanup(cleanup::Args), + Completions(completions::Args), Config(config::Args), Hold(hold::Args), Home(home::Args), @@ -69,6 +71,7 @@ pub fn start(session: &Session) -> Result<()> { Command::Cache(args) => cache::execute(args, session), Command::Cat(args) => cat::execute(args, session), Command::Cleanup(args) => cleanup::execute(args, session), + Command::Completions(args) => completions::execute(args), Command::Config(args) => config::execute(args, session), Command::Hold(args) => hold::execute(args, session), Command::Home(args) => home::execute(args, session),