which_shell/
lib.rs

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use regex::Regex;
use std::fmt::Display;
use std::{
    ffi::OsStr,
    process::{Command, Stdio},
};
use sysinfo::{Pid, System};

fn exec<I, S>(cmd: S, args: I) -> Option<String>
where
    I: IntoIterator<Item = S>,
    S: AsRef<OsStr>,
{
    let output = Command::new(cmd)
        .args(args)
        .stdin(Stdio::null())
        .output()
        .ok()?;
    Some(String::from_utf8_lossy(&output.stdout).trim().to_string())
}

fn get_file_name(path: &str) -> Option<String> {
    let path = path.replace('\\', "/");
    let name = path.split('/').last()?.split('.').next()?.trim();
    Some(name.into())
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Shell {
    Bash,
    Zsh,
    Fish,
    PowerShell,
    Pwsh,
    Cmd,
    Nu,
    Dash,
    Ksh,
    Tcsh,
    Csh,
    Sh,
    Unknown,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ShellVersion {
    pub shell: Shell,
    pub version: Option<String>,
}

impl Display for ShellVersion {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let Some(ref v) = self.version {
            f.write_str(&format!("{} {}", self.shell, v))
        } else {
            f.write_str(&format!("{}", self.shell))
        }
    }
}

impl From<&str> for Shell {
    fn from(val: &str) -> Self {
        match val {
            "fish" => Shell::Fish,
            "zsh" => Shell::Zsh,
            "OpenConsole" => Shell::PowerShell,
            "powershell" => Shell::PowerShell,
            "bash" => Shell::Bash,
            "pwsh" => Shell::Pwsh,
            "cmd" => Shell::Cmd,
            "nu" => Shell::Nu,
            "dash" => Shell::Dash,
            "ksh" => Shell::Ksh,
            "ksh93" => Shell::Ksh,
            "tcsh" => Shell::Tcsh,
            "csh" => Shell::Csh,
            "bsd-csh" => Shell::Csh,
            "sh" => Shell::Sh,
            _ => Shell::Unknown,
        }
    }
}

impl Display for Shell {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            Shell::Fish => "fish",
            Shell::Zsh => "zsh",
            Shell::Bash => "bash",
            Shell::PowerShell => "powershell",
            Shell::Cmd => "cmd",
            Shell::Pwsh => "pwsh",
            Shell::Nu => "nu",
            Shell::Dash => "dash",
            Shell::Ksh => "ksh",
            Shell::Tcsh => "tcsh",
            Shell::Csh => "csh",
            Shell::Sh => "sh",
            Shell::Unknown => "unknown",
        };
        f.write_str(s)
    }
}

fn get_shell_version(sh: Shell) -> Option<String> {
    let args = match sh {
        Shell::PowerShell => vec!["-c", "$PSVersionTable.PSVersion -replace '\\D', '.'"],
        Shell::Ksh => vec!["-c", "echo $KSH_VERSION"],
        _ => vec!["--version"],
    };
    let version = exec(sh.to_string().as_str(), args)?;
    match sh {
        Shell::Fish => {
            // fish, version 3.6.1
            return Some(version[14..].trim().into());
        }
        Shell::Pwsh => {
            // PowerShell 7.4.1
            return Some(version[11..].trim().into());
        }
        Shell::Bash => {
            // GNU bash, version 5.2.26(1)-release (aarch64-unknown-linux-android)
            let re = Regex::new(r"([0-9]+).([0-9]+).([0-9]+)").unwrap();
            let cap = re.captures(&version)?;

            if let (Some(a), Some(b), Some(c)) = (cap.get(1), cap.get(2), cap.get(3)) {
                return Some(format!("{}.{}.{}", a.as_str(), b.as_str(), c.as_str()));
            }
            None
        }
        Shell::Cmd => {
            // Microsoft Windows [版本 10.0.22635.2700]
            // (c) Microsoft Corporation。保留所有权利。
            let s = version
                .lines()
                .next()?
                .split(' ')
                .last()?
                .split(']')
                .next()?
                .trim();
            Some(s.into())
        }
        Shell::PowerShell => {
            // 5.1.26100.2161
            Some(version)
        }
        Shell::Nu => {
            // 0.99.0
            Some(version)
        }
        Shell::Ksh => {
            // Version AJM 93u+m/1.0.8 2024-01-01
            let v = version.split("/").nth(1)?;
            let v = v.split(" ").next().map(|s| s.trim().to_string());
            v
        }
        Shell::Zsh => {
            // zsh 5.9 (x86_64-ubuntu-linux-gnu)
            let v = version.split(" ").nth(1).map(|s| s.trim().to_string());
            v
        }
        Shell::Tcsh => {
            // tcsh 6.24.13 (Astron) 2024-06-12 (x86_64-unknown-linux) options wide,nls,dl,al,kan,sm,rh,nd,color,filec
            let v = version.split(" ").nth(1).map(|s| s.trim().to_string());
            v
        }
        _ => None,
    }
}

pub fn which_shell() -> Option<ShellVersion> {
    let system = System::new_all();
    // FIXME: maybe we don't need this
    // system.refresh_all();
    let mut pid = std::process::id();
    while let Some(process) = system.process(Pid::from_u32(pid)) {
        let path = process.exe()?.to_str()?;
        let cmd = get_file_name(path)?;
        let shell: Shell = cmd.as_str().into();
        match shell {
            Shell::Unknown => {
                if let Some(parent_id) = process.parent() {
                    pid = parent_id.as_u32();
                } else {
                    break;
                }
                continue;
            }
            _ => {
                let version = get_shell_version(shell);
                return Some(ShellVersion { shell, version });
            }
        }
    }
    None
}