From 063f3d0835ea1ba19e8f37fbcd9c80bdd3715bcb Mon Sep 17 00:00:00 2001 From: Christopher Valerio Date: Fri, 29 Mar 2024 13:33:15 -0600 Subject: [PATCH] feat: Adding installation for fish and bash --- crates/aiken/src/cmd/completion/completion.rs | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/crates/aiken/src/cmd/completion/completion.rs b/crates/aiken/src/cmd/completion/completion.rs index 39af1f53..403b2ef5 100644 --- a/crates/aiken/src/cmd/completion/completion.rs +++ b/crates/aiken/src/cmd/completion/completion.rs @@ -72,13 +72,46 @@ fn zsh() -> miette::Result<()> { } +fn fish() -> miette::Result<()> { + // NOTE: Installing completion on ~/.confi/fish/completions + let xdg_dirs = xdg::BaseDirectories::with_prefix("fish/completions").expect("Error finding directory"); + let completion_path = xdg_dirs + .place_config_file("aiken.fish").expect("Cannot create path"); + let mut completion_file = File::create(completion_path).expect("cannot open file"); + generate_wrapper(Shell::Fish, &mut completion_file); + Ok(()) +} + +fn bash() -> miette::Result<()> { + let xdg_dirs = xdg::BaseDirectories::with_prefix("bash-completion/completions").expect("Error finding directory"); + let home = std::env::var("HOME").expect("Cannot find your home directory"); + let config_home = xdg_dirs.get_config_home(); + let completion_path = xdg_dirs + .place_config_file("aiken.completion.bash").expect("Cannot create path"); + + let mut bashrc = OpenOptions::new() + .write(true) + .append(true) + .open(format!("{}/.bashrc", home)) + .expect(".bashrc file not found"); + if let Some(config) = config_home.to_str() { + let path: String = format!("source {config}"); + if let Err(e) = writeln!(bashrc, "{}", path) { + eprintln!("Couldn't write to file: {}", e); + } + } + let mut completion_file = File::create(completion_path).expect("cannot open file"); + generate_wrapper(Shell::Bash, &mut completion_file); + Ok(()) +} + fn completions_to_file(shell: Shell) -> miette::Result<()> { match shell { Shell::Bash => { - todo!() + bash()?; } Shell::Fish => { - todo!() + fish()?; } Shell::Zsh => { zsh()?;