cc_switch/cli/
completion.rs1use crate::config::ConfigStorage;
2use anyhow::Result;
3use clap::CommandFactory;
4use colored::*;
5use std::io::stdout;
6
7pub fn generate_aliases(shell: &str) -> Result<()> {
15 match shell {
16 "fish" => {
17 println!("alias cs='cc-switch'");
18 println!("alias ccd='claude --dangerously-skip-permissions'");
19 }
20 "zsh" => {
21 println!("alias cs='cc-switch'");
22 println!("alias ccd='claude --dangerously-skip-permissions'");
23 }
24 "bash" => {
25 println!("alias cs='cc-switch'");
26 println!("alias ccd='claude --dangerously-skip-permissions'");
27 }
28 _ => {
29 anyhow::bail!(
30 "Unsupported shell: {}. Supported shells: fish, zsh, bash",
31 shell
32 );
33 }
34 }
35
36 Ok(())
37}
38
39pub fn generate_completion(shell: &str) -> Result<()> {
47 use crate::cli::Cli;
48
49 let mut app = Cli::command();
50
51 match shell {
52 "fish" => {
53 generate_fish_completion(&mut app);
54 }
55 "zsh" => {
56 clap_complete::generate(
57 clap_complete::shells::Zsh,
58 &mut app,
59 "cc-switch",
60 &mut stdout(),
61 );
62
63 println!("\n# Useful aliases for cc-switch");
65 println!("# Add these aliases to your ~/.zshrc:");
66 println!("alias cs='cc-switch'");
67 println!("alias ccd='claude --dangerously-skip-permissions'");
68 println!("# Or run this command to add aliases temporarily:");
69 println!("alias cs='cc-switch'; alias ccd='claude --dangerously-skip-permissions'");
70
71 println!("\n# Zsh completion generated successfully");
72 println!("# Add this to your ~/.zsh/completions/_cc-switch");
73 println!("# Or add this line to your ~/.zshrc:");
74 println!("# fpath=(~/.zsh/completions $fpath)");
75 println!("# Then restart your shell or run 'source ~/.zshrc'");
76 println!(
77 "{}",
78 "# Aliases 'cs' and 'ccd' have been added for convenience".green()
79 );
80 }
81 "bash" => {
82 clap_complete::generate(
83 clap_complete::shells::Bash,
84 &mut app,
85 "cc-switch",
86 &mut stdout(),
87 );
88
89 println!("\n# Useful aliases for cc-switch");
91 println!("# Add these aliases to your ~/.bashrc or ~/.bash_profile:");
92 println!("alias cs='cc-switch'");
93 println!("alias ccd='claude --dangerously-skip-permissions'");
94 println!("# Or run this command to add aliases temporarily:");
95 println!("alias cs='cc-switch'; alias ccd='claude --dangerously-skip-permissions'");
96
97 println!("\n# Bash completion generated successfully");
98 println!("# Add this to your ~/.bash_completion or /etc/bash_completion.d/");
99 println!("# Then restart your shell or run 'source ~/.bashrc'");
100 println!(
101 "{}",
102 "# Aliases 'cs' and 'ccd' have been added for convenience".green()
103 );
104 }
105 "elvish" => {
106 clap_complete::generate(
107 clap_complete::shells::Elvish,
108 &mut app,
109 "cc-switch",
110 &mut stdout(),
111 );
112
113 println!("\n# Useful aliases for cc-switch");
115 println!("fn cs {{|@args| cc-switch $@args }}");
116 println!("fn ccd {{|@args| claude --dangerously-skip-permissions $@args }}");
117
118 println!("\n# Elvish completion generated successfully");
119 println!(
120 "{}",
121 "# Aliases 'cs' and 'ccd' have been added for convenience".green()
122 );
123 }
124 "powershell" => {
125 clap_complete::generate(
126 clap_complete::shells::PowerShell,
127 &mut app,
128 "cc-switch",
129 &mut stdout(),
130 );
131
132 println!("\n# Useful aliases for cc-switch");
134 println!("Set-Alias -Name cs -Value cc-switch");
135 println!("function ccd {{ claude --dangerously-skip-permissions @args }}");
136
137 println!("\n# PowerShell completion generated successfully");
138 println!(
139 "{}",
140 "# Aliases 'cs' and 'ccd' have been added for convenience".green()
141 );
142 }
143 _ => {
144 anyhow::bail!(
145 "Unsupported shell: {}. Supported shells: fish, zsh, bash, elvish, powershell",
146 shell
147 );
148 }
149 }
150
151 Ok(())
152}
153
154pub fn list_aliases_for_completion() -> Result<()> {
163 let storage = ConfigStorage::load()?;
164
165 println!("cc");
167 println!("official");
168
169 if storage.configurations.contains_key("current") {
172 println!("current");
173 }
174
175 let mut aliases: Vec<String> = storage.configurations.keys().cloned().collect();
177 aliases.sort();
178
179 for alias_name in aliases {
180 if alias_name != "current" {
181 println!("{alias_name}");
182 }
183 }
184
185 Ok(())
186}
187
188fn generate_fish_completion(app: &mut clap::Command) {
193 clap_complete::generate(
195 clap_complete::shells::Fish,
196 app,
197 "cc-switch",
198 &mut std::io::stdout(),
199 );
200
201 println!("\n# Custom completion for use subcommand with dynamic aliases");
203 println!(
204 "complete -c cc-switch -n '__fish_cc_switch_using_subcommand use' -f -a '(cc-switch --list-aliases)' -d 'Configuration alias name'"
205 );
206 println!("# Custom completion for switch subcommand (alias for use)");
208 println!(
209 "complete -c cc-switch -n '__fish_cc_switch_using_subcommand switch' -f -a '(cc-switch --list-aliases)' -d 'Configuration alias name'"
210 );
211 println!("# Custom completion for remove subcommand with dynamic aliases");
213 println!(
214 "complete -c cc-switch -n '__fish_cc_switch_using_subcommand remove' -f -a '(cc-switch --list-aliases)' -d 'Configuration alias name'"
215 );
216
217 println!("\n# To use these aliases immediately, run:");
219 println!("# eval \"(cc-switch alias fish)\"");
220 println!("\n# Or add them permanently to your ~/.config/fish/config.fish:");
221 println!("# echo \"alias cs='cc-switch'\" >> ~/.config/fish/config.fish");
222 println!(
223 "# echo \"alias ccd='claude --dangerously-skip-permissions'\" >> ~/.config/fish/config.fish"
224 );
225 println!("\n# IMPORTANT: For cs alias completion to work, you must also:");
226 println!(
227 "# 1. Add the completion script: cc-switch completion fish > ~/.config/fish/completions/cc-switch.fish"
228 );
229 println!("# 2. OR run: eval \"(cc-switch completion fish)\" | source");
230
231 println!("\n# Completion for the 'cs' alias");
233 println!("complete -c cs -w cc-switch");
234
235 println!("\n# Completion for 'cs' alias subcommands");
237 println!(
238 "complete -c cs -n '__fish_use_subcommand' -f -a 'add remove list set-default-dir completion alias use switch current' -d 'Subcommand'"
239 );
240
241 println!("\n# Fish completion generated successfully");
242 println!("# Add this to your ~/.config/fish/completions/cc-switch.fish");
243 println!("# Then restart your shell or run 'source ~/.config/fish/config.fish'");
244 println!(
245 "{}",
246 "# Aliases 'cs' and 'ccd' have been added for convenience".green()
247 );
248}