use crate::{app::App, model::cpu::CoreKind, ui::cpu_matrix_grid::display_label};
use ratatui::{
prelude::*,
widgets::{Block, Borders, Paragraph, Wrap},
};
pub fn render(frame: &mut Frame<'_>, area: Rect, app: &App) {
let block = Block::default()
.title(" SELECTED WORKLOAD / CORE ")
.borders(Borders::ALL);
let mut lines = Vec::new();
if let Some(process) = app.selected_process() {
let observed = process
.last_cpu
.and_then(|id| app.cpu.cpus.iter().find(|cpu| cpu.logical_id == id));
let observed_label = observed.map_or("unknown".into(), |cpu| {
let core_index = app
.cpu
.cores
.iter()
.position(|core| core.logical_cpus.contains(&cpu.logical_id));
format!(
"{} / T{}",
core_index.map_or_else(|| "C?".into(), |index| display_label(app, index)),
cpu.logical_id
)
});
let (p_threads, e_threads) = process
.threads
.iter()
.filter_map(|thread| {
thread
.last_cpu
.and_then(|id| app.cpu.cpus.iter().find(|cpu| cpu.logical_id == id))
})
.fold((0, 0), |(p, e), cpu| match cpu.core_kind {
CoreKind::Performance => (p + 1, e),
CoreKind::Efficiency | CoreKind::LowPowerEfficiency => (p, e + 1),
_ => (p, e),
});
lines.push(Line::from(format!(
"{} · PID {} · {:.0}% CPU · {} RSS · {} threads",
process.name,
process.pid,
process.cpu_percent.unwrap_or_default(),
format_bytes(process.rss_bytes),
process.thread_count
)));
lines.push(Line::from(format!(
"Last CPU: {} · observed-thread split: P {} / E {} · snapshot-derived",
observed_label, p_threads, e_threads
)));
} else {
lines.push(Line::from(
"No process selected · press p to browse processes",
));
}
if let Some(core) = app.cpu.cores.get(app.selected_core) {
lines.push(Line::from(format!(
"Core: {} · package {} · die {} · logical CPUs {}",
core.core_id.map_or("?".into(), |id| id.to_string()),
core.package_id.map_or("?".into(), |id| id.to_string()),
core.die_id.map_or("?".into(), |id| id.to_string()),
core.logical_cpus
.iter()
.map(|id| format!("CPU{id}"))
.collect::<Vec<_>>()
.join(",")
)));
let detail = core
.logical_cpus
.iter()
.filter_map(|id| app.cpu.cpus.iter().find(|cpu| cpu.logical_id == *id))
.map(|cpu| {
format!(
"T{} {} {}",
cpu.logical_id,
cpu.utilization.map_or("--".into(), |v| format!("{v:.0}%")),
cpu.detection.source.label()
)
})
.collect::<Vec<_>>()
.join(" · ");
lines.push(Line::from(detail));
}
if app.diagnostics {
let diagnostics = &app.cpu.diagnostics;
lines.push(Line::from(format!(
"Online logical CPUs: {} · Assigned: {} · Physical cores: {}",
diagnostics.online_logical_cpus,
diagnostics.assigned_logical_cpus,
diagnostics.physical_core_count
)));
lines.push(Line::from(format!(
"P-cores: {} · E-cores: {} · Unknown: {} · Detection: {}",
diagnostics.performance_core_count,
diagnostics.efficiency_core_count,
diagnostics.unknown_core_count,
app.cpu
.cpus
.first()
.map_or("unknown", |cpu| cpu.detection.source.label())
)));
lines.push(Line::from(format!(
"Missing assignments: {} · Duplicate assignments: {}",
diagnostics.missing_assignments, diagnostics.duplicate_assignments
)));
if diagnostics.has_mismatch() {
lines.push(Line::from(
"⚠ topology assignment mismatch: missing or duplicate logical CPUs",
));
}
}
frame.render_widget(
Paragraph::new(Text::from(lines))
.block(block)
.wrap(Wrap { trim: true }),
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)
}
}