lc/cli/
completion.rs

1//! Shell completion commands
2
3use crate::cli::{Cli, CompletionShell};
4use anyhow::Result;
5use clap::CommandFactory;
6use clap_complete::{generate, Shell};
7use std::io;
8
9/// Handle shell completion generation
10pub async fn handle(shell: CompletionShell) -> Result<()> {
11    let mut cmd = Cli::command();
12    let shell_type = match shell {
13        CompletionShell::Bash => Shell::Bash,
14        CompletionShell::Zsh => Shell::Zsh,
15        CompletionShell::Fish => Shell::Fish,
16        CompletionShell::PowerShell => Shell::PowerShell,
17        CompletionShell::Elvish => Shell::Elvish,
18    };
19
20    // Generate basic completions
21    generate(shell_type, &mut cmd, "lc", &mut io::stdout());
22    
23    // Add custom completion functions for dynamic values
24    match shell {
25        CompletionShell::Bash => generate_bash_dynamic_completions(),
26        CompletionShell::Zsh => generate_zsh_dynamic_completions(),
27        CompletionShell::Fish => generate_fish_dynamic_completions(),
28        _ => {
29            eprintln!("Note: Dynamic completions for providers are not yet supported for {:?}", shell);
30            eprintln!("Basic command completions have been generated.");
31        }
32    }
33    
34    Ok(())
35}
36
37/// Generate dynamic completion functions for Bash
38fn generate_bash_dynamic_completions() {
39    println!(r#"
40# Dynamic completion functions for lc (Bash)
41_lc_complete_providers() {{
42    local providers
43    providers=$(lc providers list 2>/dev/null | grep "  •" | awk '{{print $2}}' 2>/dev/null || echo "")
44    COMPREPLY=($(compgen -W "$providers" -- "${{COMP_WORDS[COMP_CWORD]}}"))
45}}
46
47_lc_complete_models() {{
48    local models
49    models=$(lc models 2>/dev/null | awk '/^  •/ {{gsub(/^  • /, ""); print $1}}' 2>/dev/null || echo "")
50    COMPREPLY=($(compgen -W "$models" -- "${{COMP_WORDS[COMP_CWORD]}}"))
51}}
52
53# Register enhanced completion for provider and model options
54complete -o default -F _lc lc
55
56# Instructions for setup
57# Add the above to your ~/.bashrc or ~/.bash_completion
58# Then run: source ~/.bashrc
59"#);
60}
61
62/// Generate dynamic completion functions for Zsh
63fn generate_zsh_dynamic_completions() {
64    println!(r#"
65# Dynamic completion functions for lc (Zsh)
66_lc_providers() {{
67    local providers
68    providers=($(lc providers list 2>/dev/null | grep "  •" | awk '{{print $2}}' 2>/dev/null || echo ""))
69    _describe 'providers' providers
70}}
71
72_lc_models() {{
73    local models
74    models=($(lc models 2>/dev/null | awk '/^  •/ {{gsub(/^  • /, ""); print $1}}' 2>/dev/null || echo ""))
75    _describe 'models' models
76}}
77
78# Instructions for setup
79# Add the above to your ~/.zshrc or a file in your fpath
80# Then run: source ~/.zshrc
81"#);
82}
83
84/// Generate dynamic completion functions for Fish
85fn generate_fish_dynamic_completions() {
86    println!(r#"
87# Dynamic completion functions for lc (Fish)
88function __lc_complete_providers
89    lc providers list 2>/dev/null | grep "  •" | awk '{{print $2}}' 2>/dev/null
90end
91
92function __lc_complete_models
93    lc models 2>/dev/null | awk '/^  •/ {{gsub(/^  • /, ""); print $1}}' 2>/dev/null
94end
95
96# Add dynamic completions
97complete -c lc -s p -l provider -f -a "(__lc_complete_providers)" -d "Provider to use"
98complete -c lc -s m -l model -f -a "(__lc_complete_models)" -d "Model to use"
99
100# Instructions for setup
101# Add the above to ~/.config/fish/completions/lc.fish
102# The file will be loaded automatically by Fish
103"#);
104}