use crate::document::{MIN_WIDTH, Settings, Style};
use crate::theme::Palette;
use crate::{code, mermaid};
const MIN_NESTED_WIDTH: usize = 12;
const MAX_NESTING: usize = 32;
#[derive(Clone, Copy)]
pub(super) struct Context<'a> {
pub settings: &'a Settings,
width: usize,
pub style: Style,
pub nesting: usize,
pub list_depth: usize,
pub diagrams: &'a mermaid::Allowance,
pub highlights: &'a code::Allowance,
}
pub(super) enum Nested<'a> {
Indented(Context<'a>),
Unindented(Context<'a>),
TooDeep,
}
impl<'a> Context<'a> {
pub fn new(settings: &'a Settings, diagrams: &'a mermaid::Allowance, highlights: &'a code::Allowance) -> Self {
let width = settings.width.max(MIN_WIDTH);
Self { settings, width, style: Style::fg(settings.theme.palette.text), nesting: 0, list_depth: 0, diagrams, highlights }
}
pub fn palette(&self) -> &Palette {
&self.settings.theme.palette
}
pub fn width(&self) -> usize {
self.width
}
pub fn settings_at_width(&self) -> Settings {
Settings { width: self.width, ..self.settings.clone() }
}
pub fn nested(&self, columns: usize) -> Nested<'a> {
if self.nesting >= MAX_NESTING {
return Nested::TooDeep;
}
let deeper = Self { nesting: self.nesting + 1, ..*self };
match self.width().checked_sub(columns).filter(|width| *width >= MIN_NESTED_WIDTH) {
Some(width) => Nested::Indented(Self { width, ..deeper }),
None => Nested::Unindented(deeper),
}
}
pub fn with_style(self, style: Style) -> Self {
Self { style, ..self }
}
pub fn in_list(self) -> Self {
Self { list_depth: self.list_depth + 1, ..self }
}
}