Skip to main content

git_claw/workflow/
completion.rs

1//! Shell auto-completion generator workflow (`git claw completion <shell>`).
2//!
3//! Generates auto-completion scripts for various shells (Bash, Fish, Zsh, PowerShell, Elvish).
4
5use crate::cli::Cli;
6use crate::workflow::error::WorkflowError;
7use clap::CommandFactory;
8use clap_complete::{generate, Shell};
9use std::io::Write;
10
11/// Generates shell completion script and writes it to the provided writer.
12///
13/// :param shell: Target shell (e.g., Fish, Bash, Zsh).
14/// :param writer: Destination stream implementing [`Write`].
15///
16/// # Example
17///
18/// ```
19/// use clap_complete::Shell;
20/// use git_claw::workflow::completion::generate_completion;
21///
22/// let mut buffer = Vec::new();
23/// generate_completion(Shell::Fish, &mut buffer);
24/// let output = String::from_utf8(buffer).expect("valid utf-8");
25/// assert!(output.contains("complete -c git-claw"));
26/// ```
27pub fn generate_completion<W: Write>(shell: Shell, writer: &mut W) {
28    let mut cmd = Cli::command();
29    generate(shell, &mut cmd, "git-claw", writer);
30}
31
32/// Runs the shell completion workflow, writing completion script to stdout.
33///
34/// :param shell: Target shell for which to generate completions.
35/// :return: Result indicating success or error.
36///
37/// # Example
38///
39/// ```
40/// use clap_complete::Shell;
41/// use git_claw::workflow::completion::run_completion_workflow;
42///
43/// let result = run_completion_workflow(Shell::Bash);
44/// assert!(result.is_ok());
45/// ```
46pub fn run_completion_workflow(shell: Shell) -> Result<(), WorkflowError> {
47    let mut stdout = std::io::stdout();
48    generate_completion(shell, &mut stdout);
49    Ok(())
50}