cc_switch/cli/
completion.rs1use crate::config::ConfigStorage;
2use anyhow::Result;
3use clap::CommandFactory;
4use std::fs;
5use std::io::Write;
6use std::path::PathBuf;
7
8pub fn generate_aliases(shell: &str) -> Result<()> {
16 match shell {
17 "fish" | "zsh" | "bash" => {
18 println!("alias cs='cc-switch'");
19 println!("alias cx='cc-switch codex'");
20 println!("alias co='cc-switch omp'");
21 }
22 _ => {
23 anyhow::bail!(
24 "Unsupported shell: {}. Supported shells: fish, zsh, bash",
25 shell
26 );
27 }
28 }
29
30 Ok(())
31}
32
33fn completion_install_path(shell: &str) -> Option<PathBuf> {
35 let home = dirs::home_dir()?;
36 match shell {
37 "fish" => Some(home.join(".config/fish/completions/cc-switch.fish")),
38 "zsh" => Some(home.join(".zsh/completions/_cc-switch")),
39 "bash" => Some(home.join(".bash_completion.d/cc-switch")),
40 _ => None,
41 }
42}
43
44pub fn generate_completion(shell: &str) -> Result<()> {
52 use crate::cli::Cli;
53
54 let mut app = Cli::command();
55 let mut buf: Vec<u8> = Vec::new();
56
57 match shell {
58 "fish" => {
59 generate_fish_completion(&mut app, &mut buf);
60 }
61 "zsh" => {
62 clap_complete::generate(clap_complete::shells::Zsh, &mut app, "cc-switch", &mut buf);
63 }
64 "bash" => {
65 clap_complete::generate(clap_complete::shells::Bash, &mut app, "cc-switch", &mut buf);
66 }
67 "elvish" => {
68 clap_complete::generate(
69 clap_complete::shells::Elvish,
70 &mut app,
71 "cc-switch",
72 &mut std::io::stdout(),
73 );
74 return Ok(());
75 }
76 "powershell" => {
77 clap_complete::generate(
78 clap_complete::shells::PowerShell,
79 &mut app,
80 "cc-switch",
81 &mut std::io::stdout(),
82 );
83 return Ok(());
84 }
85 _ => {
86 anyhow::bail!(
87 "Unsupported shell: {}. Supported shells: fish, zsh, bash, elvish, powershell",
88 shell
89 );
90 }
91 }
92
93 if let Some(path) = completion_install_path(shell) {
94 if let Some(parent) = path.parent() {
95 fs::create_dir_all(parent)?;
96 }
97 fs::write(&path, &buf)?;
98 eprintln!("Installed {shell} completion to {}", path.display());
99 } else {
100 std::io::stdout().write_all(&buf)?;
101 }
102
103 Ok(())
104}
105
106pub fn list_aliases_for_completion() -> Result<()> {
115 let storage = ConfigStorage::load()?;
116
117 println!("cc");
119 println!("official");
120
121 if storage.configurations.contains_key("current") {
124 println!("current");
125 }
126
127 let mut aliases: Vec<String> = storage.configurations.keys().cloned().collect();
129 aliases.sort();
130
131 for alias_name in aliases {
132 if alias_name != "current" {
133 println!("{alias_name}");
134 }
135 }
136
137 Ok(())
138}
139
140pub fn list_codex_aliases_for_completion() -> Result<()> {
147 let storage = ConfigStorage::load()?;
148
149 if let Some(ref configs) = storage.codex_configurations {
151 let mut aliases: Vec<String> = configs.keys().cloned().collect();
152 aliases.sort();
153
154 for alias_name in aliases {
155 println!("{alias_name}");
156 }
157 }
158
159 Ok(())
160}
161pub fn list_omp_aliases_for_completion() -> Result<()> {
168 let storage = ConfigStorage::load()?;
169
170 if let Some(configs) = &storage.omp_configurations {
172 let mut aliases: Vec<String> = configs.keys().cloned().collect();
173 aliases.sort();
174
175 for alias_name in aliases {
176 println!("{alias_name}");
177 }
178 }
179
180 Ok(())
181}
182
183fn generate_fish_completion(app: &mut clap::Command, out: &mut Vec<u8>) {
185 clap_complete::generate(clap_complete::shells::Fish, app, "cc-switch", out);
186
187 let extra = r#"
188# Custom completion for use subcommand with dynamic aliases
189complete -c cc-switch -n '__fish_cc_switch_using_subcommand use' -f -a '(cc-switch --list-aliases)' -d 'Configuration alias name'
190# Custom completion for switch subcommand (alias for use)
191complete -c cc-switch -n '__fish_cc_switch_using_subcommand switch' -f -a '(cc-switch --list-aliases)' -d 'Configuration alias name'
192# Custom completion for remove subcommand with dynamic aliases
193complete -c cc-switch -n '__fish_cc_switch_using_subcommand remove' -f -a '(cc-switch --list-aliases)' -d 'Configuration alias name'
194
195# Completion for 'completion' subcommand with shell types
196complete -c cc-switch -n '__fish_cc_switch_using_subcommand completion' -f -a 'fish zsh bash elvish powershell' -d 'Shell type'
197complete -c cs -n '__fish_seen_subcommand_from completion' -f -a 'fish zsh bash elvish powershell' -d 'Shell type'
198
199# Custom completion for codex subcommand with dynamic aliases
200complete -c cc-switch -n '__fish_seen_subcommand_from codex' -n '__fish_seen_subcommand_from use' -f -a '(cc-switch --list-codex-aliases)' -d 'Codex configuration alias name'
201complete -c cc-switch -n '__fish_seen_subcommand_from codex' -n '__fish_seen_subcommand_from remove' -f -a '(cc-switch --list-codex-aliases)' -d 'Codex configuration alias name'
202
203# Completion for the 'cs' alias
204complete -c cs -w cc-switch
205
206# Completion for 'cs' alias subcommands
207complete -c cs -n '__fish_use_subcommand' -f -a 'add remove list set-default-dir completion alias use switch current codex omp statusline' -d 'Subcommand'
208
209
210# Completion for 'cs list' subcommand
211complete -c cs -n '__fish_seen_subcommand_from list' -l plain -s p -d 'Plain text output'
212complete -c cs -n '__fish_seen_subcommand_from list' -l name -s n -d 'Show only name and URL'
213
214
215# Completion for 'cs statusline' subcommand
216complete -c cs -n '__fish_seen_subcommand_from statusline' -f -a 'install uninstall' -d 'Statusline action'
217
218# Completion for the 'cx' alias (cc-switch codex)
219complete -c cx -f
220complete -c cx -n '__fish_use_subcommand' -f -a 'add use remove list' -d 'Codex subcommand'
221complete -c cx -n '__fish_seen_subcommand_from use' -f -a '(cc-switch --list-codex-aliases)' -d 'Codex configuration alias name'
222complete -c cx -n '__fish_seen_subcommand_from remove' -f -a '(cc-switch --list-codex-aliases)' -d 'Codex configuration alias name'
223complete -c cx -n '__fish_seen_subcommand_from list' -f -l plain -s p -d 'Plain text output'
224complete -c cx -n '__fish_seen_subcommand_from list' -f -l name -s n -d 'Show only name and auth mode'
225complete -c cx -n '__fish_seen_subcommand_from add' -f -l interactive -s i -d 'Interactive mode'
226complete -c cx -n '__fish_seen_subcommand_from add' -f -l from-file -d 'Import from auth.json (defaults to ~/.codex/auth.json if no path)' -r
227
228# Completion for 'omp' subcommand
229complete -c cc-switch -n '__fish_seen_subcommand_from omp' -n '__fish_seen_subcommand_from use' -f -a '(cc-switch --list-omp-aliases)' -d 'OMP configuration alias name'
230complete -c cc-switch -n '__fish_seen_subcommand_from omp' -n '__fish_seen_subcommand_from remove' -f -a '(cc-switch --list-omp-aliases)' -d 'OMP configuration alias name'
231
232# Completion for the 'co' alias (cc-switch omp)
233complete -c co -f
234complete -c co -n '__fish_use_subcommand' -f -a 'add use remove list' -d 'OMP subcommand'
235complete -c co -n '__fish_seen_subcommand_from use' -f -a '(cc-switch --list-omp-aliases)' -d 'OMP configuration alias name'
236complete -c co -n '__fish_seen_subcommand_from remove' -f -a '(cc-switch --list-omp-aliases)' -d 'OMP configuration alias name'
237complete -c co -n '__fish_seen_subcommand_from list' -f -l plain -s p -d 'Plain text output'
238complete -c co -n '__fish_seen_subcommand_from list' -f -l name -s n -d 'Show only name and auth mode'
239complete -c co -n '__fish_seen_subcommand_from add' -f -l interactive -s i -d 'Interactive mode'
240complete -c co -n '__fish_seen_subcommand_from add' -f -l from-file -d 'Import from .env file (defaults to ~/.omp/agent/.env if no path)' -r
241"#;
242 out.extend_from_slice(extra.as_bytes());
243
244 generate_cs_completion_file();
245 generate_cx_completion_file();
246 generate_co_completion_file();
247}
248
249fn generate_cs_completion_file() {
254 let home = dirs::home_dir().unwrap_or_else(|| std::path::PathBuf::from("~"));
255 let completions_dir = home.join(".config").join("fish").join("completions");
256
257 if !completions_dir.exists()
258 && let Err(e) = fs::create_dir_all(&completions_dir)
259 {
260 eprintln!("Warning: Could not create completions directory: {e}");
261 return;
262 }
263
264 let cs_content = r#"# Completion for 'cs' alias (cc-switch)
265complete -c cs -w cc-switch
266"#;
267
268 let cs_path = completions_dir.join("cs.fish");
269
270 if let Err(e) = fs::write(&cs_path, cs_content) {
271 eprintln!("Warning: Could not write cs.fish: {e}");
272 }
273
274 eprintln!("Created completion file: {}", cs_path.display());
275}
276
277fn generate_cx_completion_file() {
282 let home = dirs::home_dir().unwrap_or_else(|| std::path::PathBuf::from("~"));
284 let completions_dir = home.join(".config").join("fish").join("completions");
285
286 if !completions_dir.exists()
287 && let Err(e) = fs::create_dir_all(&completions_dir)
288 {
289 eprintln!("Warning: Could not create completions directory: {e}");
290 return;
291 }
292
293 let cx_content = r#"# Completion for 'cx' alias (cc-switch codex)
294# Define cx as a fish function
295function cx
296 cc-switch codex $argv
297end
298
299# Disable file completion by default
300complete -c cx -f
301complete -c cx -n '__fish_use_subcommand' -f -a 'add use remove list' -d 'Codex subcommand'
302complete -c cx -n '__fish_seen_subcommand_from use' -f -a '(cc-switch --list-codex-aliases)' -d 'Codex configuration alias name'
303complete -c cx -n '__fish_seen_subcommand_from remove' -f -a '(cc-switch --list-codex-aliases)' -d 'Codex configuration alias name'
304complete -c cx -n '__fish_seen_subcommand_from list' -f -l plain -s p -d 'Plain text output'
305complete -c cx -n '__fish_seen_subcommand_from list' -f -l name -s n -d 'Show only name and auth mode'
306complete -c cx -n '__fish_seen_subcommand_from add' -f -l interactive -s i -d 'Interactive mode'
307complete -c cx -n '__fish_seen_subcommand_from add' -f -l from-file -d 'Import from auth.json (defaults to ~/.codex/auth.json if no path)' -r
308"#;
309
310 let cx_path = completions_dir.join("cx.fish");
311
312 if let Err(e) = fs::write(&cx_path, cx_content) {
313 eprintln!("Warning: Could not write cx.fish: {e}");
314 }
315
316 eprintln!("\nCreated completion file: {}", cx_path.display());
317}
318fn generate_co_completion_file() {
323 let home = dirs::home_dir().unwrap_or_else(|| std::path::PathBuf::from("~"));
324 let completions_dir = home.join(".config").join("fish").join("completions");
325
326 if !completions_dir.exists()
327 && let Err(e) = fs::create_dir_all(&completions_dir)
328 {
329 eprintln!("Warning: Could not create completions directory: {e}");
330 return;
331 }
332
333 let co_content = r#"# Completion for 'co' alias (cc-switch omp)
334# Define co as a fish function
335function co
336 cc-switch omp $argv
337end
338
339# Disable file completion by default
340complete -c co -f
341complete -c co -n '__fish_use_subcommand' -f -a 'add use remove list' -d 'OMP subcommand'
342complete -c co -n '__fish_seen_subcommand_from use' -f -a '(cc-switch --list-omp-aliases)' -d 'OMP configuration alias name'
343complete -c co -n '__fish_seen_subcommand_from remove' -f -a '(cc-switch --list-omp-aliases)' -d 'OMP configuration alias name'
344complete -c co -n '__fish_seen_subcommand_from list' -f -l plain -s p -d 'Plain text output'
345complete -c co -n '__fish_seen_subcommand_from list' -f -l name -s n -d 'Show only name and auth mode'
346complete -c co -n '__fish_seen_subcommand_from add' -f -l interactive -s i -d 'Interactive mode'
347complete -c co -n '__fish_seen_subcommand_from add' -f -l from-file -d 'Import from .env file (defaults to ~/.omp/agent/.env if no path)' -r
348"#;
349
350 let co_path = completions_dir.join("co.fish");
351
352 if let Err(e) = fs::write(&co_path, co_content) {
353 eprintln!("Warning: Could not write co.fish: {e}");
354 }
355
356 eprintln!("\nCreated completion file: {}", co_path.display());
357}