Skip to main content

shannonshell/
shell.rs

1use std::collections::HashMap;
2use std::path::PathBuf;
3
4/// Returns the shannon config directory, respecting XDG_CONFIG_HOME.
5/// Falls back to ~/.config/shannon.
6pub fn config_dir() -> PathBuf {
7    let base = match std::env::var("XDG_CONFIG_HOME") {
8        Ok(val) if !val.is_empty() => PathBuf::from(val),
9        _ => dirs::home_dir()
10            .unwrap_or_else(|| PathBuf::from("."))
11            .join(".config"),
12    };
13    base.join("shannon")
14}
15
16/// Returns the path to the shared SQLite history database.
17pub fn history_db() -> PathBuf {
18    config_dir().join("history.db")
19}
20
21#[derive(Clone)]
22pub struct ShellState {
23    pub env: HashMap<String, String>,
24    pub cwd: PathBuf,
25    pub last_exit_code: i32,
26}
27
28impl ShellState {
29    pub fn from_current_env() -> Self {
30        ShellState {
31            env: std::env::vars().collect(),
32            cwd: std::env::current_dir().unwrap_or_else(|_| PathBuf::from("/")),
33            last_exit_code: 0,
34        }
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn test_shell_state_from_current_env() {
44        let state = ShellState::from_current_env();
45        assert!(state.env.contains_key("PATH"));
46        assert!(state.cwd.is_absolute());
47        assert_eq!(state.last_exit_code, 0);
48    }
49}