From 41220a0938af05564a525405c9ecfc0186b23091 Mon Sep 17 00:00:00 2001 From: NamedNeon <130935222+NamedNeon@users.noreply.github.com> Date: Tue, 12 Sep 2023 14:13:38 -0400 Subject: [PATCH 01/10] minor refactoring --- src/main.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index d8177f6..8f56cac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,7 +32,7 @@ use whoami; fn main() { // Generate system info struct - let sys_info = InformationStruct::new(); + let sys_info = Information::new(); // Format the date and time let datetime_formatted = format!( @@ -41,12 +41,10 @@ fn main() { Utc::now().format("%H:%M %Y-%m-%d") ); - println!(); - // TODO: Add support to change what items print, as well as their colors. // This should be done via some sort of user accessible, persistent config, // and preferably can be modified via env vars. - println!("{}", String::from(">>> OxideFetch  <<<").red()); + println!("\n{}", String::from(">>> OxideFetch  <<<").red()); color_print("Date:\t", '󰃰', &Some(datetime_formatted), "bright yellow"); color_print( "Host:\t", @@ -60,11 +58,14 @@ fn main() { color_print("Uptime:\t", '', &Some(sys_info.uptime), "bright gray"); color_print("Shell:\t", '', &sys_info.shell, "bright magenta"); color_print("CPU:\t", '', &Some(sys_info.cpu), "green"); - if let Some(gpus) = sys_info.gpu { + + if sys_info.gpu.is_some() { + let gpus = sys_info.gpu.unwrap(); for gpu in gpus { color_print("GPU:\t", '󰍹', &Some(gpu), "bright green") } } + //color_print("GPU:\t", '', &sys_info.gpu, "bright green"); color_print("Memory:\t", '󰍛', &Some(sys_info.memory), "bright blue"); } @@ -82,7 +83,7 @@ fn color_print(field_title: &str, icon: char, field: &Option, color: &st } #[derive(Debug)] -struct InformationStruct { +struct Information { // Only fields whose setters can fail, are given Option types. // Unsure if I should coerce these fields into an Option *here*, or // within the args of color_print, since that function only accepts args of @@ -102,7 +103,7 @@ struct InformationStruct { color: String, } -impl InformationStruct { +impl Information { fn new() -> Self { let mut sys = System::new_all(); sys.refresh_all(); @@ -270,13 +271,13 @@ impl InformationStruct { #[cfg(test)] mod test { - use crate::InformationStruct; + use crate::Information; use std::fs; // Self explanatory. #[test] pub fn log_gathered_data() { - let sys_info = InformationStruct::new(); + let sys_info = Information::new(); let data_string = format!("{:#?}", sys_info); let result = fs::write("./test_output.txt", data_string); From 89aab3aa7b8f10a69bf4d21cd2521ec1dbe63965 Mon Sep 17 00:00:00 2001 From: NamedNeon <130935222+NamedNeon@users.noreply.github.com> Date: Wed, 13 Sep 2023 13:54:39 -0400 Subject: [PATCH 02/10] Inital terminal detection work. --- Cargo.lock | 90 +++++++++++++++++++++++++++++++++++++- Cargo.toml | 3 -- src/main.rs | 15 +++---- src/terminal.rs | 112 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 207 insertions(+), 13 deletions(-) create mode 100644 src/terminal.rs 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 From bef621f04fa5d98719857753b882896f99b86140 Mon Sep 17 00:00:00 2001 From: NamedNeon <130935222+NamedNeon@users.noreply.github.com> Date: Wed, 13 Sep 2023 13:58:20 -0400 Subject: [PATCH 03/10] remove TODO. --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 589790d..345e1a0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -129,7 +129,7 @@ impl Information { } }, - terminal: get_terminal(), // TODO: Add terminal detection. + terminal: get_terminal(), cpu: String::from(sys.cpus()[0].brand()), gpu: { From 43174e5ba7ea44d9e0b43adfbf296b76eac6c972 Mon Sep 17 00:00:00 2001 From: NamedNeon <130935222+NamedNeon@users.noreply.github.com> Date: Wed, 13 Sep 2023 14:19:10 -0400 Subject: [PATCH 04/10] fix identification of Darwin-based systems --- src/main.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 345e1a0..302d731 100644 --- a/src/main.rs +++ b/src/main.rs @@ -111,10 +111,25 @@ impl Information { fn new() -> Self { let mut sys = System::new_all(); sys.refresh_all(); + + let mut os_name = sys.name(); + + // Get full OS name for Darwin-based systems (i.e. macOS, iOS). + if os_name == Some("Darwin".to_string()) { + let long_os = sys.long_os_version(); + if long_os.is_some() { + // Isolate system type from version information. + let long_os_split = + long_os.unwrap().split_whitespace().collect::>()[0].to_string(); + + os_name = Some(long_os_split); + } + } + Self { username: whoami::username(), hostname: whoami::hostname(), - os_name: sys.name(), + os_name, os_ver: sys.os_version(), kernel_ver: sys.kernel_version(), uptime: compound_duration::format_dhms(sys.uptime()), @@ -225,7 +240,7 @@ impl Information { "Windows" => '', "Android" => '', "iOS" => '', - "macOS" => '', + "MacOS" => '', "Unknown System" => '?', _ => { if sys From 07b95707e5c9a97b023d3fdf6b82fbb827d9a21f Mon Sep 17 00:00:00 2001 From: NamedNeon <130935222+NamedNeon@users.noreply.github.com> Date: Wed, 13 Sep 2023 14:35:04 -0400 Subject: [PATCH 05/10] set terminal icon and color --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 302d731..3d8674d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,7 +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("Terminal:\t", '', &sys_info.terminal, "bright green"); color_print("CPU:\t", '', &Some(sys_info.cpu), "green"); if sys_info.gpu.is_some() { From f6225b1027f5d9690bc5748a79c5005a313cb50d Mon Sep 17 00:00:00 2001 From: NamedNeon <130935222+NamedNeon@users.noreply.github.com> Date: Wed, 13 Sep 2023 14:38:09 -0400 Subject: [PATCH 06/10] tie icon match to os_name --- src/main.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3d8674d..d8a0562 100644 --- a/src/main.rs +++ b/src/main.rs @@ -129,7 +129,7 @@ impl Information { Self { username: whoami::username(), hostname: whoami::hostname(), - os_name, + os_name: os_name.clone(), os_ver: sys.os_version(), kernel_ver: sys.kernel_version(), uptime: compound_duration::format_dhms(sys.uptime()), @@ -212,8 +212,7 @@ impl Information { Byte::from(sys.total_memory()).get_appropriate_unit(true) ), - icon: match sys - .name() + icon: match os_name .unwrap_or(String::from("Unknown System")) .as_ref() // Getting the icon for the distro. From 7fbc8f902df36b99830a729936bfa5b07e71f531 Mon Sep 17 00:00:00 2001 From: NamedNeon <130935222+NamedNeon@users.noreply.github.com> Date: Wed, 13 Sep 2023 15:08:10 -0400 Subject: [PATCH 07/10] add back field-titles --- Cargo.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index a213bfc..1d2e8f4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,3 +20,6 @@ opt-level = "z" lto = true codegen-units = 1 panic = "abort" + +[features] +field-titles = [] From b43e1491e7b2797b46f055e486c15dc0da123ca4 Mon Sep 17 00:00:00 2001 From: NamedNeon <130935222+NamedNeon@users.noreply.github.com> Date: Wed, 13 Sep 2023 17:46:37 -0400 Subject: [PATCH 08/10] terminal: detect self-hosted shells (i.e. cmd and powershell); conemu detection; extension stripping --- src/terminal.rs | 55 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/src/terminal.rs b/src/terminal.rs index 48a3b35..0b7ffcc 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -1,6 +1,12 @@ use std::env; use sysinfo::{Pid, PidExt, ProcessExt, System, SystemExt}; +// Allows detection of shells that host themselves (i.e. Command Prompt). +const SELF_HOSTED_SHELLS: [&str; 2] = [ + "cmd.exe", + "powershell.exe" +]; + macro_rules! env_to_name { ($env: expr, $name: expr) => { match env::var($env) { @@ -24,6 +30,7 @@ pub fn get_terminal() -> Option { Err(_) => (), }; + env_to_name!("ConEmuPID", "ConEmu"); env_to_name!("WT_SESSION", "Windows Terminal"); env_to_name!("SSH_CONNECTION", "SSH"); env_to_name!("tw52", "TosWin2"); @@ -32,12 +39,16 @@ pub fn get_terminal() -> Option { let mut pid = Pid::from_u32(std::process::id()); let shell = match env::var("SHELL") { Ok(f) => f, - Err(_) => return None, + Err(_) => "".to_string(), }; let shell_name = shell.split("/").last().unwrap(); + let mut name: Option = None; + + let mut self_hosted = false; + // Get to the shell PID. - loop { + 'find_shell: loop { let ppid = pid_to_ppid(pid); if ppid.is_none() { // We ran out of parents. @@ -48,32 +59,46 @@ pub fn get_terminal() -> Option { pid = ppid.unwrap(); // It should be safe to unwrap the PPID now. - let name = pid_to_name(pid); + let pid_name = pid_to_name(pid); - if name.is_none() { + if pid_name.is_none() { continue; } - println!("{}", name.as_ref().unwrap()); + let name_unwrapped = pid_name.unwrap(); - let name_unwrapped = name.unwrap(); + // Detect self-hosted shells. + for shell in SELF_HOSTED_SHELLS { + if name_unwrapped == shell { + self_hosted = true; + name = Some(name_unwrapped); + break 'find_shell; + } + } if name_unwrapped == shell.as_str() || name_unwrapped == shell_name { + name = Some(name_unwrapped); break; // We found the shell. } } - // Try to get parent once more. - pid = match pid_to_ppid(pid) { - None => return None, - Some(f) => f, - }; + if !self_hosted { + // Try to get parent once more. + match pid_to_ppid(pid) { + None => return None, + Some(f) => { + // Try to get name. + name = pid_to_name(f); + }, + }; + } - // Get the name. - return match pid_to_name(pid) { + return match name { + Some(f) => { + Some(f.split(".").nth(0).unwrap().to_string()) + }, None => None, - Some(f) => Some(f), - }; + } } fn pid_to_name(pid: Pid) -> Option { From 295cdec9fa4a1f3aab3d1ada1a7c5db1bccb7731 Mon Sep 17 00:00:00 2001 From: NamedNeon <130935222+NamedNeon@users.noreply.github.com> Date: Wed, 13 Sep 2023 18:12:46 -0400 Subject: [PATCH 09/10] added pretty names. --- Cargo.lock | 87 +------------------------------------------------ Cargo.toml | 1 + src/terminal.rs | 27 ++++++++++++++- 3 files changed, 28 insertions(+), 87 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2800d6e..a65d5d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -28,12 +28,6 @@ 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" @@ -293,17 +287,6 @@ 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" @@ -353,15 +336,13 @@ name = "oxidefetch" version = "1.3.2" dependencies = [ "byte-unit", - "cfg-if", "chrono", "colored", "compound_duration", - "nix", + "lazy_static", "quoted-string", "sysinfo", "whoami", - "windows-sys", ] [[package]] @@ -602,69 +583,3 @@ 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 1d2e8f4..d3ccf74 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ byte-unit = "4.0.18" chrono = "0.4.23" colored = "2.0.0" compound_duration = "1.2.0" +lazy_static = "1.4.0" quoted-string = "0.6.1" sysinfo = "0.28.1" whoami = "1.3.0" diff --git a/src/terminal.rs b/src/terminal.rs index 0b7ffcc..7c04a1b 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -1,6 +1,23 @@ use std::env; +use std::collections::HashMap; +use lazy_static::lazy_static; use sysinfo::{Pid, PidExt, ProcessExt, System, SystemExt}; +lazy_static! { + static ref PRETTY_NAMES: HashMap<&'static str, &'static str> = { + let mut m = HashMap::new(); + m.insert("cmd", "Command Prompt"); + m.insert("powershell", "PowerShell"); + m.insert("konsole", "Konsole"); + m.insert("gnome-terminal", "GNOME Terminal"); + m.insert("xterm", "XTerm"); + m.insert("xfce4-terminal", "XFCE Terminal"); + m.insert("kitty", "KiTTY"); + m.insert("alacritty", "Alacritty"); + m + }; +} + // Allows detection of shells that host themselves (i.e. Command Prompt). const SELF_HOSTED_SHELLS: [&str; 2] = [ "cmd.exe", @@ -95,7 +112,15 @@ pub fn get_terminal() -> Option { return match name { Some(f) => { - Some(f.split(".").nth(0).unwrap().to_string()) + // Remove the file extension. + let mut res = f.split(".").nth(0).unwrap().to_string(); + + // Try to get a "prettier name". + if PRETTY_NAMES.contains_key(res.as_str()) { + res = PRETTY_NAMES.get(res.as_str()).unwrap().to_string(); + } + + Some(res) }, None => None, } From d36e7853e7c594854bdc2c910dd27296b18b45fd Mon Sep 17 00:00:00 2001 From: NamedNeon <130935222+NamedNeon@users.noreply.github.com> Date: Wed, 13 Sep 2023 18:16:38 -0400 Subject: [PATCH 10/10] finishing touches --- README.md | 6 +++--- src/terminal.rs | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 701e2bb..375a38e 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,8 @@ OxideFetch can display all of the following information: - Operating system name, symbol, and matching color - Kernel version - Uptime -- Shell +- Shell +- Terminal - CPU - GPU - Memory @@ -63,8 +64,7 @@ No weird quirks to report at this time. - Add host system name detection such as "Windows Subsystem for Linux", "IdeaPad 3", "Dell Optiplex", etc. - Add package count/package manager detection - Crosstest on more distributions to verify `sys.name()` outputs -#### Distant future: -- Add terminal emulator detection by walking up the process tree +#### Distant future: - More extensible user configuration for entry formatting ### Changelog diff --git a/src/terminal.rs b/src/terminal.rs index 7c04a1b..81723b0 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -24,7 +24,7 @@ const SELF_HOSTED_SHELLS: [&str; 2] = [ "powershell.exe" ]; -macro_rules! env_to_name { +macro_rules! match_env_to_terminal { ($env: expr, $name: expr) => { match env::var($env) { Ok(_) => return Some($name.to_string()), @@ -47,11 +47,11 @@ pub fn get_terminal() -> Option { Err(_) => (), }; - env_to_name!("ConEmuPID", "ConEmu"); - env_to_name!("WT_SESSION", "Windows Terminal"); - env_to_name!("SSH_CONNECTION", "SSH"); - env_to_name!("tw52", "TosWin2"); - env_to_name!("tw100", "TosWin2"); + match_env_to_terminal!("ConEmuPID", "ConEmu"); + match_env_to_terminal!("WT_SESSION", "Windows Terminal"); + match_env_to_terminal!("SSH_CONNECTION", "SSH"); + match_env_to_terminal!("tw52", "TosWin2"); + match_env_to_terminal!("tw100", "TosWin2"); let mut pid = Pid::from_u32(std::process::id()); let shell = match env::var("SHELL") {