Skip to main content

schemaui_cli/
completion.rs

1use std::path::Path;
2
3use argh_complete::Generator;
4use color_eyre::eyre::Result;
5
6use crate::cli::{CompletionCommand, CompletionShell, command_info};
7
8pub fn render_script(shell: CompletionShell) -> String {
9    let command_name = command_name();
10    let info = command_info();
11
12    match shell {
13        CompletionShell::Bash => argh_complete::bash::Bash::generate(&command_name, &info),
14        CompletionShell::Zsh => argh_complete::zsh::Zsh::generate(&command_name, &info),
15        CompletionShell::Fish => argh_complete::fish::Fish::generate(&command_name, &info),
16        CompletionShell::Nushell => argh_complete::nushell::Nushell::generate(&command_name, &info),
17    }
18}
19
20pub fn run_cli(args: CompletionCommand) -> Result<()> {
21    print!("{}", render_script(args.shell));
22    Ok(())
23}
24
25fn command_name() -> String {
26    std::env::args()
27        .next()
28        .and_then(|arg0| {
29            Path::new(&arg0)
30                .file_name()
31                .map(|name| name.to_string_lossy().to_string())
32        })
33        .filter(|name| !name.is_empty())
34        .unwrap_or_else(|| "schemaui".to_string())
35}