pub mod renderer;
use std::cell::Cell;
use ratatui::text::{Span, Text};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MermaidBlockId(pub u64);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TableBlockId(pub u64);
pub type CellSpans = Vec<Span<'static>>;
#[derive(Debug, Clone)]
pub struct TableBlock {
pub id: TableBlockId,
pub headers: Vec<CellSpans>,
pub rows: Vec<Vec<CellSpans>>,
pub alignments: Vec<pulldown_cmark::Alignment>,
pub natural_widths: Vec<usize>,
pub rendered_height: u32,
}
#[derive(Debug)]
pub enum DocBlock {
Text(Text<'static>),
Mermaid {
id: MermaidBlockId,
source: String,
cell_height: Cell<u32>,
},
Table(TableBlock),
}
impl DocBlock {
pub fn height(&self) -> u32 {
match self {
DocBlock::Text(t) => t.lines.len() as u32,
DocBlock::Mermaid { cell_height, .. } => cell_height.get(),
DocBlock::Table(t) => t.rendered_height,
}
}
}
pub fn update_mermaid_heights(blocks: &[DocBlock], cache: &crate::mermaid::MermaidCache) {
for block in blocks {
if let DocBlock::Mermaid {
id,
source,
cell_height,
} = block
{
cell_height.set(cache.height(id, source));
}
}
}
pub fn cell_display_width(spans: &[Span<'static>]) -> usize {
spans
.iter()
.map(|s| unicode_width::UnicodeWidthStr::width(s.content.as_ref()))
.sum()
}
pub fn cell_to_string(spans: &[Span<'static>]) -> String {
spans.iter().map(|s| s.content.as_ref()).collect()
}