1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use std::str::FromStr;
use sysinfo::{get_current_pid, ProcessExt, RefreshKind, System, SystemExt};
use thiserror::Error;

/// A non-exhaustive list of errors when fetching the process name
/// and resolving it into a shell.
#[derive(Error, Debug, PartialEq, Clone)]
#[non_exhaustive]
pub enum Error {
    #[error("The platform is not supported")]
    UnsupportedPlatform,
    #[error("Current process does not have a parent")]
    NoParent,
    #[error("Unknown shell")]
    Unknown,
    #[error("Unavailable with some su implementations")]
    InSu,
}

/// Fetches the parent process's name in lowercase.
///
/// # Errors
///
/// Returns [`Error::UnsupportedPlatform`] if the call to [`sysinfo::get_current_pid`] fails.
///
/// Returns [`Error::NoParent`] if this process has no parent.
pub fn get_shell_name() -> Result<String, Error> {
    let sys = System::new_with_specifics(RefreshKind::new().with_processes());
    let process = sys
        .get_process(get_current_pid().map_err(|_| Error::UnsupportedPlatform)?)
        .expect("Process with current pid does not exist");
    let parent = sys
        .get_process(process.parent().ok_or(Error::NoParent)?)
        .expect("Process with parent pid does not exist");
    let shell = parent.name().trim().to_lowercase();
    let shell = shell.strip_suffix(".exe").unwrap_or(&shell); // windows bad
    let shell = shell.strip_prefix('-').unwrap_or(shell); // login shells
    Ok(shell.to_owned())
}
pub fn get_shell() -> Result<Shell, Error> {
    Shell::get()
}

/// The type of shell.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[non_exhaustive]
pub enum Shell {
    Bash,
    Elvish,
    Fish,
    Ion,
    Nushell,
    Powershell,
    Xonsh,
    Zsh,
}

impl Shell {
    /// Fetch the shell running this process.
    ///
    /// See [`get_shell_name`] for more info.
    pub fn get() -> Result<Self, Error> {
        match get_shell_name()?.as_str() {
            "su" => Err(Error::InSu),
            shell if shell.starts_with("python") => Ok(Self::Xonsh),
            shell => Self::from_str(shell),
        }
    }
    /// Get the string representation of this shell.
    ///
    /// All names are in lowercase.
    ///
    /// Unexpected values or ambiguities are sorted out below:
    ///
    /// - [`Shell::Nushell`] => `nu`
    /// - [`Shell::Powershell`] => `powershell`
    pub fn to_str(self) -> &'static str {
        match self {
            Shell::Bash => "bash",
            Shell::Elvish => "elvish",
            Shell::Fish => "fish",
            Shell::Ion => "ion",
            Shell::Nushell => "nu",
            Shell::Powershell => "powershell",
            Shell::Xonsh => "xonsh",
            Shell::Zsh => "zsh",
        }
    }
}

impl FromStr for Shell {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "bash" => Ok(Shell::Bash),
            "elvish" => Ok(Shell::Elvish),
            "fish" => Ok(Shell::Fish),
            "ion" => Ok(Shell::Ion),
            "nu" | "nushell" => Ok(Shell::Nushell),
            "pwsh" | "powershell" => Ok(Shell::Powershell),
            "xonsh" | "xon.sh" => Ok(Shell::Xonsh),
            "zsh" => Ok(Shell::Zsh),
            _ => Err(Error::Unknown),
        }
    }
}