Skip to main content

lean_ctx_cli/cli/completions/
mod.rs

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