Skip to content

Commit

Permalink
add command for generating autocompletion script
Browse files Browse the repository at this point in the history
  • Loading branch information
Zolwiastyl committed Feb 5, 2024
1 parent 2069117 commit 2aaa6a5
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ clap = { version = "4.2.4", features = ["derive", "cargo", "color"] }
[dependencies]
anyhow = "1.0.70"
clap = { version = "4.2.4", features = ["derive", "cargo", "color"] }
clap_complete = "4.4.10"
serde = { version = "1.0.163", features = ["derive"] }
serde_json = "1.0.96"
regex = "1.5.4"
Expand Down
14 changes: 14 additions & 0 deletions src/autocompletion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use anyhow::Result;
use clap_complete::{generate_to, shells::Bash};
use std::path::PathBuf;

use crate::cli::define::build_cli_commands;

pub fn generate(path: PathBuf) -> Result<()> {
let mut cmd = build_cli_commands();
let path = generate_to(Bash, &mut cmd, "git-helpe-rs", path)?;

println!("completion file has been generated into: {path:?}");

Ok(())
}
4 changes: 4 additions & 0 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::Result;

use git_helpe_rs::{
autocompletion,
branch::{checkout_to_branch_with_prefix, checkout_to_branch_with_template},
cli,
commit::commit_with_formatted_message,
Expand Down Expand Up @@ -39,6 +40,9 @@ fn main() -> Result<()> {
cli::OperationWithArguments::SetClipboardCommands(args) => {
config.set_clipboard_command(args)
}
cli::OperationWithArguments::GenerateAutocompletionScript(path) => {
autocompletion::generate(path)
}
};

match resp {
Expand Down
10 changes: 10 additions & 0 deletions src/cli/define.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ pub fn build_cli_commands() -> Command {
",
)),
)
.subcommand(
Command::new("generate-autocompletion-script")
.about("Generates bash completion script")
.arg(
Arg::new("output-directory")
.required(true)
.help("Directory in which git-helpe-rs.bash will be placed"),
),
)
// ============= COLORS ============== //
.color(clap::ColorChoice::Always)
.get_styles()
}
Expand Down
7 changes: 7 additions & 0 deletions src/cli/map_to_operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ impl TryFrom<ArgMatches> for ParsedArguments {
},
))
}
Some(("generate-autocompletion-script", args)) => {
let path: PathBuf = args.get_one::<String>("output-directory").unwrap().into();

Ok(OperationWithArguments::GenerateAutocompletionScript(
path.to_owned(),
))
}
Some(("show", _args)) => Ok(OperationWithArguments::Show),
_ => Err(anyhow::anyhow!("Unknown command")),
};
Expand Down
1 change: 1 addition & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub enum OperationWithArguments {
SetBranchPrefix(SetFormat),
SetClipboardCommands(SetClipboardCommands),
Show,
GenerateAutocompletionScript(PathBuf),
}

pub struct ParsedArguments {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod autocompletion;
pub mod branch;
pub mod cli;
pub mod commit;
Expand Down

0 comments on commit 2aaa6a5

Please sign in to comment.