use crate::commands::{Cli, Result, RunCommand};
use clap::CommandFactory;
use clap_complete::{Shell, generate};
use std::io::stdout;

/// Print completions for the given shell.
///
/// These can be sourced directly, e.g.:
///
/// for fish run:
/// ```
///    jobber completions fish | source
/// ```
///
/// for zsh run:
/// ```
///     source <(jobber completions zsh)
/// ```
///
/// But they can also be loaded/stored permanently, e.g.:
///
/// for zsh run:
/// ```
///     mkdir -p ~/.zsh/completions
///     jobber completions zsh > ~/.zsh/completions/_jobber
/// ```
///   ...and add this to your .zshrc:
/// ```
///     fpath=(~/.zsh/completions $fpath)
///     autoload -Uz compinit && compinit
///```
/// for fish just run:
/// ```
///     jobber completions fish > ~/.config/fish/completions/jobber.fish
/// ```
#[derive(clap::Parser)]
#[command(verbatim_doc_comment)]
pub(crate) struct Completions {
    /// Shell to print completions for
    #[clap(id = "SHELL")]
    shell: Shell,
}

impl RunCommand for Completions {
    fn run(&self, _cli: &Cli) -> Result<()> {
        generate(self.shell, &mut Cli::command(), "jobber", &mut stdout());
        Ok(())
    }
}
