Skip to content

Commit

Permalink
feat(hok): add hok completions command to generate shell completion
Browse files Browse the repository at this point in the history
Signed-off-by: Chawye Hsu <[email protected]>
  • Loading branch information
chawyehsu committed Dec 9, 2024
1 parent 056ddfe commit da1b6d8
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 72 deletions.
123 changes: 52 additions & 71 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
22 changes: 22 additions & 0 deletions src/cmd/completions.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
3 changes: 3 additions & 0 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod bucket;
mod cache;
mod cat;
mod cleanup;
mod completions;
mod config;
mod hold;
mod home;
Expand Down Expand Up @@ -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),
Expand All @@ -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),
Expand Down

0 comments on commit da1b6d8

Please sign in to comment.