diff --git a/Cargo.lock b/Cargo.lock index a019440..2800d6e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -28,6 +28,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "bitflags" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635" + [[package]] name = "bumpalo" version = "3.11.1" @@ -256,9 +262,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.139" +version = "0.2.147" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" +checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" [[package]] name = "link-cplusplus" @@ -287,6 +293,17 @@ dependencies = [ "autocfg", ] +[[package]] +name = "nix" +version = "0.27.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2eb04e9c688eff1c89d72b407f168cf79bb9e867a9d3323ed6c01519eb9cc053" +dependencies = [ + "bitflags", + "cfg-if", + "libc", +] + [[package]] name = "ntapi" version = "0.4.0" @@ -336,12 +353,15 @@ name = "oxidefetch" version = "1.3.2" dependencies = [ "byte-unit", + "cfg-if", "chrono", "colored", "compound_duration", + "nix", "quoted-string", "sysinfo", "whoami", + "windows-sys", ] [[package]] @@ -582,3 +602,69 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" diff --git a/Cargo.toml b/Cargo.toml index 42dcb80..a213bfc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,3 @@ opt-level = "z" lto = true codegen-units = 1 panic = "abort" - -[features] -field-titles = [] \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 8f56cac..589790d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,6 +22,8 @@ SOFTWARE. */ +mod terminal; + use byte_unit::*; use chrono::*; use colored::*; @@ -29,6 +31,7 @@ use compound_duration; use std::env; use sysinfo::*; use whoami; +use crate::terminal::get_terminal; fn main() { // Generate system info struct @@ -57,6 +60,7 @@ fn main() { color_print("Kernel:\t", '', &sys_info.kernel_ver, "bright blue"); color_print("Uptime:\t", '', &Some(sys_info.uptime), "bright gray"); color_print("Shell:\t", '', &sys_info.shell, "bright magenta"); + color_print("Terminal:\t", '', &sys_info.terminal, "lime green"); color_print("CPU:\t", '', &Some(sys_info.cpu), "green"); if sys_info.gpu.is_some() { @@ -95,7 +99,7 @@ struct Information { kernel_ver: Option, uptime: String, shell: Option, - _terminal: Option, + terminal: Option, cpu: String, gpu: Option>, memory: String, @@ -109,15 +113,10 @@ impl Information { sys.refresh_all(); Self { username: whoami::username(), - hostname: whoami::hostname(), - os_name: sys.name(), - os_ver: sys.os_version(), - kernel_ver: sys.kernel_version(), - uptime: compound_duration::format_dhms(sys.uptime()), // Tracks the SHELL env var and trims the last item from the resultant fs path. @@ -130,8 +129,7 @@ impl Information { } }, - _terminal: None, // TODO: Add terminal detection. - + terminal: get_terminal(), // TODO: Add terminal detection. cpu: String::from(sys.cpus()[0].brand()), gpu: { @@ -273,6 +271,7 @@ mod test { use crate::Information; use std::fs; + use crate::terminal::get_terminal; // Self explanatory. #[test] diff --git a/src/terminal.rs b/src/terminal.rs new file mode 100644 index 0000000..48a3b35 --- /dev/null +++ b/src/terminal.rs @@ -0,0 +1,112 @@ +use std::env; +use sysinfo::{Pid, PidExt, ProcessExt, System, SystemExt}; + +macro_rules! env_to_name { + ($env: expr, $name: expr) => { + match env::var($env) { + Ok(_) => return Some($name.to_string()), + Err(_) => (), + }; + } +} + +pub fn get_terminal() -> Option { + // Match Apple terminals. + match env::var("TERM_PROGRAM") { + Ok(f) => { + return match f.as_str() { + "iTerm.app" => Some("iTerm2".to_string()), + "Terminal.app" => Some("Apple Terminal".to_string()), + "Hyper.app" => Some("Hyper".to_string()), + _ => None, + }; + } + Err(_) => (), + }; + + env_to_name!("WT_SESSION", "Windows Terminal"); + env_to_name!("SSH_CONNECTION", "SSH"); + env_to_name!("tw52", "TosWin2"); + env_to_name!("tw100", "TosWin2"); + + let mut pid = Pid::from_u32(std::process::id()); + let shell = match env::var("SHELL") { + Ok(f) => f, + Err(_) => return None, + }; + let shell_name = shell.split("/").last().unwrap(); + + // Get to the shell PID. + loop { + let ppid = pid_to_ppid(pid); + + if ppid.is_none() { // We ran out of parents. + return None; + } else if ppid.unwrap().as_u32() == 1 { // We have reached the daemon. + return None; + } + + pid = ppid.unwrap(); // It should be safe to unwrap the PPID now. + + let name = pid_to_name(pid); + + if name.is_none() { + continue; + } + + println!("{}", name.as_ref().unwrap()); + + let name_unwrapped = name.unwrap(); + + if name_unwrapped == shell.as_str() || name_unwrapped == shell_name { + break; // We found the shell. + } + } + + // Try to get parent once more. + pid = match pid_to_ppid(pid) { + None => return None, + Some(f) => f, + }; + + // Get the name. + return match pid_to_name(pid) { + None => None, + Some(f) => Some(f), + }; +} + +fn pid_to_name(pid: Pid) -> Option { + let mut system = System::new(); + system.refresh_processes(); + + for process in system.processes() { + if *process.0 == pid { + return Some(process.1.name().to_string()); + } + } + + None +} + +fn pid_to_ppid(pid: Pid) -> Option { + let mut system = System::new(); + system.refresh_processes(); + + for process in system.processes() { + // Check if the process in question is ours. + if *process.0 != pid { + continue; + } + + // Check if it has a parent. + if process.1.parent().is_none() { + continue; + } + + // Get the parent PID. + return Some(process.1.parent().unwrap()); + } + + None +} \ No newline at end of file