Skip to content

Commit

Permalink
updated table view
Browse files Browse the repository at this point in the history
  • Loading branch information
jacek-kurlit committed Feb 12, 2024
1 parent 8664e89 commit 39c6ac7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 36 deletions.
29 changes: 14 additions & 15 deletions src/processes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::time::Duration;

use anyhow::{anyhow, Context, Result};
use procfs::process::ProcState;
mod users;

use self::users::UserResolver;
Expand All @@ -14,26 +17,31 @@ impl ProcessQuery {
}
}

//FIXME: failing entire app because of single error during fetch of indiwidual status may be not best CX
pub fn find_all_processes(&self) -> Result<Vec<Process>> {
let mut result = Vec::new();
let tps = procfs::ticks_per_second();

println!("{: >10} {: <8} {: >8} CMD", "PID", "TTY", "TIME");

for prc in procfs::process::all_processes().context("Could not load all processes")? {
let prc = prc?;
if let Ok(stat) = prc.stat() {
// total_time is in seconds
let total_time = (stat.utime + stat.stime) as f32 / (tps as f32);
let state = stat
.state()
.context(format!("Failed to retrieve process {} status", stat.pid))?;

let user_name = self
.user_resolver
.resolve_name(prc.uid()?)
.ok_or(anyhow!("Could not find user of process {}", stat.pid))?;
result.push(Process {
pid: stat.pid,
user_name,
total_time,
total_time: Duration::from_secs_f32(total_time),
cmd: stat.comm,
state,
args: prc.cmdline().unwrap_or_default(),
});
}
}
Expand All @@ -44,17 +52,8 @@ impl ProcessQuery {
pub struct Process {
pub pid: i32,
pub user_name: String,
pub total_time: f32,
pub total_time: Duration,
pub cmd: String,
}

impl Process {
pub fn ref_array(&self) -> [String; 4] {
[
format!("\n{}\n", self.user_name),
format!("\n{}\n", self.pid),
format!("\n{}\n", self.total_time),
format!("\n{}\n", self.cmd),
]
}
pub state: ProcState,
pub args: Vec<String>,
}
50 changes: 29 additions & 21 deletions src/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,38 +179,46 @@ fn render_table(f: &mut Frame, app: &mut App, area: Rect) {
.add_modifier(Modifier::REVERSED)
.fg(app.colors.selected_style_fg);

let header = ["user", "pid", "time", "cmd"]
.iter()
.cloned()
.map(Cell::from)
.collect::<Row>()
.style(header_style)
.height(1);
let header = Row::new(vec![
"\nUSER\n",
"\nPID\n",
"\nTIME\n",
"\nSTATUS\n",
"\nCMD\n",
"\nARGS\n",
])
.style(header_style)
.height(3);
let rows = app.processes.iter().enumerate().map(|(i, data)| {
let color = match i % 2 {
0 => app.colors.normal_row_color,
_ => app.colors.alt_row_color,
};
//FIXME: a lot of cloning data, is this needed?
let item = data.ref_array();
item.into_iter()
.map(|content| Cell::from(Text::from(content)))
.collect::<Row>()
.style(Style::new().fg(app.colors.row_fg).bg(color))
.height(4)
Row::new(vec![
format!("\n{}\n", data.user_name),
format!("\n{}\n", data.pid),
format!(
"\n{:02}:{:02}\n",
data.total_time.as_secs() / 3600,
(data.total_time.as_secs() % 3600) / 60
),
format!("\n{:?}\n", data.state),
format!("\n{}\n", data.cmd),
format!("\n{:?}\n", data.args),
])
.style(Style::new().fg(app.colors.row_fg).bg(color))
.height(3)
});
let bar = " █ ";
let t = Table::new(
rows,
[
// + 1 is for padding.
// Constraint::Length(app.longest_item_lens.0 + 1),
// Constraint::Min(app.longest_item_lens.1 + 1),
// Constraint::Min(app.longest_item_lens.2),
Constraint::Percentage(30),
Constraint::Percentage(10),
Constraint::Percentage(10),
Constraint::Percentage(10),
Constraint::Percentage(10),
Constraint::Percentage(20),
Constraint::Percentage(20),
Constraint::Percentage(30),
Constraint::Percentage(40),
],
)
.header(header)
Expand Down

0 comments on commit 39c6ac7

Please sign in to comment.