command_vault/shell/
hooks.rs

1use anyhow::{anyhow, Result};
2use std::env;
3use std::path::PathBuf;
4
5/// Get the directory containing shell integration scripts
6pub fn get_shell_integration_dir() -> PathBuf {
7    let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
8    path.push("shell");
9    path
10}
11
12/// Get the path to the ZSH integration script
13pub fn get_zsh_integration_path() -> PathBuf {
14    let mut path = get_shell_integration_dir();
15    path.push("zsh-integration.zsh");
16    path
17}
18
19/// Get the path to the Bash integration script
20pub fn get_bash_integration_path() -> PathBuf {
21    let mut path = get_shell_integration_dir();
22    path.push("bash-integration.sh");
23    path
24}
25
26/// Get the path to the Fish integration script
27pub fn get_fish_integration_path() -> PathBuf {
28    let mut path = get_shell_integration_dir();
29    path.push("fish-integration.fish");
30    path
31}
32
33/// Detect the current shell from environment variables
34pub fn detect_current_shell() -> String {
35    // First check for FISH_VERSION environment variable (highest priority)
36    if env::var("FISH_VERSION").is_ok() {
37        return "fish".to_string();
38    }
39
40    // Then check SHELL environment variable
41    if let Ok(shell_path) = env::var("SHELL") {
42        let shell_path = shell_path.to_lowercase();
43        
44        // Check for fish first (to match FISH_VERSION priority)
45        if shell_path.contains("fish") {
46            return "fish".to_string();
47        }
48        
49        // Then check for other shells
50        if shell_path.contains("zsh") {
51            return "zsh".to_string();
52        }
53        if shell_path.contains("bash") {
54            return "bash".to_string();
55        }
56    }
57
58    // Default to bash if no shell is detected or unknown shell
59    "bash".to_string()
60}
61
62/// Get the shell integration script path for a specific shell
63pub fn get_shell_integration_script(shell: &str) -> Result<PathBuf> {
64    let shell_lower = shell.to_lowercase();
65    match shell_lower.as_str() {
66        "zsh" => Ok(get_zsh_integration_path()),
67        "bash" => Ok(get_bash_integration_path()),
68        "fish" => Ok(get_fish_integration_path()),
69        _ => Err(anyhow!("Unsupported shell: {}", shell)),
70    }
71}
72
73/// Initialize shell integration
74pub fn init_shell(shell_override: Option<String>) -> Result<PathBuf> {
75    let shell = if let Some(shell) = shell_override {
76        shell
77    } else {
78        detect_current_shell()
79    };
80
81    get_shell_integration_script(&shell)
82}