command_vault/shell/
hooks.rsuse anyhow::{anyhow, Result};
use std::env;
use std::path::PathBuf;
pub fn get_shell_integration_dir() -> PathBuf {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("shell");
path
}
pub fn get_zsh_integration_path() -> PathBuf {
let mut path = get_shell_integration_dir();
path.push("zsh-integration.zsh");
path
}
pub fn get_bash_integration_path() -> PathBuf {
let mut path = get_shell_integration_dir();
path.push("bash-integration.sh");
path
}
pub fn get_fish_integration_path() -> PathBuf {
let mut path = get_shell_integration_dir();
path.push("fish-integration.fish");
path
}
pub fn detect_current_shell() -> String {
if env::var("FISH_VERSION").is_ok() {
return "fish".to_string();
}
if let Ok(shell_path) = env::var("SHELL") {
let shell_path = shell_path.to_lowercase();
if shell_path.contains("fish") {
return "fish".to_string();
}
if shell_path.contains("zsh") {
return "zsh".to_string();
}
if shell_path.contains("bash") {
return "bash".to_string();
}
}
"bash".to_string()
}
pub fn get_shell_integration_script(shell: &str) -> Result<PathBuf> {
let shell_lower = shell.to_lowercase();
match shell_lower.as_str() {
"zsh" => Ok(get_zsh_integration_path()),
"bash" => Ok(get_bash_integration_path()),
"fish" => Ok(get_fish_integration_path()),
_ => Err(anyhow!("Unsupported shell: {}", shell)),
}
}
pub fn init_shell(shell_override: Option<String>) -> Result<PathBuf> {
let shell = if let Some(shell) = shell_override {
shell
} else {
detect_current_shell()
};
get_shell_integration_script(&shell)
}