use papergrid::records::{Records, RecordsMut};
use crate::{
measurment::{Max, Measurment, Min},
CellOption, Table, TableOption, Width,
};
#[derive(Debug)]
pub struct Justify<W> {
width: W,
}
impl<W> Justify<W>
where
W: Measurment<Width>,
{
pub fn new(width: W) -> Self {
Self { width }
}
}
impl Justify<Max> {
pub fn max() -> Self {
Self { width: Max }
}
}
impl Justify<Min> {
pub fn min() -> Self {
Self { width: Min }
}
}
impl<W, R> TableOption<R> for Justify<W>
where
W: Measurment<Width>,
R: Records + RecordsMut<String>,
{
fn change(&mut self, table: &mut Table<R>) {
let width = self.width.measure(table.get_records(), table.get_config());
let (count_rows, count_cols) = table.shape();
for row in 0..count_rows {
for col in 0..count_cols {
let pos = (row, col).into();
Width::increase(width).change_cell(table, pos);
Width::truncate(width).change_cell(table, pos);
}
}
table.destroy_width_cache();
table.destroy_height_cache();
}
}