Skip to main content

actr_cli/commands/
completion.rs

1//! `actr completion` — generate shell completion scripts.
2
3use crate::core::{Command, CommandContext, CommandResult, ComponentType};
4use anyhow::Result;
5use async_trait::async_trait;
6use clap::{Args, ValueEnum};
7use clap_complete::{generate, shells};
8use std::io;
9
10#[derive(Args, Debug)]
11#[command(
12    about = "Generate shell completion script",
13    long_about = "Generate a completion script for the given shell. Pipe the output to your shell's completion directory.\n\nExample:\n  actr completion bash > /usr/local/etc/bash_completion.d/actr\n  actr completion zsh  > \"$fpath[1]/_actr\""
14)]
15pub struct CompletionCommand {
16    /// Target shell
17    #[arg(value_enum)]
18    pub shell: Shell,
19}
20
21#[derive(Copy, Clone, Debug, ValueEnum)]
22pub enum Shell {
23    Bash,
24    Zsh,
25    Fish,
26    #[value(name = "powershell")]
27    PowerShell,
28    Elvish,
29}
30
31#[async_trait]
32impl Command for CompletionCommand {
33    async fn execute(&self, _ctx: &CommandContext) -> Result<CommandResult> {
34        // We need the top-level Cli struct to generate completions.
35        // main.rs re-exports this via a helper.
36        let mut cmd = crate::cli::build_cli();
37        let bin = "actr".to_string();
38        let mut out = io::stdout();
39        match self.shell {
40            Shell::Bash => generate(shells::Bash, &mut cmd, bin, &mut out),
41            Shell::Zsh => generate(shells::Zsh, &mut cmd, bin, &mut out),
42            Shell::Fish => generate(shells::Fish, &mut cmd, bin, &mut out),
43            Shell::PowerShell => generate(shells::PowerShell, &mut cmd, bin, &mut out),
44            Shell::Elvish => generate(shells::Elvish, &mut cmd, bin, &mut out),
45        }
46        Ok(CommandResult::Success(String::new()))
47    }
48
49    fn required_components(&self) -> Vec<ComponentType> {
50        vec![]
51    }
52
53    fn name(&self) -> &str {
54        "completion"
55    }
56
57    fn description(&self) -> &str {
58        "Generate shell completion script"
59    }
60}