|
1 | | -use anyhow::Result; |
2 | | -use clap::CommandFactory; |
| 1 | +use anyhow::{Context, Result, bail}; |
| 2 | +use clap::{Args, CommandFactory}; |
3 | 3 | use clap_complete::{Shell, generate}; |
| 4 | +use colored::Colorize; |
4 | 5 | use std::io; |
| 6 | +use std::path::PathBuf; |
5 | 7 |
|
6 | 8 | use crate::cli::Cli; |
7 | 9 |
|
8 | | -pub fn run(shell: Shell) -> Result<()> { |
9 | | - let mut cmd = Cli::command(); |
10 | | - let name = cmd.get_name().to_string(); |
11 | | - generate(shell, &mut cmd, name, &mut io::stdout()); |
| 10 | +#[derive(Args)] |
| 11 | +pub struct CompletionsArgs { |
| 12 | + /// Shell to generate completions for (auto-detected from $SHELL if omitted) |
| 13 | + pub shell: Option<Shell>, |
| 14 | + |
| 15 | + /// Install completions into your shell config file |
| 16 | + #[arg(long)] |
| 17 | + pub install: bool, |
| 18 | +} |
| 19 | + |
| 20 | +// ── Shell detection ──────────────────────────────────────────────────────── |
| 21 | + |
| 22 | +fn detect_shell() -> Result<Shell> { |
| 23 | + let shell_env = std::env::var("SHELL").unwrap_or_default(); |
| 24 | + if shell_env.contains("zsh") { |
| 25 | + Ok(Shell::Zsh) |
| 26 | + } else if shell_env.contains("bash") { |
| 27 | + Ok(Shell::Bash) |
| 28 | + } else if shell_env.contains("fish") { |
| 29 | + Ok(Shell::Fish) |
| 30 | + } else { |
| 31 | + bail!( |
| 32 | + "Could not detect your shell from $SHELL ({shell_env}).\n\ |
| 33 | + Specify it explicitly: switchboard completions bash|zsh|fish" |
| 34 | + ) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +fn rc_file(shell: Shell) -> Result<PathBuf> { |
| 39 | + let home = dirs::home_dir().context("Could not determine home directory")?; |
| 40 | + match shell { |
| 41 | + Shell::Bash => Ok(home.join(".bashrc")), |
| 42 | + Shell::Zsh => Ok(home.join(".zshrc")), |
| 43 | + Shell::Fish => Ok(home |
| 44 | + .join(".config/fish/completions") |
| 45 | + .join("switchboard.fish")), |
| 46 | + _ => bail!("Auto-install is not supported for {shell:?}. Add completions manually."), |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +fn eval_line(shell: Shell) -> &'static str { |
| 51 | + match shell { |
| 52 | + Shell::Bash => r#"eval "$(switchboard completions bash)""#, |
| 53 | + Shell::Zsh => r#"eval "$(switchboard completions zsh)""#, |
| 54 | + _ => unreachable!(), |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// ── Entry point ──────────────────────────────────────────────────────────── |
| 59 | + |
| 60 | +pub fn run(args: CompletionsArgs) -> Result<()> { |
| 61 | + let shell = match args.shell { |
| 62 | + Some(s) => s, |
| 63 | + None => detect_shell()?, |
| 64 | + }; |
| 65 | + |
| 66 | + if args.install { |
| 67 | + install_completions(shell) |
| 68 | + } else { |
| 69 | + let mut cmd = Cli::command(); |
| 70 | + let name = cmd.get_name().to_string(); |
| 71 | + generate(shell, &mut cmd, name, &mut io::stdout()); |
| 72 | + Ok(()) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// ── Installer ────────────────────────────────────────────────────────────── |
| 77 | + |
| 78 | +fn install_completions(shell: Shell) -> Result<()> { |
| 79 | + let target = rc_file(shell)?; |
| 80 | + |
| 81 | + match shell { |
| 82 | + Shell::Fish => { |
| 83 | + // Fish: write completions directly to the completions directory |
| 84 | + if let Some(parent) = target.parent() { |
| 85 | + std::fs::create_dir_all(parent)?; |
| 86 | + } |
| 87 | + let mut buf = Vec::new(); |
| 88 | + let mut cmd = Cli::command(); |
| 89 | + let name = cmd.get_name().to_string(); |
| 90 | + generate(shell, &mut cmd, name, &mut buf); |
| 91 | + std::fs::write(&target, &buf)?; |
| 92 | + println!("{} Wrote completions to {}", "✓".green(), target.display()); |
| 93 | + } |
| 94 | + Shell::Bash | Shell::Zsh => { |
| 95 | + let line = eval_line(shell); |
| 96 | + |
| 97 | + // Check if already installed |
| 98 | + if target.exists() { |
| 99 | + let contents = std::fs::read_to_string(&target)?; |
| 100 | + if contents.contains("switchboard completions") { |
| 101 | + println!( |
| 102 | + "{} Completions already configured in {}", |
| 103 | + "✓".green(), |
| 104 | + target.display() |
| 105 | + ); |
| 106 | + return Ok(()); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // Append eval line to rc file |
| 111 | + use std::io::Write; |
| 112 | + let mut file = std::fs::OpenOptions::new() |
| 113 | + .create(true) |
| 114 | + .append(true) |
| 115 | + .open(&target)?; |
| 116 | + writeln!(file)?; |
| 117 | + writeln!(file, "# Switchboard CLI completions")?; |
| 118 | + writeln!(file, "{line}")?; |
| 119 | + |
| 120 | + println!("{} Added completions to {}", "✓".green(), target.display()); |
| 121 | + println!( |
| 122 | + " Restart your shell or run: {}", |
| 123 | + format!("source {}", target.display()).dimmed() |
| 124 | + ); |
| 125 | + } |
| 126 | + _ => bail!("Auto-install is not supported for {shell:?}. Add completions manually."), |
| 127 | + } |
| 128 | + |
12 | 129 | Ok(()) |
13 | 130 | } |
0 commit comments