use ratatui::layout::Constraint;
const MARKER_WIDTH: u16 = 2;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Column {
Name,
Kind,
Bar,
Level,
State,
Trend,
LastSeen,
}
impl Column {
pub const ALL: [Column; 7] = [
Column::Name,
Column::Kind,
Column::Bar,
Column::Level,
Column::State,
Column::Trend,
Column::LastSeen,
];
const GIVEN_UP: [Column; 5] = [
Column::LastSeen,
Column::Trend,
Column::Kind,
Column::Bar,
Column::State,
];
pub fn header(self) -> &'static str {
match self {
Column::Name => "Device",
Column::Kind => "Type",
Column::Bar => "Battery",
Column::Level => "%",
Column::State => "State",
Column::Trend => "Trend",
Column::LastSeen => "Last seen",
}
}
pub fn width(self) -> u16 {
match self {
Column::Name => 26,
Column::Kind | Column::Bar => 12,
Column::Level => 4,
Column::State => 11,
Column::Trend => 6,
Column::LastSeen => 9,
}
}
pub fn constraint(self) -> Constraint {
match self {
Column::Name => Constraint::Max(self.width()),
other => Constraint::Length(other.width()),
}
}
fn floor(self) -> u16 {
match self {
Column::Name => 14,
other => other.width(),
}
}
}
pub fn fitting(width: u16) -> Vec<Column> {
Column::GIVEN_UP
.iter()
.fold(Column::ALL.to_vec(), |columns, given_up| {
if needed(&columns) <= width {
columns
} else {
columns.into_iter().filter(|c| c != given_up).collect()
}
})
}
fn needed(columns: &[Column]) -> u16 {
let cells: u16 = columns.iter().map(|column| column.floor()).sum();
let gaps = u16::try_from(columns.len().saturating_sub(1)).unwrap_or_default();
cells + gaps + MARKER_WIDTH
}
#[cfg(test)]
mod tests {
use super::super::theme;
use super::*;
#[test]
fn each_width_keeps_the_columns_the_drop_order_leaves_it() {
use Column::{Bar, Kind, Level, Name, State, Trend};
let table: [(u16, &[Column]); 8] = [
(200, &Column::ALL),
(76, &Column::ALL),
(75, &[Name, Kind, Bar, Level, State, Trend]),
(65, &[Name, Kind, Bar, Level, State]),
(58, &[Name, Bar, Level, State]),
(45, &[Name, Level, State]),
(32, &[Name, Level]),
(0, &[Name, Level]),
];
for (width, columns) in table {
assert_eq!(fitting(width).as_slice(), columns, "at {width}");
}
}
#[test]
fn the_name_and_the_level_are_never_given_up() {
for width in 0..=120 {
let columns = fitting(width);
assert!(columns.contains(&Column::Name), "at {width}");
assert!(columns.contains(&Column::Level), "at {width}");
}
}
#[test]
fn a_wider_terminal_never_shows_fewer_columns() {
let counts: Vec<usize> = (0..=120).map(|width| fitting(width).len()).collect();
assert!(
counts.windows(2).all(|pair| pair[0] <= pair[1]),
"{counts:?}"
);
}
#[test]
fn what_is_kept_fits_unless_nothing_can() {
for width in 20..=120 {
let columns = fitting(width);
assert!(
needed(&columns) <= width || columns.len() == 2,
"{columns:?} do not fit in {width}"
);
}
}
#[test]
fn every_column_keeps_its_header_and_its_room() {
for column in Column::ALL {
assert!(!column.header().is_empty());
assert!(column.floor() <= column.width());
assert!(
column.header().len() <= usize::from(column.width()),
"{column:?} cannot print its own header"
);
}
assert!(usize::from(Column::Trend.width()) >= theme::SPARK_WIDTH);
}
}