diff --git a/src/widgets/proc.rs b/src/widgets/proc.rs index 07172c3..b929f73 100644 --- a/src/widgets/proc.rs +++ b/src/widgets/proc.rs @@ -1,14 +1,26 @@ use num_rational::Ratio; +use psutil::process; use tui::buffer::Buffer; -use tui::layout::Rect; -use tui::widgets::Widget; +use tui::layout::{Constraint, Rect}; +use tui::style::{Color, Modifier, Style}; +use tui::widgets::{Row, Table, Widget}; use crate::update::UpdatableWidget; use crate::widgets::block; +struct Proc { + pid: u32, + name: String, + commandline: String, + cpu: f32, + mem: f32, +} + pub struct ProcWidget { title: String, update_interval: Ratio, + + procs: Vec, } impl ProcWidget { @@ -16,12 +28,30 @@ impl ProcWidget { ProcWidget { title: " Processes ".to_string(), update_interval: Ratio::from_integer(1), + + procs: Vec::new(), } } } impl UpdatableWidget for ProcWidget { - fn update(&mut self) {} + fn update(&mut self) { + self.procs = process::processes() + .unwrap() + .into_iter() + .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(name), + cpu: 0.0, + mem: 0.0, + } + }) + .collect(); + } fn get_update_interval(&self) -> Ratio { self.update_interval @@ -30,6 +60,33 @@ impl UpdatableWidget for ProcWidget { impl Widget for ProcWidget { fn draw(&mut self, area: Rect, buf: &mut Buffer) { - block::new().title(&self.title).draw(area, buf); + let row_style = Style::default().fg(Color::White); + + Table::new( + ["Count", "Command", "CPU%", "Mem%"].iter(), + self.procs.iter().map(|proc| { + Row::StyledData( + vec![ + proc.pid.to_string(), + proc.commandline.to_string(), + proc.cpu.to_string(), + proc.mem.to_string(), + ] + .into_iter(), + row_style, + ) + }), + ) + .block(block::new().title(&self.title)) + .header_style(Style::default().fg(Color::Yellow).modifier(Modifier::BOLD)) + .widths(&[ + Constraint::Length(20), + Constraint::Length(20), + Constraint::Length(10), + Constraint::Length(10), + ]) + .style(Style::default().fg(Color::White)) + .column_spacing(1) + .draw(area, buf); } }