Skip to main content

lean_ctx/cli/completions/
mod.rs

1//! CLI tab-completions: `lean-ctx completions <shell>` generates a script,
2//! `lean-ctx __complete <shell> -- <words…>` serves dynamic completions.
3
4mod engine;
5mod shells;
6pub mod spec;
7
8/// `lean-ctx completions zsh|bash|fish` — print a static completion script.
9pub fn run_completions(args: &[String]) {
10    let shell = args.first().map_or("zsh", String::as_str);
11    let script = match shell {
12        "zsh" => shells::zsh_script(),
13        "bash" => shells::bash_script(),
14        "fish" => shells::fish_script(),
15        other => {
16            eprintln!("Unknown shell: {other}  (supported: zsh, bash, fish)");
17            std::process::exit(1);
18        }
19    };
20    print!("{script}");
21}
22
23/// `lean-ctx __complete zsh -- <words…>` — emit completions for the current input.
24#[allow(non_snake_case)]
25pub fn run___complete(args: &[String]) {
26    let (shell, words) = match args.iter().position(|a| a == "--") {
27        Some(pos) => {
28            let shell = args.first().map_or("zsh", String::as_str);
29            (shell, &args[pos + 1..])
30        }
31        None => ("zsh", args),
32    };
33
34    let completions = engine::complete(words);
35
36    let output = match shell {
37        "zsh" => shells::format_zsh(&completions),
38        "fish" => shells::format_fish(&completions),
39        _ => shells::format_bash(&completions),
40    };
41    print!("{output}");
42}