use crate::data::cpu::CpuSnapshot;
use crate::utils::fast_int_sqrt;
use crate::widgets::percentage_bar::percentage_bar;
use ratatui::style::{Color, Style};
use ratatui::{
buffer::Buffer,
layout::Rect,
text::{Line, Span, Text},
widgets::{Paragraph, Widget},
};
pub struct CpuWidget<'a> {
pub data: &'a CpuSnapshot,
}
impl<'a> CpuWidget<'a> {
pub fn grid_dimensions(&self) -> (u16, u16) {
let cores = self.data.cores.len();
if cores <= 3 {
return (cores as u16, 1);
}
let cpu_rows = fast_int_sqrt(cores) as u16;
let mut cpu_cols: u16 = 0;
while ((cpu_cols * cpu_rows) as usize) < cores {
cpu_cols += 1;
}
(cpu_rows, cpu_cols)
}
pub fn grid_height(&self) -> u16 {
self.grid_dimensions().0
}
}
impl<'a> Widget for CpuWidget<'a> {
fn render(self, area: Rect, buf: &mut Buffer) {
let (cpu_rows, cpu_cols) = self.grid_dimensions();
let mut spans = vec![Span::styled(" Total ", Style::default().fg(Color::Cyan))];
let usage = self.data.usage;
let text = format!("{:.1}%", usage);
let total_bar = percentage_bar(area.width - 16, usage, &text);
spans.extend(total_bar);
let cores_len = self.data.cores.len();
let mut lines = vec![Line::from(spans).left_aligned()];
let total_width = area.width - 6;
let core_width: u16 = total_width / cpu_cols;
for r in 0..cpu_rows {
let mut spans = vec![Span::raw(" ")];
let mut widths = vec![core_width; cpu_cols as usize];
let remain: u16 = total_width - widths.iter().sum::<u16>();
for width in widths.iter_mut().take(remain as usize) {
*width += 1;
}
'inner: for c in 0..cpu_cols {
let i = (c * cpu_rows + r) as usize;
if i >= cores_len {
break 'inner;
}
spans.push(Span::styled(
format!(" {:>2}", i),
Style::default().fg(Color::Cyan),
));
let width = widths[c as usize].saturating_sub(7);
let usage = self.data.cores[i].usage;
let text = format!("{:.1}%{:>3.0}°C", usage, self.data.cores[i].temp);
let bar = percentage_bar(width, usage, &text);
spans.extend(bar);
}
lines.push(Line::from(spans));
}
let content = Text::from(lines);
Paragraph::new(content).left_aligned().render(area, buf);
}
}
#[cfg(test)]
mod tests {
use super::CpuSnapshot;
use super::CpuWidget;
use crate::data::cpu::CoreSnapshot;
fn cpu_snap(cores: usize) -> CpuSnapshot {
CpuSnapshot {
usage: 0.0,
cores: vec![
CoreSnapshot {
usage: 0.0,
temp: 0.0
};
cores
],
}
}
#[test]
fn test_grid_dimensions() {
let widget = CpuWidget { data: &cpu_snap(1) };
assert_eq!(widget.grid_dimensions(), (1, 1));
let widget = CpuWidget { data: &cpu_snap(2) };
assert_eq!(widget.grid_dimensions(), (2, 1));
let widget = CpuWidget { data: &cpu_snap(3) };
assert_eq!(widget.grid_dimensions(), (3, 1));
let widget = CpuWidget { data: &cpu_snap(4) };
assert_eq!(widget.grid_dimensions(), (2, 2));
let widget = CpuWidget { data: &cpu_snap(5) };
assert_eq!(widget.grid_dimensions(), (2, 3));
let widget = CpuWidget {
data: &cpu_snap(12),
};
assert_eq!(widget.grid_dimensions(), (3, 4));
let widget = CpuWidget {
data: &cpu_snap(16),
};
assert_eq!(widget.grid_dimensions(), (4, 4));
let widget = CpuWidget {
data: &cpu_snap(32),
};
assert_eq!(widget.grid_dimensions(), (5, 7));
}
}