use ratatui::prelude::*;
pub struct Areas {
pub header: Rect,
pub cpu: Rect,
pub workload: Rect,
pub memory: Rect,
pub gpu: Rect,
pub ollama: Rect,
}
pub fn is_compact(area: Rect) -> bool {
area.width < 100 || area.height < 26
}
pub fn main(area: Rect, core_count: usize) -> Areas {
let compact = is_compact(area);
let cpu_height = required_cpu_height(area, core_count, compact);
let chunks = if compact {
Layout::vertical([
Constraint::Length(1),
Constraint::Length(cpu_height),
Constraint::Percentage(24),
Constraint::Min(8),
])
.split(area)
} else {
Layout::vertical([
Constraint::Length(1),
Constraint::Length(cpu_height),
Constraint::Percentage(20),
Constraint::Min(8),
])
.split(area)
};
if compact {
let middle = Layout::vertical([Constraint::Percentage(50), Constraint::Percentage(50)])
.split(chunks[2]);
let bottom = Layout::vertical([Constraint::Percentage(50), Constraint::Percentage(50)])
.split(chunks[3]);
Areas {
header: chunks[0],
cpu: chunks[1],
workload: middle[0],
memory: middle[1],
gpu: bottom[0],
ollama: bottom[1],
}
} else {
let middle = Layout::horizontal([Constraint::Percentage(58), Constraint::Percentage(42)])
.split(chunks[2]);
let bottom = Layout::horizontal([Constraint::Percentage(58), Constraint::Percentage(42)])
.split(chunks[3]);
Areas {
header: chunks[0],
cpu: chunks[1],
workload: middle[0],
memory: middle[1],
gpu: bottom[0],
ollama: bottom[1],
}
}
}
fn required_cpu_height(area: Rect, count: usize, compact: bool) -> u16 {
let width = area.width.saturating_sub(2).max(1) as usize;
if compact {
let columns = (width / 12).max(1);
return (count.max(1).div_ceil(columns) as u16 + 2).max(5);
}
let dimension = 5;
let columns = (width / (dimension * 2 + 2)).max(1);
let rows = count.max(1).div_ceil(columns);
(rows as u16 * (dimension as u16 + 3) + 2).max(8)
}
pub fn matrix_layout(area: Rect, count: usize) -> (usize, Vec<Rect>) {
for dimension in (3..=5).rev() {
let width = (dimension * 2 + 2) as u16;
let height = (dimension + 3) as u16;
let columns = (area.width / width.max(1)).max(1) as usize;
let rows = count.max(1).div_ceil(columns);
if rows * height as usize <= area.height as usize {
return (dimension, matrix_rects(area, count, width, height, columns));
}
}
let dimension = 3;
let width = 8;
let height = 6;
let columns = (area.width / width).max(1) as usize;
(dimension, matrix_rects(area, count, width, height, columns))
}
fn matrix_rects(area: Rect, count: usize, width: u16, height: u16, columns: usize) -> Vec<Rect> {
(0..count)
.map(|index| {
let column = index % columns;
let row = index / columns;
Rect::new(
area.x + column as u16 * width,
area.y + row as u16 * height,
width.min(area.width),
height.min(area.height.saturating_sub(row as u16 * height)),
)
})
.filter(|rect| rect.height > 0 && rect.y < area.bottom())
.collect()
}
pub fn centered(area: Rect, width: u16, height: u16) -> Rect {
let width = width.min(area.width.saturating_sub(2));
let height = height.min(area.height.saturating_sub(2));
Rect {
x: area.x + area.width.saturating_sub(width) / 2,
y: area.y + area.height.saturating_sub(height) / 2,
width,
height,
}
}
#[cfg(test)]
mod tests {
use super::matrix_layout;
use ratatui::prelude::Rect;
#[test]
fn chooses_dense_matrix_dimension() {
let (dimension, rects) = matrix_layout(Rect::new(0, 0, 60, 20), 8);
assert_eq!(dimension, 5);
assert_eq!(rects.len(), 8);
assert!(
rects
.iter()
.all(|rect| rect.right() <= 60 && rect.bottom() <= 20)
);
}
#[test]
fn falls_back_to_small_matrix_when_tight() {
let (dimension, rects) = matrix_layout(Rect::new(0, 0, 24, 8), 4);
assert_eq!(dimension, 3);
assert_eq!(rects.len(), 4);
}
}