command_vault/shell/
hooks.rs1use anyhow::{anyhow, Result};
2use std::env;
3use std::path::PathBuf;
4
5pub fn get_shell_integration_dir() -> PathBuf {
7 let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
8 path.push("shell");
9 path
10}
11
12pub fn get_zsh_integration_path() -> PathBuf {
14 let mut path = get_shell_integration_dir();
15 path.push("zsh-integration.zsh");
16 path
17}
18
19pub fn get_bash_integration_path() -> PathBuf {
21 let mut path = get_shell_integration_dir();
22 path.push("bash-integration.sh");
23 path
24}
25
26pub fn get_fish_integration_path() -> PathBuf {
28 let mut path = get_shell_integration_dir();
29 path.push("fish-integration.fish");
30 path
31}
32
33pub fn detect_current_shell() -> String {
35 if env::var("FISH_VERSION").is_ok() {
37 return "fish".to_string();
38 }
39
40 if let Ok(shell_path) = env::var("SHELL") {
42 let shell_path = shell_path.to_lowercase();
43
44 if shell_path.contains("fish") {
46 return "fish".to_string();
47 }
48
49 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 "bash".to_string()
60}
61
62pub 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
73pub 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}