use anyhow::{Context, Result};
use std::fs;
use std::path::{Path, PathBuf};
pub fn run_install() -> Result<()> {
let bin_dir = get_bin_dir()?;
fs::create_dir_all(&bin_dir).context("Failed to create ~/.local/bin")?;
let zsh_path = bin_dir.join("fab-shell.zsh");
let bash_path = bin_dir.join("fab-shell.bash");
let fish_path = bin_dir.join("fab-shell.fish");
fs::write(&zsh_path, crate::shell_wrapper::ZSH_WRAPPER)
.context("Failed to write fab-shell.zsh")?;
fs::write(&bash_path, crate::shell_wrapper::BASH_WRAPPER)
.context("Failed to write fab-shell.bash")?;
fs::write(&fish_path, crate::shell_wrapper::FISH_WRAPPER)
.context("Failed to write fab-shell.fish")?;
println!("✅ Wrote shell wrappers to {}", bin_dir.display());
let home = dirs_home()?;
let fish_funcs_dir = home.join(".config").join("fish").join("functions");
let fish_relevant = if home.join(".config").join("fish").exists() || fish_funcs_dir.exists() {
let _ = fs::create_dir_all(&fish_funcs_dir);
let fish_func_file = fish_funcs_dir.join("f.fish");
if fs::write(&fish_func_file, crate::shell_wrapper::FISH_WRAPPER).is_ok() {
println!("✅ Installed fish function to {}", fish_func_file.display());
true
} else {
false
}
} else {
false
};
let zshrc = home.join(".zshrc");
let zsh_relevant = ensure_source_line(&zshrc, &bin_dir, "fab-shell.zsh")?;
let bashrc = home.join(".bashrc");
let bash_relevant = ensure_source_line(&bashrc, &bin_dir, "fab-shell.bash")?;
if zsh_relevant || bash_relevant || fish_relevant {
print_reload_hint(&bin_dir, zsh_relevant, bash_relevant, fish_relevant);
} else {
println!("\nℹ️ No ~/.zshrc, ~/.bashrc, or ~/.config/fish found. To use the shell function, add a source line to your shell config manually.");
}
Ok(())
}
pub fn run_uninstall() -> Result<()> {
let home = dirs_home()?;
let mut removed_any = false;
for name in [".zshrc", ".bashrc"] {
let rc_path = home.join(name);
if !rc_path.exists() {
continue;
}
let content = fs::read_to_string(&rc_path)?;
let kept: Vec<&str> = content
.lines()
.filter(|line| {
let trimmed = line.trim();
let is_marker = trimmed == "# f shell function (for cd support)";
let is_source = (trimmed.starts_with("source ") || trimmed.starts_with("source '"))
&& trimmed.contains("fab-shell.zsh")
|| (trimmed.starts_with("source ") || trimmed.starts_with("source '"))
&& trimmed.contains("fab-shell.bash");
!is_marker && !is_source
})
.collect();
if kept.len() != content.lines().count() {
fs::write(&rc_path, format!("{}\n", kept.join("\n")))
.with_context(|| format!("Failed to write {}", rc_path.display()))?;
println!(
"🗑 Removed fab shell function lines from {}",
rc_path.display()
);
removed_any = true;
}
}
let fish_func_file = home
.join(".config")
.join("fish")
.join("functions")
.join("f.fish");
if fish_func_file.exists() {
let _ = fs::remove_file(&fish_func_file);
println!("🗑 Removed {}", fish_func_file.display());
removed_any = true;
}
let bin_dir = get_bin_dir()?;
for wrapper in ["fab-shell.zsh", "fab-shell.bash", "fab-shell.fish"] {
let path = bin_dir.join(wrapper);
if path.exists() {
fs::remove_file(&path)?;
println!("🗑 Removed {}", path.display());
removed_any = true;
}
}
if !removed_any {
println!("ℹ️ Nothing to remove — fab shell function was not installed.");
}
Ok(())
}
fn ensure_source_line(rc_path: &Path, bin_dir: &Path, wrapper_name: &str) -> Result<bool> {
if !rc_path.exists() {
return Ok(false);
}
let plain = format!("source {}/{}", bin_dir.display(), wrapper_name);
let source_line = format!("source '{}/{}'", bin_dir.display(), wrapper_name);
if !file_contains(rc_path, &plain)? && !file_contains(rc_path, &source_line)? {
fs::write(
rc_path,
format!(
"{}\n# f shell function (for cd support)\n{}\n",
fs::read_to_string(rc_path)?,
source_line
),
)
.with_context(|| format!("Failed to append to {}", rc_path.display()))?;
println!("✅ Added source line to {}", rc_path.display());
} else {
println!("ℹ️ {} already has source line", rc_path.display());
}
Ok(true)
}
fn print_reload_hint(bin_dir: &Path, include_zsh: bool, include_bash: bool, include_fish: bool) {
println!();
println!("🔄 Activate the shell function in your CURRENT terminal:");
if include_zsh {
println!(" source {}/fab-shell.zsh", bin_dir.display());
}
if include_bash {
println!(" source {}/fab-shell.bash", bin_dir.display());
}
if include_fish {
println!(" source {}/fab-shell.fish", bin_dir.display());
}
println!();
println!("(The source line in your rc file only affects new shells.)");
}
fn get_bin_dir() -> Result<PathBuf> {
let home = dirs_home()?;
Ok(home.join(".local").join("bin"))
}
fn dirs_home() -> Result<PathBuf> {
let home = std::env::var("HOME").context("HOME env var not set")?;
Ok(PathBuf::from(home))
}
fn file_contains(path: &Path, needle: &str) -> Result<bool> {
let content = fs::read_to_string(path).context(format!("Failed to read {}", path.display()))?;
Ok(content.lines().any(|l| l.trim() == needle.trim()))
}