Skip to main content

tome/
shell_type.rs

1use std::path::Path;
2
3#[derive(Debug, PartialEq)]
4pub enum ShellType {
5    BASH,
6    FISH,
7    UNKNOWN,
8    ZSH,
9}
10
11/// Determine the shell type from the shell_executable_path
12/// passed.
13///
14/// If no string is passed, attempt to infer the information from the
15/// "SHELL" environment variable.
16///
17/// This function is designed to infer the shell from a
18/// variety of architectures and operating systems.
19pub fn get_shell_type(shell_executable_path: &str) -> Result<ShellType, String> {
20    // strip any leading directories. The root should be the shell.
21    let shell_type = match Path::new(shell_executable_path).file_stem() {
22        Some(p) => match p.to_str() {
23            Some(inner_p) => inner_p,
24            None => {
25                return Err(format!(
26                    "could not covert shell type to string for '{}'",
27                    shell_executable_path
28                ));
29            }
30        },
31        None => {
32            return Err(format!(
33                "could not extract file stem to determine shell type from '{}'",
34                shell_executable_path
35            ));
36        }
37    };
38
39    if shell_type.ends_with("fish") {
40        return Ok(ShellType::FISH);
41    }
42
43    if shell_type.ends_with("bash") {
44        return Ok(ShellType::BASH);
45    }
46
47    if shell_type.ends_with("zsh") {
48        return Ok(ShellType::ZSH);
49    }
50
51    Ok(ShellType::UNKNOWN)
52}