1
0
mirror of https://github.com/danog/ytop.git synced 2024-11-30 04:29:10 +01:00

WIP proc widget

This commit is contained in:
Caleb Bassi 2020-01-13 01:09:10 -08:00
parent 614ee925bd
commit ba88fc1478
3 changed files with 39 additions and 18 deletions

2
Cargo.lock generated
View File

@ -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)",

View File

@ -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<Partition> = self
.partitions
.iter()
.map(|(_key, val)| val.clone())
.collect();
let mut partitions: Vec<Partition> = self.partitions.values().cloned().collect();
partitions.sort_by(|a, b| a.name.cmp(&b.name));
Table::new(

View File

@ -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<u64>,
colorscheme: &'a Colorscheme,
selected_row: usize,
procs: Vec<Proc>,
processes: HashMap<u32, process::Process>,
}
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<Proc>| process.ok())
.collect();
for id in to_remove {
self.processes.remove(&id);
}
}
fn get_update_interval(&self) -> Ratio<u64> {