use crate::model::gpu::{GpuKind, GpuSnapshot};
use ratatui::{
prelude::*,
widgets::{Block, Borders, Paragraph, Wrap},
};
pub fn render(
frame: &mut Frame<'_>,
area: Rect,
gpu: &GpuSnapshot,
dimension: usize,
selected: bool,
) {
let integrated = gpu.device.kind == GpuKind::Integrated;
let util = gpu.utilization_percent.unwrap_or_default() as f64;
let style = if integrated {
Style::default().fg(Color::Cyan)
} else {
Style::default().fg(Color::Green)
};
let glyph = if util < 25.0 {
'▪'
} else if util < 50.0 {
'▒'
} else if util < 75.0 {
'▓'
} else if integrated {
'▒'
} else {
'█'
};
let fill = gpu.utilization_percent.map_or(0, |value| {
(value as f64 / 100.0 * (dimension * dimension) as f64).round() as usize
});
let label = match gpu.device.kind {
GpuKind::Integrated => "iGPU",
GpuKind::Discrete => "dGPU",
GpuKind::Unknown => "GPU",
};
let utilization = gpu
.utilization_percent
.map_or_else(|| "--".into(), |value| format!("{value:02}%"));
let mut lines = vec![Line::from(format!("{label}{} {utilization}", gpu.index))];
for row in 0..dimension {
let mut spans = Vec::new();
for column in 0..dimension {
let index = row * dimension + column;
spans.push(Span::styled(
format!("{} ", if index < fill { glyph } else { '░' }),
if index < fill {
style
} else {
Style::default()
},
));
}
lines.push(Line::from(spans));
}
lines.push(Line::from(format!(
"{} · {} · {}°C · {}",
gpu.name.chars().take(14).collect::<String>(),
gpu.device
.driver
.as_deref()
.map_or("driver --", |driver| driver),
gpu.temperature_celsius
.map_or("--".into(), |v| v.to_string()),
power(gpu)
)));
let block = Block::default()
.title(format!(" {} ", gpu.device.kind.label()))
.borders(Borders::ALL)
.border_style(if selected {
style.add_modifier(Modifier::BOLD)
} else {
style
});
frame.render_widget(
Paragraph::new(Text::from(lines))
.block(block)
.wrap(Wrap { trim: true }),
area,
);
}
fn power(gpu: &GpuSnapshot) -> String {
match (gpu.power_draw_milliwatts, gpu.power_limit_milliwatts) {
(Some(draw), Some(limit)) => {
format!("{:.0}/{:.0}W", draw as f64 / 1000.0, limit as f64 / 1000.0)
}
_ => "power --".into(),
}
}