use std::ops::Range;
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Text};
use tui_markdown::{Options, StyleSheet};
use crate::doc::{BlockKind, Document};
use crate::srcmap::{LineOffsets, align};
use crate::wrap::{Row, clip_line, wrap_line, wrap_table};
const BLOCK_GAP: usize = 1;
#[derive(Debug, Clone)]
struct Styles;
impl StyleSheet for Styles {
fn heading(&self, level: u8) -> Style {
match level {
1 => Style::new().fg(Color::Cyan).add_modifier(Modifier::BOLD | Modifier::UNDERLINED),
2 => Style::new().fg(Color::Cyan).add_modifier(Modifier::BOLD),
_ => Style::new().fg(Color::LightCyan).add_modifier(Modifier::BOLD),
}
}
fn heading_marker(&self, _level: u8) -> &'static str {
""
}
fn code(&self) -> Style {
Style::new().fg(Color::LightYellow)
}
fn link(&self) -> Style {
Style::new().fg(Color::Blue).add_modifier(Modifier::UNDERLINED)
}
fn blockquote(&self) -> Style {
Style::new().fg(Color::Green).add_modifier(Modifier::ITALIC)
}
}
#[derive(Debug)]
pub(crate) struct RenderedBlock {
text: Text<'static>,
offsets: Vec<LineOffsets>,
kind: BlockKind,
pub(crate) rows: Vec<Row>,
pub(crate) first_row: usize,
}
#[derive(Debug)]
pub(crate) struct DocLayout {
pub(crate) width: usize,
pub(crate) blocks: Vec<RenderedBlock>,
pub(crate) total_rows: usize,
}
fn render_block(doc: &Document, index: usize) -> (Text<'static>, Vec<LineOffsets>) {
let source = doc.block_text(index);
let text = own(tui_markdown::from_str_with_options(source, &Options::new(Styles)));
let plain: Vec<String> = text.lines.iter().map(ToString::to_string).collect();
let base = doc.blocks.get(index).map_or(0, |b| b.range.start);
let offsets = align(&plain, source, base);
(text, offsets)
}
fn own(text: Text<'_>) -> Text<'static> {
let lines = text
.lines
.into_iter()
.map(|line| {
let spans =
line.spans.into_iter().map(|s| ratatui::text::Span::styled(s.content.into_owned(), s.style));
Line::from(spans.collect::<Vec<_>>()).style(line.style)
})
.collect::<Vec<_>>();
Text::from(lines).style(text.style)
}
impl DocLayout {
pub(crate) fn build(doc: &Document, width: usize) -> Self {
let blocks = doc
.blocks
.iter()
.enumerate()
.map(|(i, block)| {
let (text, offsets) = render_block(doc, i);
RenderedBlock { text, offsets, kind: block.kind, rows: Vec::new(), first_row: 0 }
})
.collect();
let mut layout = Self { width: 0, blocks, total_rows: 0 };
layout.reflow(width);
layout
}
pub(crate) fn reflow(&mut self, width: usize) {
let width = width.max(1);
self.width = width;
let mut row = 0usize;
for block in &mut self.blocks {
block.first_row = row;
let lines = || block.text.lines.iter().zip(&block.offsets);
block.rows = if block.kind == BlockKind::Table {
wrap_table(&block.text.lines, &block.offsets, width)
} else if block.kind.preserves_columns() {
lines().map(|(l, o)| clip_line(l, o, width)).collect()
} else {
lines().flat_map(|(l, o)| wrap_line(l, o, width)).collect()
};
row += block.rows.len() + BLOCK_GAP;
}
self.total_rows = row.saturating_sub(BLOCK_GAP);
}
pub(crate) fn block_at_row(&self, row: usize) -> Option<usize> {
let idx = self.blocks.partition_point(|b| b.first_row <= row);
let i = idx.checked_sub(1)?;
let block = self.blocks.get(i)?;
(row < block.first_row + block.rows.len()).then_some(i)
}
pub(crate) fn row(&self, row: usize) -> Option<&Row> {
let block = self.blocks.get(self.block_at_row(row)?)?;
block.rows.get(row - block.first_row)
}
pub(crate) fn first_row_in_range(&self, block: usize, range: &Range<usize>) -> Option<usize> {
let b = self.blocks.get(block)?;
b.rows
.iter()
.position(|r| r.cells.iter().any(|c| c.is_some_and(|o| range.contains(&o))))
.map(|i| b.first_row + i)
}
pub(crate) fn rendered_in_range(&self, source: &str, range: &Range<usize>) -> String {
let mut out = String::new();
for block in &self.blocks {
let break_char = if block.kind.preserves_columns() { '\n' } else { ' ' };
let mut last_offset: Option<usize> = None;
let chars = block.text.lines.iter().zip(&block.offsets).flat_map(|(line, offsets)| {
line.spans.iter().flat_map(|s| s.content.chars()).zip(offsets.iter())
});
for (ch, offset) in chars {
let Some(offset) = offset.filter(|o| range.contains(o)) else { continue };
if let Some(prev) = last_offset
&& let Some(skipped) = source.get(prev..offset)
&& skipped.chars().any(char::is_whitespace)
&& !out.ends_with(char::is_whitespace)
{
out.push(break_char);
}
out.push(ch);
last_offset = Some(offset + ch.len_utf8());
}
}
out
}
}
#[cfg(test)]
#[allow(clippy::expect_used, reason = "tests assert by panicking")]
mod tests {
use super::*;
#[test]
fn rendered_text_strips_markup_and_joins_blocks_without_separator() {
let doc = Document::parse("Ship the **login page**\nby Friday.\n\nNext para.\n".to_owned());
let layout = DocLayout::build(&doc, 80);
let whole = 0..doc.source.len();
assert_eq!(layout.rendered_in_range(&doc.source, &whole), "Ship the login page by Friday.Next para.");
let bold = doc.source.find("**login").expect("present");
let bold_range = bold..bold + "**login page**".len();
assert_eq!(layout.rendered_in_range(&doc.source, &bold_range), "login page");
}
}