use crate::{
grid::config::{ColoredConfig, Entity, Offset, Position, SpannedConfig},
grid::records::{ExactRecords, Records},
settings::CellOption,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct LineChar {
c: char,
offset: Offset,
horizontal: bool,
}
impl LineChar {
pub fn horizontal(c: char, offset: impl Into<Offset>) -> Self {
let offset = offset.into();
let horizontal = true;
Self {
c,
offset,
horizontal,
}
}
pub fn vertical(c: char, offset: impl Into<Offset>) -> Self {
let offset = offset.into();
let horizontal = false;
Self {
c,
offset,
horizontal,
}
}
}
impl<R> CellOption<R, ColoredConfig> for LineChar
where
R: Records + ExactRecords,
{
fn change(self, records: &mut R, cfg: &mut ColoredConfig, entity: Entity) {
let cells = entity.iter(records.count_rows(), records.count_columns());
match self.horizontal {
true => add_char_horizontal(cfg, self.c, self.offset, cells),
false => add_char_vertical(cfg, self.c, self.offset, cells),
}
}
}
fn add_char_vertical<I: Iterator<Item = Position>>(
cfg: &mut SpannedConfig,
c: char,
offset: Offset,
cells: I,
) {
for pos in cells {
cfg.set_vertical_char(pos, offset, c);
}
}
fn add_char_horizontal<I: Iterator<Item = Position>>(
cfg: &mut SpannedConfig,
c: char,
offset: Offset,
cells: I,
) {
for pos in cells {
cfg.set_horizontal_char(pos, offset, c);
}
}