winbrew_cli\services/
completions.rs1use anyhow::Result;
2use clap::CommandFactory;
3use clap_complete::{
4 generate,
5 shells::{Bash, Fish, PowerShell, Zsh},
6};
7use std::io::{self, Write};
8
9use crate::cli::{Cli, CompletionShell};
10
11pub fn run(shell: CompletionShell) -> Result<()> {
12 let mut stdout = io::stdout().lock();
13
14 write_completion(shell, &mut stdout);
15
16 stdout.flush()?;
17 Ok(())
18}
19
20pub(crate) fn write_completion<W: Write>(shell: CompletionShell, writer: &mut W) {
21 let mut command = Cli::command();
22 let bin_name = command.get_name().to_string();
23
24 match shell {
25 CompletionShell::Bash => generate(Bash, &mut command, bin_name, writer),
26 CompletionShell::Fish => generate(Fish, &mut command, bin_name, writer),
27 CompletionShell::Zsh => generate(Zsh, &mut command, bin_name, writer),
28 CompletionShell::PowerShell => generate(PowerShell, &mut command, bin_name, writer),
29 }
30}
31
32#[cfg(test)]
33mod tests {
34 use super::{CompletionShell, write_completion};
35
36 #[test]
37 fn bash_completion_generation_writes_output() {
38 let mut buffer = Vec::new();
39
40 write_completion(CompletionShell::Bash, &mut buffer);
41
42 assert!(!buffer.is_empty());
43 }
44}