From 4bc54ae5a66f0ee5602513a0e7187a451a37eb78 Mon Sep 17 00:00:00 2001 From: Atsukoro1 Date: Fri, 2 Dec 2022 22:46:44 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Resolution?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/helpers/paths.rs | 15 +++++ src/main.rs | 20 ++++-- src/system/host.rs | 156 +++++++++++++++++++++++++++++++++++++++++++ src/system/net.rs | 20 ++++++ src/system/specs.rs | 89 +++++++++++++++++++++++- 5 files changed, 295 insertions(+), 5 deletions(-) diff --git a/src/helpers/paths.rs b/src/helpers/paths.rs index 090854d..8249f6f 100644 --- a/src/helpers/paths.rs +++ b/src/helpers/paths.rs @@ -13,4 +13,19 @@ pub fn get_pony_path() -> String { .unwrap() .to_string() } +} + +#[cfg(target_os = "windows")] +pub fn get_pony_path() -> String { + if std::env::current_exe().unwrap().to_str().unwrap().contains("target") { + PathBuf::from("ponies/") + .to_str() + .unwrap() + .to_string() + } else { + PathBuf::from("C:\\Program Files\\Ponyfetch\\ponies\\") + .to_str() + .unwrap() + .to_string() + } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 786a4d8..4c49cc8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,15 +31,22 @@ const ACTIONS: [Action; 13] = [ name: None, func: None, }, + #[cfg(target_os = "linux")] Action { action_type: ActionType::Details, name: Some("Distro"), func: Some(system::host::get_distro), }, + #[cfg(target_os = "windows")] + Action { + action_type: ActionType::Details, + name: Some("Product"), + func: Some(system::specs::get_kernel), + }, Action { action_type: ActionType::Details, name: Some("Kernel"), - func: Some(system::specs::get_kernel), + func: Some(system::host::get_distro), }, Action { action_type: ActionType::Details, @@ -71,11 +78,18 @@ const ACTIONS: [Action; 13] = [ name: Some("RAM"), func: Some(system::specs::get_ram_used), }, + #[cfg(target_os = "linux")] Action { action_type: ActionType::Details, name: Some("Init System"), func: Some(system::host::get_init_system), }, + #[cfg(target_os = "windows")] + Action { + action_type: ActionType::Details, + name: Some("GPU"), + func: Some(system::specs::get_gpu), + }, Action { action_type: ActionType::Delimiter, name: None, @@ -91,8 +105,6 @@ const ACTIONS: [Action; 13] = [ fn main() { let args: Args = Args::parse(); - println!("{}", system::host::get_init_system()); - let line_count = helpers::file::get_file_linecount( &format!("{}{}.txt", helpers::paths::get_pony_path(), &args.pony) ); @@ -149,4 +161,4 @@ fn main() { println!(); } -} +} \ No newline at end of file diff --git a/src/system/host.rs b/src/system/host.rs index bcae436..b0383a8 100644 --- a/src/system/host.rs +++ b/src/system/host.rs @@ -1,7 +1,163 @@ +#[cfg(target_os = "linux")] use crate::helpers::file::file_open; +#[cfg(target_os = "linux")] use std::{fs::File, io::Read}; + +#[cfg(any(target_os = "windows", target_os = "linux"))] use std::process::Command; +#[cfg(target_os = "windows")] +pub fn get_hostname() -> String { + let mut hostname = String::new(); + + let output = Command::new("reg") + .args(&["query", "HKLM\\SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ComputerName", "/v", "ComputerName"]) + .output() + .expect("Failed to execute process"); + + let output = String::from_utf8_lossy(&output.stdout); + + for line in output.lines() { + if line.contains("ComputerName") { + hostname = line.split_whitespace().last().unwrap().to_string(); + } + } + + hostname +} + +#[cfg(target_os = "windows")] +pub fn get_user() -> String { + let mut user = String::new(); + + let output = Command::new("reg") + .args(&["query", "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList", "/s"]) + .output() + .expect("Failed to execute process"); + + let output = String::from_utf8_lossy(&output.stdout); + + for line in output.lines() { + if line.contains("ProfileImagePath") { + user = line.split_whitespace().last().unwrap().to_string(); + user = user.split("\\").last().unwrap().to_string(); + } + } + + user +} + +#[cfg(target_os = "windows")] +pub fn get_distro() -> String { + let mut distro = String::new(); + + let output = Command::new("reg") + .args(&["query", "HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "/v", "ProductName"]) + .output() + .expect("Failed to execute process"); + + let output = String::from_utf8_lossy(&output.stdout); + + for line in output.lines() { + if line.contains("ProductName") { + distro = line.split_whitespace().last().unwrap().to_string(); + } + } + + distro +} + +#[cfg(target_os = "windows")] +pub fn get_uptime() -> String { + let mut uptime = String::new(); + + let output = Command::new("reg") + .args(&["query", "HKLM\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment", "/v", "NUMBER_OF_PROCESSORS"]) + .output() + .expect("Failed to execute process"); + + let output = String::from_utf8_lossy(&output.stdout); + + for line in output.lines() { + if line.contains("NUMBER_OF_PROCESSORS") { + uptime = line.split_whitespace().last().unwrap().to_string(); + } + } + + uptime +} + +#[cfg(target_os = "windows")] +pub fn get_shell() -> String { + let mut shell = String::new(); + + let output = Command::new("reg") + .args(&["query", "HKCU\\Console", "/v", "FaceName"]) + .output() + .expect("Failed to execute process"); + + let output = String::from_utf8_lossy(&output.stdout); + + for line in output.lines() { + if line.contains("FaceName") { + shell = line.split_whitespace().last().unwrap().to_string(); + } + } + + if shell == "Lucida Console" { + shell = "PowerShell".to_string(); + } else { + shell = "CMD".to_string(); + } + + shell +} + +#[cfg(target_os = "windows")] +pub fn get_resolution() -> String { + let mut temp_horiz = String::new(); + let mut temp_vert = String::new(); + + let output = Command::new("wmic") + .args(&[ + "path", + "Win32_VideoController", + "get", + "CurrentVerticalResolution,CurrentHorizontalResolution", + "/format:value" + ]) + .output() + .expect("Failed to execute process"); + + let output = String::from_utf8_lossy(&output.stdout); + + let split_by_equals = |item: &str| -> String { + item.split("=") + .collect::>() + .iter() + .nth(1) + .unwrap() + .trim() + .to_string() + }; + + for line in output.lines() { + if line.contains("CurrentHorizontalResolution") { + temp_horiz = split_by_equals(line); + } + + if line.contains("CurrentVerticalResolution") { + temp_vert = split_by_equals(line); + } + } + + format!( + "{}x{}", + temp_horiz, + temp_vert + ).to_string() +} + #[cfg(target_os = "linux")] pub fn get_hostname() -> String { let mut hostname = file_open("/etc/hostname"); diff --git a/src/system/net.rs b/src/system/net.rs index 06e5a20..cbfbc21 100644 --- a/src/system/net.rs +++ b/src/system/net.rs @@ -3,6 +3,26 @@ use std::process::Command; use std::sync::Mutex; use std::ops::Add; +#[cfg(target_os = "windows")] +pub fn get_ipaddr() -> String { + let mut ipaddr = String::new(); + + let output = Command::new("ipconfig") + .args(&["/all"]) + .output() + .expect("Failed to execute process"); + + let output = String::from_utf8_lossy(&output.stdout); + + for line in output.lines() { + if line.contains("IPv4 Address") { + ipaddr = line.split_whitespace().last().unwrap().to_string(); + } + } + + ipaddr +} + #[cfg(target_os = "linux")] pub fn get_ipaddr() -> String { let final_str: Mutex = Mutex::new(String::new()); diff --git a/src/system/specs.rs b/src/system/specs.rs index 6aa53df..06b5e2f 100644 --- a/src/system/specs.rs +++ b/src/system/specs.rs @@ -3,6 +3,72 @@ use std::fs::File; use std::io::Read; use std::rc::Rc; +#[cfg(target_os = "windows")] +pub fn get_cpu() -> String { + use std::process::Command; + + let mut cpu = String::new(); + + let output = Command::new("wmic") + .args(&["cpu", "get", "name"]) + .output() + .expect("Failed to execute process"); + + let output = String::from_utf8_lossy(&output.stdout); + + for line in output.lines() { + if !line.contains("Name") && line.trim().len() > 0 { + cpu = line.to_string(); + } + } + + cpu +} + +#[cfg(target_os = "windows")] +pub fn get_ram_used() -> String { + use std::process::Command; + + let mut ram_used = String::new(); + + let output = Command::new("wmic") + .args(&["OS", "get", "FreePhysicalMemory"]) + .output() + .expect("Failed to execute process"); + + let output = String::from_utf8_lossy(&output.stdout); + + for line in output.lines() { + if !line.contains("FreePhysicalMemory") && line.trim().len() > 0 { + ram_used = line.to_string(); + } + } + + ram_used +} + +#[cfg(target_os = "windows")] +pub fn get_kernel() -> String { + use std::process::Command; + + let mut kernel = String::new(); + + let output = Command::new("wmic") + .args(&["OS", "get", "Caption"]) + .output() + .expect("Failed to execute process"); + + let output = String::from_utf8_lossy(&output.stdout); + + for line in output.lines() { + if !line.contains("Caption") && line.trim().len() > 0 { + kernel = line.to_string(); + } + } + + kernel +} + #[cfg(target_os = "linux")] pub fn get_cpu() -> String { let mut cpu: Rc = Rc::new(String::new()); @@ -51,7 +117,6 @@ pub fn get_ram_used() -> String { ) } -#[cfg(target_os = "linux")] fn eval_ram(line: String) -> u128 { let kbs: u128 = line.split(":") .collect::>()[1].to_string() @@ -71,4 +136,26 @@ pub fn get_kernel() -> String { temp_buf.split(" ") .collect::>()[2] .to_string() +} + +#[cfg(target_os = "windows")] +pub fn get_gpu() -> String { + use std::process::Command; + + let mut gpu = String::new(); + + let output = Command::new("wmic") + .args(&["path", "win32_VideoController", "get", "Name"]) + .output() + .expect("Failed to execute process"); + + let output = String::from_utf8_lossy(&output.stdout); + + for line in output.lines() { + if !line.contains("Name") && line.trim().len() > 0 { + gpu = line.to_string(); + } + } + + gpu } \ No newline at end of file