diff --git a/Cargo.lock b/Cargo.lock index e8a1447..875e390 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -176,9 +176,9 @@ checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" [[package]] name = "ntapi" -version = "0.3.7" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c28774a7fd2fbb4f0babd8237ce554b73af68021b5f695a3cebd6c59bac0980f" +checksum = "e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4" dependencies = [ "winapi", ] @@ -256,9 +256,9 @@ dependencies = [ [[package]] name = "sysinfo" -version = "0.23.13" +version = "0.30.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3977ec2e0520829be45c8a2df70db2bf364714d8a748316a10c3c35d4d2b01c9" +checksum = "732ffa00f53e6b2af46208fba5718d9662a421049204e156328b66791ffa15ae" dependencies = [ "cfg-if", "core-foundation-sys", @@ -266,7 +266,7 @@ dependencies = [ "ntapi", "once_cell", "rayon", - "winapi", + "windows", ] [[package]] @@ -309,6 +309,25 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e48a53791691ab099e5e2ad123536d0fff50652600abaf43bbf952894110d0be" +dependencies = [ + "windows-core", + "windows-targets 0.52.5", +] + +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets 0.52.5", +] + [[package]] name = "windows-sys" version = "0.48.0" diff --git a/Cargo.toml b/Cargo.toml index bde892a..4c90962 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,6 @@ authors = ["HARSHIT VIJAY"] [dependencies] colored = "2.0" -sysinfo = "0.23" +sysinfo = "0.30" whoami = "0.9" clap = { version = "4.5", features = ["derive"] } diff --git a/src/main.rs b/src/main.rs index 8a7fb0d..2cde51c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,42 @@ use colored::*; use clap::Command; -use sysinfo::{NetworkExt, ProcessorExt, System, SystemExt}; +use sysinfo::{CpuRefreshKind, RefreshKind, System ,Networks}; use whoami; +fn format_uptime(uptime: u64) -> String { + let (secs, mins, hours, days) = (uptime % 60, (uptime / 60) % 60, (uptime / 3600) % 24, uptime / (3600 * 24)); + + if days > 0 { + format!("{} day{}", days, if days > 1 { "s" } else { "" }) + } else if hours > 0 { + format!("{} hour{}", hours, if hours > 1 { "s" } else { "" }) + } else if mins > 0 { + format!("{} minute{}", mins, if mins > 1 { "s" } else { "" }) + } else { + format!("{} second{}", secs, if secs > 1 { "s" } else { "" }) + } +} + +fn format_network_data(mut data_bytes: u64) -> String { + if data_bytes == 0 { + return "0 B".to_string(); + } + + let units = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"]; + let mut power = 0; + + while data_bytes > 1024 && power < units.len() - 1 { + data_bytes /= 1024; + power += 1; + } + + format!("{:.1} {}", data_bytes as f64 / 1024.0f64.powf(power as f64), units[power]) + } + + fn main() { let mut system = System::new_all(); - // Refresh system information - system.refresh_all(); - let matches = Command::new("ospect") .about("OSpect: Comprehensive System Insights utility tool") .version("0.1.0") @@ -37,8 +65,8 @@ fn main() { // println!("'all' subcommand used"); print_basic_info(&mut system); print_hardware_info(&mut system); - print_network_info(&mut system); - print_os_info(&mut system); + // print_network_info(&mut system); + print_os_info(); }, Some(("hardware", _)) => { // println!("'hardware' subcommand used"); @@ -46,11 +74,11 @@ fn main() { }, Some(("network", _)) => { // println!("'network' subcommand used"); - print_network_info(&mut system); + print_network_info(); }, Some(("os", _)) => { // println!("'os' subcommand used"); - print_os_info(&mut system); + print_os_info(); }, // None => println!("ospect command used without subcommands"), // Default behavior None => print_basic_info(&mut system), // Default behavior @@ -62,34 +90,52 @@ fn main() { fn print_basic_info(system: &mut System) { println!("{}", "Basic System Information".yellow()); println!("{}: {}", "Username".blue(), whoami::username()); - println!("{}: {}", "Hostname".blue(), system.host_name().unwrap_or_default()); - println!("{}: {}", "OS".blue(), system.name().unwrap_or_default()); - println!("{}: {}", "Kernel version".blue(), system.kernel_version().unwrap_or_default()); - println!("{}: {} GB", "Total RAM".blue(), system.total_memory() / 1024 / 1024); + println!("{}: {}", "Hostname".blue(), sysinfo::System::host_name().unwrap_or_default()); + println!("{}: {}", "OS".blue(), sysinfo::System::name().unwrap_or_default()); + println!("{}: {}", "Kernel version".blue(), sysinfo::System::kernel_version().unwrap_or_default()); + println!("{}: {} GB", "Total RAM".blue(), system.total_memory() /1024 /1024 /1024); + println!("{}: {}", "CPU Architecture".blue(), std::env::consts::ARCH); // Add more basic info as needed } fn print_hardware_info(system: &mut System) { println!("{}", "Detailed Hardware Information".yellow()); println!("{}: {} GB", "Total Memory".blue(), system.total_memory() / 1024 / 1024); - println!("{}: {}", "Total CPU Cores".blue(), system.processors().len()); - for processor in system.processors() { - println!("{}: {}", "CPU Model".blue(), processor.name()); - println!("{}: {} MHz", "CPU Speed".blue(), processor.frequency()); + println!("{}: {}", "Total CPU Cores".blue(), system.physical_core_count().unwrap_or_default()); + let s = System::new_with_specifics( + RefreshKind::new().with_cpu(CpuRefreshKind::everything()), + ); + let mut i=0; + let mut is_first_cpu = true; + for cpu in s.cpus() { + if is_first_cpu { + println!("{}: {}", "CPU Model".blue(), cpu.brand()); + is_first_cpu = false; + } + println!("{} {}: {} MHz", "CPU Speed".blue(), i, cpu.frequency()); + println!("{} {}: {}%", "CPU Usage".blue(), i, cpu.cpu_usage()); + + // Additional information can be printed if available + i+=1; } } -fn print_network_info(system: &mut System) { + +fn print_network_info() { println!("{}", "Network Information".yellow()); - for (interface_name, network) in system.networks() { - println!("{}: {}", "Network Interface".blue(), interface_name); - println!("{}: {} Mbps", "Received Data".blue(), network.received() as f64 / 1024.0 / 1024.0); - println!("{}: {} Mbps", "Transmitted Data".blue(), network.transmitted() as f64 / 1024.0 / 1024.0); + let networks = Networks::new_with_refreshed_list(); + for (interface_name , network) in &networks { + println!("[{interface_name}]"); + println!("MAC Address: {}", network.mac_address()); + println!("Total Packets Received: {}", format_network_data(network.total_packets_received())); + println!("Total Packets Transmitted: {}", format_network_data(network.total_packets_transmitted())); + println!(); } } -fn print_os_info(system: &mut System) { +fn print_os_info() { println!("{}", "Detailed OS Information".yellow()); - println!("{}: {}", "OS Name".blue(), system.name().unwrap_or_default()); - println!("{}: {}", "Kernel Version".blue(), system.kernel_version().unwrap_or_default()); + println!("{}: {}", "OS Name".blue(), sysinfo::System::name().unwrap_or_default()); + println!("{}: {}", "Kernel Version".blue(), sysinfo::System::kernel_version().unwrap_or_default()); + println!("{}: {}", "System Uptime".blue(), format_uptime(sysinfo::System::uptime())); // Add more OS info as needed }