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
use std::str::FromStr;
use sysinfo::{get_current_pid, ProcessExt, System, SystemExt};
use thiserror::Error;
#[derive(Error, Debug)]
#[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,
}
#[must_use]
pub fn get_shell_name() -> Result<String, Error> {
let sys = System::new_all();
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);
let shell = shell.strip_prefix("-").unwrap_or(&shell);
Ok(shell.to_owned())
}
#[must_use]
pub fn get_shell() -> Result<Shell, Error> {
Shell::get()
}
#[derive(Debug)]
pub enum Shell {
Bash,
Elvish,
Fish,
Ion,
Nushell,
Powershell,
Xonsh,
Zsh,
}
impl Shell {
#[must_use]
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),
}
}
}
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),
}
}
}