#![allow(
clippy::disallowed_methods,
reason = "Completions command runs synchronously before the tokio runtime is constructed"
)]
use std::{io::Write, path::PathBuf};
use clap::Args;
#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum Shell {
Bash,
Zsh,
Fish,
}
impl Shell {
#[must_use]
pub fn from_env_name(s: &str) -> Option<Self> {
let basename = std::path::Path::new(s)
.file_name()
.and_then(|n| n.to_str())?;
match basename {
"bash" => Some(Shell::Bash),
"zsh" => Some(Shell::Zsh),
"fish" => Some(Shell::Fish),
_ => None,
}
}
pub fn detect_from_env() -> miette::Result<Self> {
let shell_var = std::env::var("SHELL").map_err(|_| {
miette::miette!(
"Could not detect shell: $SHELL environment variable is not set. Use `--shell \
<bash|zsh|fish>` to specify explicitly."
)
})?;
Self::from_env_name(&shell_var).ok_or_else(|| {
miette::miette!(
"Unsupported shell detected from $SHELL={shell_var}. Supported shells: bash, zsh, \
fish. Use `--shell <bash|zsh|fish>` to specify explicitly."
)
})
}
pub fn install_dir(self, home_override: Option<&std::path::Path>) -> miette::Result<PathBuf> {
let home = if let Some(p) = home_override {
p.to_path_buf()
} else {
dirs::home_dir().ok_or_else(|| {
miette::miette!(
"Could not determine home directory: platform home resolution failed"
)
})?
};
let dir = match self {
Shell::Bash => home.join(".local/share/bash-completion/completions"),
Shell::Zsh => zsh_install_dir(
&home,
home_override.is_some(),
home_override.is_none() && std::path::Path::new(ZSH_SYSTEM_SITE_FUNCTIONS).exists(),
),
Shell::Fish => home.join(".config/fish/completions"),
};
Ok(dir)
}
#[must_use]
pub fn completion_filename(self) -> &'static str {
match self {
Shell::Bash => "gf.bash",
Shell::Zsh => "_gf",
Shell::Fish => "gf.fish",
}
}
}
const ZSH_SYSTEM_SITE_FUNCTIONS: &str = "/usr/local/share/zsh/site-functions";
#[must_use]
fn zsh_install_dir(
home: &std::path::Path,
override_given: bool,
system_available: bool,
) -> PathBuf {
let user_dir = home.join(".local/share/zsh/site-functions");
if !override_given && system_available {
PathBuf::from(ZSH_SYSTEM_SITE_FUNCTIONS)
} else {
user_dir
}
}
#[derive(Debug, Args)]
pub struct CompletionsArgs {
#[arg(value_enum, required_unless_present_any = ["install", "uninstall"])]
pub shell: Option<Shell>,
#[arg(long, conflicts_with = "uninstall")]
pub install: bool,
#[arg(long)]
pub uninstall: bool,
}
pub fn generate<C: clap::CommandFactory>(args: &CompletionsArgs) -> std::process::ExitCode {
if args.install {
match install::<C>(args) {
Ok(()) => std::process::ExitCode::SUCCESS,
Err(e) => {
eprintln!("{e:?}");
std::process::ExitCode::from(1)
}
}
} else if args.uninstall {
match uninstall(args) {
Ok(()) => std::process::ExitCode::SUCCESS,
Err(e) => {
eprintln!("{e:?}");
std::process::ExitCode::from(1)
}
}
} else {
let Some(shell) = args.shell else {
eprintln!("Error: shell argument is required when not using --install or --uninstall");
return std::process::ExitCode::from(1);
};
let mut cmd = C::command();
let name = cmd.get_name().to_string();
let stdout = std::io::stdout();
let mut handle = stdout.lock();
write_completion(&mut cmd, shell, &name, &mut handle);
std::process::ExitCode::SUCCESS
}
}
fn resolve_shell(explicit: Option<Shell>) -> miette::Result<Shell> {
explicit.map_or_else(Shell::detect_from_env, Ok)
}
fn write_completion<W: Write>(cmd: &mut clap::Command, shell: Shell, name: &str, writer: &mut W) {
use clap_complete::{generate, shells};
match shell {
Shell::Bash => generate(shells::Bash, cmd, name, writer),
Shell::Zsh => generate(shells::Zsh, cmd, name, writer),
Shell::Fish => generate(shells::Fish, cmd, name, writer),
}
}
fn install<C: clap::CommandFactory>(args: &CompletionsArgs) -> miette::Result<()> {
let shell = resolve_shell(args.shell)?;
let dir = shell.install_dir(None)?;
std::fs::create_dir_all(&dir).map_err(|e| {
miette::miette!(
"Failed to create completion directory {}: {e}",
dir.display()
)
})?;
let file_path = dir.join(shell.completion_filename());
let mut cmd = C::command();
let name = cmd.get_name().to_string();
let mut buf = Vec::new();
write_completion(&mut cmd, shell, &name, &mut buf);
let output = String::from_utf8(buf)
.map_err(|e| miette::miette!("Generated completion is not valid UTF-8: {e}"))?;
std::fs::write(&file_path, &output).map_err(|e| {
miette::miette!(
"Failed to write completion file {}: {e}",
file_path.display()
)
})?;
let shell_name = shell_name_str(shell);
println!(
"✅ Completion installed for {shell_name} → {}",
file_path.display()
);
Ok(())
}
fn uninstall(args: &CompletionsArgs) -> miette::Result<()> {
let shell = resolve_shell(args.shell)?;
let dir = shell.install_dir(None)?;
let file_path = dir.join(shell.completion_filename());
if !file_path.exists() {
let shell_name = shell_name_str(shell);
return Err(miette::miette!(
"No completion file found at {} — nothing to uninstall for {shell_name}",
file_path.display()
));
}
std::fs::remove_file(&file_path).map_err(|e| {
miette::miette!(
"Failed to remove completion file {}: {e}",
file_path.display()
)
})?;
let shell_name = shell_name_str(shell);
println!("✅ Completion uninstalled for {shell_name}");
Ok(())
}
const fn shell_name_str(shell: Shell) -> &'static str {
match shell {
Shell::Bash => "bash",
Shell::Zsh => "zsh",
Shell::Fish => "fish",
}
}
#[cfg(test)]
mod tests {
use clap::CommandFactory as _;
use super::*;
#[test]
fn test_should_detect_bash_from_full_path() {
assert_eq!(Shell::from_env_name("/bin/bash"), Some(Shell::Bash));
}
#[test]
fn test_should_detect_zsh_from_full_path() {
assert_eq!(Shell::from_env_name("/usr/bin/zsh"), Some(Shell::Zsh));
}
#[test]
fn test_should_detect_fish_from_bare_name() {
assert_eq!(Shell::from_env_name("fish"), Some(Shell::Fish));
}
#[test]
fn test_should_return_none_for_unknown_shell() {
assert_eq!(Shell::from_env_name("/bin/tcsh"), None);
assert_eq!(Shell::from_env_name(""), None);
}
#[test]
fn test_should_return_correct_bash_filename() {
assert_eq!(Shell::Bash.completion_filename(), "gf.bash");
}
#[test]
fn test_should_return_correct_zsh_filename() {
assert_eq!(Shell::Zsh.completion_filename(), "_gf");
}
#[test]
fn test_should_return_correct_fish_filename() {
assert_eq!(Shell::Fish.completion_filename(), "gf.fish");
}
#[test]
fn test_should_return_correct_bash_install_dir() {
let home = std::path::Path::new("/tmp/gf-test-home");
let dir = Shell::Bash
.install_dir(Some(home))
.expect("override provided");
assert_eq!(
dir,
PathBuf::from("/tmp/gf-test-home/.local/share/bash-completion/completions")
);
}
#[test]
fn test_should_return_correct_zsh_install_dir() {
let home = std::path::Path::new("/tmp/gf-test-home");
let dir = Shell::Zsh
.install_dir(Some(home))
.expect("override provided");
assert_eq!(
dir,
PathBuf::from("/tmp/gf-test-home/.local/share/zsh/site-functions")
);
}
#[test]
fn test_should_return_correct_fish_install_dir() {
let home = std::path::Path::new("/tmp/gf-test-home");
let dir = Shell::Fish
.install_dir(Some(home))
.expect("override provided");
assert_eq!(
dir,
PathBuf::from("/tmp/gf-test-home/.config/fish/completions")
);
}
#[test]
fn test_should_return_home_zsh_dir_on_override_even_when_system_dir_exists() {
let home = std::path::Path::new("/tmp/gf-test-home");
let dir = zsh_install_dir(home, true, true);
assert_eq!(
dir,
PathBuf::from("/tmp/gf-test-home/.local/share/zsh/site-functions")
);
}
#[test]
fn test_should_return_home_zsh_dir_on_override_when_system_dir_missing() {
let home = std::path::Path::new("/tmp/gf-test-home");
let dir = zsh_install_dir(home, true, false);
assert_eq!(
dir,
PathBuf::from("/tmp/gf-test-home/.local/share/zsh/site-functions")
);
}
#[test]
fn test_should_prefer_system_zsh_dir_without_override_when_available() {
let home = std::path::Path::new("/tmp/gf-test-home");
let dir = zsh_install_dir(home, false, true);
assert_eq!(dir, PathBuf::from("/usr/local/share/zsh/site-functions"));
}
#[test]
fn test_should_fall_back_to_home_zsh_dir_without_override_when_system_missing() {
let home = std::path::Path::new("/tmp/gf-test-home");
let dir = zsh_install_dir(home, false, false);
assert_eq!(
dir,
PathBuf::from("/tmp/gf-test-home/.local/share/zsh/site-functions")
);
}
#[test]
fn test_should_generate_bash_completion_contains_function() {
let mut cmd = crate::Cli::command();
let mut output = Vec::new();
write_completion(&mut cmd, Shell::Bash, "gf", &mut output);
let output = String::from_utf8(output).expect("completion should be valid UTF-8");
assert!(
output.contains("complete -F"),
"bash completion should contain `complete -F` invocation"
);
}
#[test]
fn test_should_generate_zsh_completion_contains_compdef() {
let mut cmd = crate::Cli::command();
let mut output = Vec::new();
write_completion(&mut cmd, Shell::Zsh, "gf", &mut output);
let output = String::from_utf8(output).expect("completion should be valid UTF-8");
assert!(
output.contains("#compdef"),
"zsh completion should contain `#compdef` directive"
);
}
#[test]
fn test_should_generate_fish_completion_contains_complete() {
let mut cmd = crate::Cli::command();
let mut output = Vec::new();
write_completion(&mut cmd, Shell::Fish, "gf", &mut output);
let output = String::from_utf8(output).expect("completion should be valid UTF-8");
assert!(
output.contains("complete -c"),
"fish completion should contain `complete -c` invocation"
);
}
#[test]
fn test_should_generate_non_empty_output_for_all_shells() {
for shell in [Shell::Bash, Shell::Zsh, Shell::Fish] {
let mut cmd = crate::Cli::command();
let mut output = Vec::new();
write_completion(&mut cmd, shell, "gf", &mut output);
assert!(
!output.is_empty(),
"completion for {shell:?} should not be empty"
);
}
}
#[test]
fn test_should_produce_different_output_per_shell() {
let mut cmd_bash = crate::Cli::command();
let mut bash_out = Vec::new();
write_completion(&mut cmd_bash, Shell::Bash, "gf", &mut bash_out);
let mut cmd_zsh = crate::Cli::command();
let mut zsh_out = Vec::new();
write_completion(&mut cmd_zsh, Shell::Zsh, "gf", &mut zsh_out);
assert_ne!(bash_out, zsh_out, "bash and zsh completions should differ");
}
}