corelens 0.1.0

Linux TUI for CPU topology, local AI workloads, and GPU metrics
Documentation
use crate::{
    app::{App, ProcessSort},
    ui::layout,
};
use ratatui::{
    prelude::*,
    widgets::{Block, Borders, Clear, Row, Table},
};

pub fn render(frame: &mut Frame<'_>, app: &App) {
    let area = layout::centered(frame.area(), 86, 22);
    frame.render_widget(Clear, area);
    let sort = match app.process_sort {
        ProcessSort::Cpu => "cpu",
        ProcessSort::Memory => "rss",
        ProcessSort::Pid => "pid",
        ProcessSort::Name => "name",
    };
    let rows = app
        .processes
        .iter()
        .enumerate()
        .take(area.height.saturating_sub(4) as usize)
        .map(|(index, process)| {
            Row::new([
                if index == app.selected_process {
                    ">"
                } else {
                    " "
                }
                .to_owned(),
                process.pid.to_string(),
                process.name.chars().take(18).collect::<String>(),
                format!("{:>5.0}%", process.cpu_percent.unwrap_or_default()),
                format_bytes(process.rss_bytes),
                process.last_cpu.map_or("-".into(), |cpu| cpu.to_string()),
            ])
        });
    frame.render_widget(
        Table::new(
            rows,
            [
                Constraint::Length(2),
                Constraint::Length(8),
                Constraint::Length(20),
                Constraint::Length(8),
                Constraint::Length(10),
                Constraint::Length(10),
            ],
        )
        .header(Row::new(["", "PID", "NAME", "CPU", "RSS", "LAST CPU"]))
        .block(
            Block::default()
                .title(format!(
                    " PROCESS BROWSER · sort {sort} · Enter select · p close "
                ))
                .borders(Borders::ALL),
        ),
        area,
    );
}
fn format_bytes(bytes: u64) -> String {
    if bytes >= 1 << 30 {
        format!("{:.1}G", bytes as f64 / (1 << 30) as f64)
    } else {
        format!("{:.0}M", bytes as f64 / (1 << 20) as f64)
    }
}