mod borders_config;
mod entity_map;
use std::collections::HashMap;
use crate::ansi::{ANSIBuf, ANSIStr};
use crate::config::compact::CompactConfig;
use crate::config::{
AlignmentHorizontal, AlignmentVertical, Border, Borders, Entity, Indent, Offset, Position,
Sides,
};
use borders_config::BordersConfig;
pub use self::entity_map::EntityMap;
use super::Formatting;
type HorizontalLine = super::HorizontalLine<char>;
type VerticalLine = super::VerticalLine<char>;
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct SpannedConfig {
margin: Sides<MarginIndent>,
padding: EntityMap<Sides<Indent>>,
padding_color: EntityMap<Sides<Option<ANSIBuf>>>,
alignment_h: EntityMap<AlignmentHorizontal>,
alignment_v: EntityMap<AlignmentVertical>,
formatting_trim_h: EntityMap<bool>,
formatting_trim_v: EntityMap<bool>,
formatting_line_alignment: EntityMap<bool>,
span_columns: HashMap<Position, usize>,
span_rows: HashMap<Position, usize>,
borders: BordersConfig<char>,
borders_colors: BordersConfig<ANSIBuf>,
borders_missing_char: char,
horizontal_chars: HashMap<Position, HashMap<Offset, char>>,
horizontal_colors: HashMap<Position, HashMap<Offset, ANSIBuf>>, vertical_chars: HashMap<Position, HashMap<Offset, char>>,
vertical_colors: HashMap<Position, HashMap<Offset, ANSIBuf>>,
justification: EntityMap<char>,
justification_color: EntityMap<Option<ANSIBuf>>,
}
impl Default for SpannedConfig {
fn default() -> Self {
Self {
margin: Sides::default(),
padding: EntityMap::default(),
padding_color: EntityMap::default(),
formatting_trim_h: EntityMap::default(),
formatting_trim_v: EntityMap::default(),
formatting_line_alignment: EntityMap::default(),
alignment_h: EntityMap::new(AlignmentHorizontal::Left),
alignment_v: EntityMap::new(AlignmentVertical::Top),
span_columns: HashMap::default(),
span_rows: HashMap::default(),
borders: BordersConfig::default(),
borders_colors: BordersConfig::default(),
borders_missing_char: ' ',
horizontal_chars: HashMap::default(),
horizontal_colors: HashMap::default(),
vertical_chars: HashMap::default(),
vertical_colors: HashMap::default(),
justification: EntityMap::new(' '),
justification_color: EntityMap::default(),
}
}
}
impl SpannedConfig {
pub fn new() -> Self {
Self::default()
}
pub fn set_margin(&mut self, margin: Sides<Indent>) {
self.margin.left.indent = margin.left;
self.margin.right.indent = margin.right;
self.margin.top.indent = margin.top;
self.margin.bottom.indent = margin.bottom;
}
pub fn set_margin_color(&mut self, margin: Sides<Option<ANSIBuf>>) {
self.margin.left.color = margin.left;
self.margin.right.color = margin.right;
self.margin.top.color = margin.top;
self.margin.bottom.color = margin.bottom;
}
pub fn set_margin_offset(&mut self, margin: Sides<Offset>) {
self.margin.left.offset = margin.left;
self.margin.right.offset = margin.right;
self.margin.top.offset = margin.top;
self.margin.bottom.offset = margin.bottom;
}
pub fn get_margin(&self) -> Sides<Indent> {
Sides::new(
self.margin.left.indent,
self.margin.right.indent,
self.margin.top.indent,
self.margin.bottom.indent,
)
}
pub fn get_margin_color(&self) -> Sides<Option<&ANSIBuf>> {
Sides::new(
self.margin.left.color.as_ref(),
self.margin.right.color.as_ref(),
self.margin.top.color.as_ref(),
self.margin.bottom.color.as_ref(),
)
}
pub fn get_margin_offset(&self) -> Sides<Offset> {
Sides::new(
self.margin.left.offset,
self.margin.right.offset,
self.margin.top.offset,
self.margin.bottom.offset,
)
}
pub fn remove_borders(&mut self) {
self.borders = BordersConfig::default();
}
pub fn remove_borders_colors(&mut self) {
self.borders_colors = BordersConfig::default();
}
pub fn remove_color_line_horizontal(&mut self) {
self.horizontal_colors.clear();
}
pub fn remove_color_line_vertical(&mut self) {
self.vertical_colors.clear();
}
pub fn remove_horizontal_chars(&mut self) {
self.horizontal_chars.clear();
}
pub fn remove_vertical_chars(&mut self) {
self.vertical_chars.clear();
}
pub fn set_borders(&mut self, borders: Borders<char>) {
self.borders.set_borders(borders);
}
pub fn get_border_default(&self) -> Option<&char> {
self.borders.get_global()
}
pub fn set_border_default(&mut self, c: char) {
self.borders.set_global(c);
}
pub fn get_borders(&self) -> &Borders<char> {
self.borders.get_borders()
}
pub fn insert_horizontal_line(&mut self, line: usize, val: HorizontalLine) {
self.borders.insert_horizontal_line(line, val);
}
pub fn remove_horizontal_line(&mut self, line: usize, count_rows: usize) {
self.borders.remove_horizontal_line(line, count_rows);
}
pub fn get_vertical_line(&self, line: usize) -> Option<&VerticalLine> {
self.borders.get_vertical_line(line)
}
pub fn get_vertical_lines(&self) -> HashMap<usize, VerticalLine> {
self.borders.get_vertical_lines()
}
pub fn insert_vertical_line(&mut self, line: usize, val: VerticalLine) {
self.borders.insert_vertical_line(line, val);
}
pub fn remove_vertical_line(&mut self, line: usize, count_columns: usize) {
self.borders.remove_vertical_line(line, count_columns);
}
pub fn get_horizontal_line(&self, line: usize) -> Option<&HorizontalLine> {
self.borders.get_horizontal_line(line)
}
pub fn get_horizontal_lines(&self) -> HashMap<usize, HorizontalLine> {
self.borders.get_horizontal_lines()
}
pub fn set_horizontal_char(&mut self, pos: Position, offset: Offset, c: char) {
let chars = self
.horizontal_chars
.entry(pos)
.or_insert_with(|| HashMap::with_capacity(1));
chars.insert(offset, c);
}
pub fn lookup_horizontal_char(&self, pos: Position, offset: usize, end: usize) -> Option<char> {
self.horizontal_chars
.get(&pos)
.and_then(|chars| {
chars.get(&Offset::Start(offset)).or_else(|| {
if end > offset {
if end == 0 {
chars.get(&Offset::End(0))
} else {
chars.get(&Offset::End(end - offset - 1))
}
} else {
None
}
})
})
.copied()
}
pub fn is_overridden_horizontal(&self, pos: Position) -> bool {
self.horizontal_chars.contains_key(&pos)
}
pub fn remove_overridden_horizontal(&mut self, pos: Position) {
self.horizontal_chars.remove(&pos);
}
pub fn set_vertical_char(&mut self, pos: Position, offset: Offset, c: char) {
let chars = self
.vertical_chars
.entry(pos)
.or_insert_with(|| HashMap::with_capacity(1));
chars.insert(offset, c);
}
pub fn lookup_vertical_char(&self, pos: Position, offset: usize, end: usize) -> Option<char> {
self.vertical_chars
.get(&pos)
.and_then(|chars| {
chars.get(&Offset::Start(offset)).or_else(|| {
if end > offset {
if end == 0 {
chars.get(&Offset::End(0))
} else {
chars.get(&Offset::End(end - offset - 1))
}
} else {
None
}
})
})
.copied()
}
pub fn is_overridden_vertical(&self, pos: Position) -> bool {
self.vertical_chars.contains_key(&pos)
}
pub fn remove_overridden_vertical(&mut self, pos: Position) {
self.vertical_chars.remove(&pos);
}
pub fn set_horizontal_char_color(&mut self, pos: Position, offset: Offset, c: ANSIBuf) {
let chars = self
.horizontal_colors
.entry(pos)
.or_insert_with(|| HashMap::with_capacity(1));
chars.insert(offset, c);
}
pub fn lookup_horizontal_color(
&self,
pos: Position,
offset: usize,
end: usize,
) -> Option<&ANSIBuf> {
self.horizontal_colors.get(&pos).and_then(|chars| {
chars.get(&Offset::Start(offset)).or_else(|| {
if end > offset {
if end == 0 {
chars.get(&Offset::End(0))
} else {
chars.get(&Offset::End(end - offset - 1))
}
} else {
None
}
})
})
}
pub fn set_vertical_char_color(&mut self, pos: Position, offset: Offset, c: ANSIBuf) {
let chars = self
.vertical_colors
.entry(pos)
.or_insert_with(|| HashMap::with_capacity(1));
chars.insert(offset, c);
}
pub fn lookup_vertical_color(
&self,
pos: Position,
offset: usize,
end: usize,
) -> Option<&ANSIBuf> {
self.vertical_colors.get(&pos).and_then(|chars| {
chars.get(&Offset::Start(offset)).or_else(|| {
if end > offset {
if end == 0 {
chars.get(&Offset::End(0))
} else {
chars.get(&Offset::End(end - offset - 1))
}
} else {
None
}
})
})
}
pub fn set_padding(&mut self, entity: Entity, padding: Sides<Indent>) {
self.padding.insert(entity, padding);
}
pub fn set_padding_color(&mut self, entity: Entity, padding: Sides<Option<ANSIBuf>>) {
self.padding_color.insert(entity, padding);
}
pub fn get_padding(&self, pos: Position) -> &Sides<Indent> {
self.padding.get(pos)
}
pub fn get_padding_color(&self, pos: Position) -> &Sides<Option<ANSIBuf>> {
self.padding_color.get(pos)
}
pub fn set_trim_horizontal(&mut self, entity: Entity, on: bool) {
self.formatting_trim_h.insert(entity, on);
}
pub fn get_trim_horizonal(&self, pos: Position) -> bool {
*self.formatting_trim_h.get(pos)
}
pub fn set_trim_vertical(&mut self, entity: Entity, on: bool) {
self.formatting_trim_v.insert(entity, on);
}
pub fn get_trim_vertical(&self, pos: Position) -> bool {
*self.formatting_trim_v.get(pos)
}
pub fn set_line_alignment(&mut self, entity: Entity, on: bool) {
self.formatting_line_alignment.insert(entity, on);
}
pub fn get_line_alignment(&self, pos: Position) -> bool {
*self.formatting_line_alignment.get(pos)
}
pub fn get_formatting(&self, pos: Position) -> Formatting {
Formatting::new(
*self.formatting_trim_h.get(pos),
*self.formatting_trim_v.get(pos),
*self.formatting_line_alignment.get(pos),
)
}
pub fn set_alignment_vertical(&mut self, entity: Entity, alignment: AlignmentVertical) {
self.alignment_v.insert(entity, alignment);
}
pub fn get_alignment_vertical(&self, pos: Position) -> &AlignmentVertical {
self.alignment_v.get(pos)
}
pub fn set_alignment_horizontal(&mut self, entity: Entity, alignment: AlignmentHorizontal) {
self.alignment_h.insert(entity, alignment);
}
pub fn get_alignment_horizontal(&self, pos: Position) -> &AlignmentHorizontal {
self.alignment_h.get(pos)
}
pub fn set_border(&mut self, pos: Position, border: Border<char>) {
self.borders.insert_border(pos, border);
}
pub fn get_border(&self, pos: Position, shape: (usize, usize)) -> Border<char> {
self.borders.get_border(pos, shape).copied()
}
pub fn get_border_color(&self, pos: Position, shape: (usize, usize)) -> Border<&ANSIBuf> {
self.borders_colors.get_border(pos, shape)
}
pub fn set_borders_missing(&mut self, c: char) {
self.borders_missing_char = c;
}
pub fn get_borders_missing(&self) -> char {
self.borders_missing_char
}
pub fn get_border_color_default(&self) -> Option<&ANSIBuf> {
self.borders_colors.get_global()
}
pub fn set_border_color_default(&mut self, clr: ANSIBuf) {
self.borders_colors = BordersConfig::default();
self.borders_colors.set_global(clr);
}
pub fn get_color_borders(&self) -> &Borders<ANSIBuf> {
self.borders_colors.get_borders()
}
pub fn set_borders_color(&mut self, clrs: Borders<ANSIBuf>) {
self.borders_colors.set_borders(clrs);
}
pub fn set_border_color(&mut self, pos: Position, border: Border<ANSIBuf>) {
self.borders_colors.insert_border(pos, border)
}
pub fn remove_border(&mut self, pos: Position, shape: (usize, usize)) {
self.borders.remove_border(pos, shape);
}
pub fn remove_border_color(&mut self, pos: Position, shape: (usize, usize)) {
self.borders_colors.remove_border(pos, shape);
}
pub fn get_justification(&self, pos: Position) -> char {
*self.justification.get(pos)
}
pub fn get_justification_color(&self, pos: Position) -> Option<&ANSIBuf> {
self.justification_color.get(pos).as_ref()
}
pub fn set_justification(&mut self, entity: Entity, c: char) {
self.justification.insert(entity, c);
}
pub fn set_justification_color(&mut self, entity: Entity, color: Option<ANSIBuf>) {
self.justification_color.insert(entity, color);
}
pub fn get_column_spans(&self) -> HashMap<Position, usize> {
self.span_columns.clone()
}
pub fn get_row_spans(&self) -> HashMap<Position, usize> {
self.span_rows.clone()
}
pub fn get_column_span(&self, pos: Position) -> Option<usize> {
self.span_columns.get(&pos).copied()
}
pub fn get_row_span(&self, pos: Position) -> Option<usize> {
self.span_rows.get(&pos).copied()
}
pub fn remove_column_spans(&mut self) {
self.span_columns.clear()
}
pub fn remove_row_spans(&mut self) {
self.span_rows.clear()
}
pub fn set_column_span(&mut self, pos: Position, span: usize) {
set_cell_column_span(self, pos, span);
}
pub fn has_column_spans(&self) -> bool {
!self.span_columns.is_empty()
}
pub fn set_row_span(&mut self, pos: Position, span: usize) {
set_cell_row_span(self, pos, span);
}
pub fn has_row_spans(&self) -> bool {
!self.span_rows.is_empty()
}
pub fn has_border_colors(&self) -> bool {
!self.borders_colors.is_empty()
}
pub fn has_offset_chars(&self) -> bool {
!self.horizontal_chars.is_empty() || !self.vertical_chars.is_empty()
}
pub fn has_justification(&self) -> bool {
!self.justification.is_empty()
|| !self.justification_color.is_empty()
|| self.justification_color.as_ref().is_some()
}
pub fn has_padding(&self) -> bool {
!self.padding.is_empty()
}
pub fn has_padding_color(&self) -> bool {
if !self.padding_color.is_empty() {
let map = HashMap::from(self.padding_color.clone());
for (entity, value) in map {
if matches!(entity, Entity::Global) {
continue;
}
if !value.is_empty() {
return true;
}
}
}
!self.padding_color.as_ref().is_empty()
}
pub fn has_formatting(&self) -> bool {
!self.formatting_trim_h.is_empty()
|| !self.formatting_trim_v.is_empty()
|| !self.formatting_line_alignment.is_empty()
}
pub fn has_alignment_vertical(&self) -> bool {
!self.alignment_v.is_empty()
}
pub fn has_alignment_horizontal(&self) -> bool {
!self.alignment_h.is_empty()
}
pub fn get_intersection(&self, pos: Position, shape: (usize, usize)) -> Option<char> {
let c = self.borders.get_intersection(pos, shape);
if let Some(c) = c {
return Some(*c);
}
if self.has_horizontal(pos.row, shape.0) && self.has_vertical(pos.col, shape.1) {
return Some(self.get_borders_missing());
}
None
}
pub fn get_horizontal(&self, pos: Position, count_rows: usize) -> Option<char> {
let c = self.borders.get_horizontal(pos, count_rows);
if let Some(c) = c {
return Some(*c);
}
if self.has_horizontal(pos.row, count_rows) {
return Some(self.get_borders_missing());
}
None
}
pub fn get_vertical(&self, pos: Position, count_columns: usize) -> Option<char> {
if let Some(c) = self.borders.get_vertical(pos, count_columns) {
return Some(*c);
}
if self.has_vertical(pos.col, count_columns) {
return Some(self.get_borders_missing());
}
None
}
pub fn get_horizontal_color(&self, pos: Position, count_rows: usize) -> Option<&ANSIBuf> {
self.borders_colors.get_horizontal(pos, count_rows)
}
pub fn get_vertical_color(&self, pos: Position, count_columns: usize) -> Option<&ANSIBuf> {
self.borders_colors.get_vertical(pos, count_columns)
}
pub fn get_intersection_color(&self, pos: Position, shape: (usize, usize)) -> Option<&ANSIBuf> {
self.borders_colors.get_intersection(pos, shape)
}
pub fn has_horizontal(&self, row: usize, count_rows: usize) -> bool {
self.borders.has_horizontal(row, count_rows)
}
pub fn has_vertical(&self, col: usize, count_columns: usize) -> bool {
self.borders.has_vertical(col, count_columns)
}
pub fn count_horizontal(&self, count_rows: usize) -> usize {
(0..=count_rows)
.filter(|&row| self.has_horizontal(row, count_rows))
.count()
}
pub fn count_vertical(&self, count_columns: usize) -> usize {
(0..=count_columns)
.filter(|&col| self.has_vertical(col, count_columns))
.count()
}
pub fn is_cell_visible(&self, pos: Position) -> bool {
!(self.is_cell_covered_by_column_span(pos)
|| self.is_cell_covered_by_row_span(pos)
|| self.is_cell_covered_by_both_spans(pos))
}
pub fn is_cell_covered_by_row_span(&self, pos: Position) -> bool {
is_cell_covered_by_row_span(self, pos)
}
pub fn is_cell_covered_by_column_span(&self, pos: Position) -> bool {
is_cell_covered_by_column_span(self, pos)
}
pub fn is_cell_covered_by_both_spans(&self, pos: Position) -> bool {
is_cell_covered_by_both_spans(self, pos)
}
}
impl From<CompactConfig> for SpannedConfig {
fn from(compact: CompactConfig) -> Self {
use Entity::Global;
let mut cfg = Self::default();
cfg.set_padding(Global, *compact.get_padding());
cfg.set_padding_color(Global, to_ansi_color(*compact.get_padding_color()));
cfg.set_margin(*compact.get_margin());
cfg.set_margin_color(to_ansi_color(*compact.get_margin_color()));
cfg.set_alignment_horizontal(Global, compact.get_alignment_horizontal());
cfg.set_borders(*compact.get_borders());
cfg.set_borders_color(compact.get_borders_color().convert_into());
cfg
}
}
fn to_ansi_color(b: Sides<ANSIStr<'_>>) -> Sides<Option<ANSIBuf>> {
Sides::new(
Some(b.left.into()),
Some(b.right.into()),
Some(b.top.into()),
Some(b.bottom.into()),
)
}
fn set_cell_row_span(cfg: &mut SpannedConfig, pos: Position, span: usize) {
if span == 0 {
return;
}
if span == 1 {
cfg.span_rows.remove(&pos);
return;
}
cfg.span_rows.insert(pos, span);
}
fn set_cell_column_span(cfg: &mut SpannedConfig, pos: Position, span: usize) {
if span == 0 {
return;
}
if span == 1 {
cfg.span_columns.remove(&pos);
return;
}
cfg.span_columns.insert(pos, span);
}
fn is_cell_covered_by_column_span(cfg: &SpannedConfig, pos: Position) -> bool {
cfg.span_columns
.iter()
.any(|(p, span)| p.row == pos.row && pos.col > p.col && pos.col < p.col + span)
}
fn is_cell_covered_by_row_span(cfg: &SpannedConfig, pos: Position) -> bool {
cfg.span_rows
.iter()
.any(|(p, span)| p.col == pos.col && pos.row > p.row && pos.row < p.row + span)
}
fn is_cell_covered_by_both_spans(cfg: &SpannedConfig, pos: Position) -> bool {
if !cfg.has_column_spans() || !cfg.has_row_spans() {
return false;
}
cfg.span_rows.iter().any(|(p1, row_span)| {
cfg.span_columns
.iter()
.filter(|(p2, _)| &p1 == p2)
.any(|(_, col_span)| {
pos.row > p1.row
&& pos.row < p1.row + row_span
&& pos.col > p1.col
&& pos.col < p1.col + col_span
})
})
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
struct MarginIndent {
indent: Indent,
offset: Offset,
color: Option<ANSIBuf>,
}
impl Default for MarginIndent {
fn default() -> Self {
Self {
indent: Indent::default(),
offset: Offset::Start(0),
color: None,
}
}
}