use std::io;
use std::process::ExitCode;
use clap::{value_parser, Arg, Command};
use clap_complete::Shell;
pub(crate) fn run(args: &[String]) -> anyhow::Result<ExitCode> {
if args.iter().any(|a| matches!(a.as_str(), "--help" | "-h")) {
print_help();
return Ok(ExitCode::SUCCESS);
}
let shell_name = args.first().ok_or_else(|| {
anyhow::anyhow!(
"missing required argument: <SHELL>\n\n\
Usage: skim completions <SHELL>\n\n\
Supported shells: bash, zsh, fish, powershell, elvish"
)
})?;
let shell: Shell = shell_name.parse().map_err(|_| {
anyhow::anyhow!(
"unknown shell: '{shell_name}'\n\n\
Supported shells: bash, zsh, fish, powershell, elvish"
)
})?;
let mut cmd = build_full_command();
clap_complete::generate(shell, &mut cmd, "skim", &mut io::stdout());
Ok(ExitCode::SUCCESS)
}
fn build_full_command() -> Command {
let mut cmd = crate::file_operation_command()
.subcommand_required(false)
.args_conflicts_with_subcommands(true);
let completions_sub = Command::new("completions")
.about("Generate shell completion scripts")
.arg(
Arg::new("shell")
.value_parser(value_parser!(Shell))
.help("Shell to generate completions for"),
);
cmd = cmd.subcommand(completions_sub);
cmd = cmd.subcommand(super::agents::command());
cmd = cmd.subcommand(super::rewrite::command());
cmd = cmd.subcommand(super::init::command());
cmd = cmd.subcommand(super::discover::command());
cmd = cmd.subcommand(super::learn::command());
cmd = cmd.subcommand(super::file::command());
cmd = cmd.subcommand(super::infra::command());
cmd = cmd.subcommand(super::log::command());
const IMPLEMENTED_SUBCOMMANDS: &[&str] = &[
"agents",
"completions",
"discover",
"file",
"infra",
"init",
"learn",
"log",
"rewrite",
];
for name in super::KNOWN_SUBCOMMANDS {
if IMPLEMENTED_SUBCOMMANDS.contains(name) {
continue; }
cmd = cmd.subcommand(Command::new(*name).about("Planned subcommand"));
}
cmd
}
fn print_help() {
println!("skim completions");
println!();
println!(" Generate shell completion scripts");
println!();
println!("Usage: skim completions <SHELL>");
println!();
println!("Supported shells: bash, zsh, fish, powershell, elvish");
println!();
println!("Installation:");
println!(" bash: skim completions bash > ~/.local/share/bash-completion/completions/skim");
println!(" zsh: skim completions zsh > ~/.zfunc/_skim");
println!(" fish: skim completions fish > ~/.config/fish/completions/skim.fish");
}