use crate::{
grid::{
config::{ColoredConfig, Entity},
records::{ExactRecords, IntoRecords, PeekableRecords, Records, RecordsMut},
},
settings::{
measurement::{Max, Measurement, Min},
CellOption, TableOption, Width,
},
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Justify<W> {
width: W,
}
impl<W> Justify<W>
where
W: Measurement<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<R, D, W> TableOption<R, ColoredConfig, D> for Justify<W>
where
W: Measurement<Width>,
R: Records + ExactRecords + PeekableRecords + RecordsMut<String>,
for<'a> &'a R: Records,
for<'a> <<&'a R as Records>::Iter as IntoRecords>::Cell: AsRef<str>,
{
fn change(self, records: &mut R, cfg: &mut ColoredConfig, _: &mut D) {
let width = self.width.measure(&*records, cfg);
let count_rows = records.count_rows();
let count_columns = records.count_columns();
for row in 0..count_rows {
for col in 0..count_columns {
let pos = Entity::Cell(row, col);
CellOption::change(Width::increase(width), records, cfg, pos);
CellOption::change(Width::truncate(width), records, cfg, pos);
}
}
}
}