From 20b3aef74a43959d49df63bb6d13b1a1f9dc8169 Mon Sep 17 00:00:00 2001 From: ManOfGoldForever Date: Fri, 6 Feb 2026 01:32:23 -0500 Subject: [PATCH] Slightly scuffed cpu% viewer for processes --- src/main.rs | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index e7a11a9..c73e7d4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,41 @@ +use std::thread; +use std::time::Duration; +use sysinfo::{ProcessRefreshKind, ProcessesToUpdate, System}; + fn main() { - println!("Hello, world!"); + let mut sys = System::new_all(); + sys.refresh_cpu_usage(); + let cpu_count = sys.cpus().len() as f32; + + loop { + sys.refresh_processes_specifics( + sysinfo::ProcessesToUpdate::All, + true, + sysinfo::ProcessRefreshKind::nothing().with_cpu(), + ); + print!("{esc}c", esc = 27 as char); + println!( + "{:<10} {:<30} {:<10} {:<10}", + "PID", "Name", "CPU %", "CPU Core %" + ); + println!("{}", "-".repeat(50)); + let mut procs: Vec<_> = sys.processes().values().collect(); + procs.sort_by(|a, b| b.cpu_usage().partial_cmp(&a.cpu_usage()).unwrap()); + for p in procs.iter() { + let normalized_cpu = p.cpu_usage() / cpu_count; + let mut name = p.name().to_string_lossy().into_owned(); + if name.len() > 30 { + name.truncate(27); + name.push_str("..."); + } + println!( + "{:<10} {:<30} {:<.2}% {:<.2}%", + p.pid(), + p.name().to_string_lossy(), + normalized_cpu, + p.cpu_usage(), + ); + } + thread::sleep(Duration::from_millis(1000)); + } }