1use std::env;
2use std::path::PathBuf;
3
4#[derive(Debug, Clone, PartialEq)]
6pub enum OsType {
7 Windows,
8 Linux,
9 MacOS,
10}
11
12impl OsType {
13 pub fn as_str(&self) -> &str {
14 match self {
15 OsType::Windows => "Windows",
16 OsType::Linux => "Linux",
17 OsType::MacOS => "macOS",
18 }
19 }
20}
21
22#[derive(Debug, Clone, PartialEq)]
24pub enum ShellType {
25 Cmd,
26 PowerShell,
27 Bash,
28 Zsh,
29 Fish,
30 Unknown,
31}
32
33impl ShellType {
34 pub fn as_str(&self) -> &str {
35 match self {
36 ShellType::Cmd => "cmd.exe",
37 ShellType::PowerShell => "PowerShell",
38 ShellType::Bash => "bash",
39 ShellType::Zsh => "zsh",
40 ShellType::Fish => "fish",
41 ShellType::Unknown => "unknown",
42 }
43 }
44}
45
46#[derive(Debug, Clone)]
48pub struct SystemInfo {
49 pub os: OsType,
50 pub shell: ShellType,
51 pub current_dir: PathBuf,
52 pub username: Option<String>,
53 pub hostname: Option<String>,
54}
55
56impl SystemInfo {
57 pub fn display(&self) -> String {
58 format!(
59 "OS: {}\nShell: {}\nCurrent Dir: {}\nUsername: {}\nHostname: {}",
60 self.os.as_str(),
61 self.shell.as_str(),
62 self.current_dir.display(),
63 self.username.as_deref().unwrap_or("(unknown)"),
64 self.hostname.as_deref().unwrap_or("(unknown)")
65 )
66 }
67}
68
69pub fn get_system_info() -> SystemInfo {
71 SystemInfo {
72 os: detect_os(),
73 shell: detect_shell(),
74 current_dir: env::current_dir().unwrap_or_else(|_| PathBuf::from(".")),
75 username: env::var("USERNAME").or_else(|_| env::var("USER")).ok(),
76 hostname: env::var("COMPUTERNAME")
77 .or_else(|_| env::var("HOSTNAME"))
78 .ok(),
79 }
80}
81
82pub fn detect_os() -> OsType {
84 #[cfg(target_os = "windows")]
85 return OsType::Windows;
86
87 #[cfg(target_os = "linux")]
88 return OsType::Linux;
89
90 #[cfg(target_os = "macos")]
91 return OsType::MacOS;
92
93 #[cfg(not(any(target_os = "windows", target_os = "linux", target_os = "macos")))]
94 return OsType::Linux; }
96
97pub fn detect_shell() -> ShellType {
99 if cfg!(target_os = "windows") {
100 if let Ok(comspec) = env::var("COMSPEC")
102 && comspec.to_lowercase().contains("cmd.exe")
103 {
104 return ShellType::Cmd;
105 }
106
107 if env::var("PSModulePath").is_ok() {
109 return ShellType::PowerShell;
110 }
111
112 return ShellType::Cmd;
114 }
115
116 if let Ok(shell) = env::var("SHELL") {
118 let shell_lower = shell.to_lowercase();
119
120 if shell_lower.contains("bash") {
121 return ShellType::Bash;
122 } else if shell_lower.contains("zsh") {
123 return ShellType::Zsh;
124 } else if shell_lower.contains("fish") {
125 return ShellType::Fish;
126 }
127 }
128
129 ShellType::Unknown
133}