From ba88fc14787c76b4c5d2496ba296de003d6440e3 Mon Sep 17 00:00:00 2001 From: Caleb Bassi Date: Mon, 13 Jan 2020 01:09:10 -0800 Subject: [PATCH] WIP proc widget --- Cargo.lock | 2 +- src/widgets/disk.rs | 6 +----- src/widgets/proc.rs | 49 ++++++++++++++++++++++++++++++++++----------- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3205095..f41314b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -602,7 +602,7 @@ dependencies = [ [[package]] name = "psutil" version = "1.7.0" -source = "git+https://github.com/cjbassi/rust-psutil?branch=restructure#2e108fe14e72a49d3d4af331ee1d430d526c5eac" +source = "git+https://github.com/cjbassi/rust-psutil?branch=restructure#a427da62084436c3d2187b5abe5f4e2150b2da30" dependencies = [ "glob 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "nix 0.16.1 (git+https://github.com/nix-rust/nix)", diff --git a/src/widgets/disk.rs b/src/widgets/disk.rs index 978aca8..387e3b6 100644 --- a/src/widgets/disk.rs +++ b/src/widgets/disk.rs @@ -107,11 +107,7 @@ impl UpdatableWidget for DiskWidget<'_> { impl Widget for DiskWidget<'_> { fn draw(&mut self, area: Rect, buf: &mut Buffer) { - let mut partitions: Vec = self - .partitions - .iter() - .map(|(_key, val)| val.clone()) - .collect(); + let mut partitions: Vec = self.partitions.values().cloned().collect(); partitions.sort_by(|a, b| a.name.cmp(&b.name)); Table::new( diff --git a/src/widgets/proc.rs b/src/widgets/proc.rs index 5ea47ea..7a8a3dd 100644 --- a/src/widgets/proc.rs +++ b/src/widgets/proc.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + use num_rational::Ratio; use psutil::process; use tui::buffer::Buffer; @@ -23,7 +25,10 @@ pub struct ProcWidget<'a> { update_interval: Ratio, colorscheme: &'a Colorscheme, + selected_row: usize, + procs: Vec, + processes: HashMap, } impl ProcWidget<'_> { @@ -33,31 +38,51 @@ impl ProcWidget<'_> { update_interval: Ratio::from_integer(1), colorscheme, + selected_row: 0, + procs: Vec::new(), + processes: HashMap::new(), } } } impl UpdatableWidget for ProcWidget<'_> { fn update(&mut self) { - self.procs = process::processes() + process::processes() .unwrap() .into_iter() + .filter_map(|process| process.ok()) + .for_each(|process| { + self.processes.insert(process.pid(), process); + }); + + let mut to_remove = Vec::new(); + + self.procs = self + .processes + .values_mut() .map(|process| { - let process = process.unwrap(); - let name = process.name().unwrap(); - Proc { - pid: process.pid(), - name: name.to_string(), - commandline: process - .cmdline() - .unwrap() - .unwrap_or_else(|| format!("[{}]", name)), - cpu: 0.0, - mem: 0.0, + let result = { + let name = process.name()?; + Ok(Proc { + pid: process.pid(), + name: name.to_string(), + commandline: process.cmdline()?.unwrap_or_else(|| format!("[{}]", name)), + cpu: process.cpu_percent()?, + mem: process.memory_percent()?, + }) + }; + if result.is_err() { + to_remove.push(process.pid()); } + result }) + .filter_map(|process: process::ProcessResult| process.ok()) .collect(); + + for id in to_remove { + self.processes.remove(&id); + } } fn get_update_interval(&self) -> Ratio {