schemaui_cli/
completion.rs1use std::path::Path;
2
3use anyhow::Result;
4use clap_complete::{Shell, generate};
5
6use crate::cli::{CompletionCommand, CompletionShell, command_info};
7
8pub fn render_script(shell: CompletionShell) -> String {
9 let command_name = command_name();
10 let mut command = command_info();
11 let mut bytes = Vec::new();
12
13 match shell {
14 CompletionShell::Bash => generate(Shell::Bash, &mut command, command_name, &mut bytes),
15 CompletionShell::Zsh => generate(Shell::Zsh, &mut command, command_name, &mut bytes),
16 CompletionShell::Fish => generate(Shell::Fish, &mut command, command_name, &mut bytes),
17 CompletionShell::PowerShell => {
18 generate(Shell::PowerShell, &mut command, command_name, &mut bytes)
19 }
20 }
21
22 String::from_utf8(bytes).expect("completion script should be utf-8")
23}
24
25pub fn run_cli(args: CompletionCommand) -> Result<()> {
26 print!("{}", render_script(args.shell));
27 Ok(())
28}
29
30fn command_name() -> String {
31 std::env::args()
32 .next()
33 .and_then(|arg0| {
34 Path::new(&arg0)
35 .file_name()
36 .map(|name| name.to_string_lossy().to_string())
37 })
38 .filter(|name| !name.is_empty())
39 .unwrap_or_else(|| "schemaui".to_string())
40}