use ratatui::{buffer::Buffer, layout::Rect};
use crate::widgets::markdown_widget::extensions::toc::Toc;
const CORNER_TOP_LEFT: char = '\u{256D}';
const CORNER_TOP_RIGHT: char = '\u{256E}';
const CORNER_BOTTOM_LEFT: char = '\u{2570}';
const CORNER_BOTTOM_RIGHT: char = '\u{256F}';
const HORIZONTAL: char = '\u{2500}';
const VERTICAL: char = '\u{2502}';
impl<'a> Toc<'a> {
pub(crate) fn render_border(&self, area: Rect, buf: &mut Buffer) -> Rect {
if area.width < 4 || area.height < 3 {
return area;
}
let border_style = self.config.border_style;
let title_style = self.config.title_style;
let bg_style = self.config.background_style;
buf.cell_mut((area.x, area.y))
.map(|cell| cell.set_char(CORNER_TOP_LEFT).set_style(border_style));
if self.expanded {
let title = &self.config.title;
let title_start = area.x + 2;
let title_end = title_start + title.len() as u16;
buf.cell_mut((area.x + 1, area.y))
.map(|cell| cell.set_char(' ').set_style(bg_style));
for (i, ch) in title.chars().enumerate() {
let x = title_start + i as u16;
if x < area.x + area.width - 1 {
buf.cell_mut((x, area.y))
.map(|cell| cell.set_char(ch).set_style(title_style));
}
}
if title_end < area.x + area.width - 1 {
buf.cell_mut((title_end, area.y))
.map(|cell| cell.set_char(' ').set_style(bg_style));
}
let line_start = title_end + 1;
for x in line_start..(area.x + area.width - 1) {
buf.cell_mut((x, area.y))
.map(|cell| cell.set_char(HORIZONTAL).set_style(border_style));
}
} else {
for x in (area.x + 1)..(area.x + area.width - 1) {
buf.cell_mut((x, area.y))
.map(|cell| cell.set_char(HORIZONTAL).set_style(border_style));
}
}
buf.cell_mut((area.x + area.width - 1, area.y))
.map(|cell| cell.set_char(CORNER_TOP_RIGHT).set_style(border_style));
for y in (area.y + 1)..(area.y + area.height - 1) {
buf.cell_mut((area.x, y))
.map(|cell| cell.set_char(VERTICAL).set_style(border_style));
buf.cell_mut((area.x + area.width - 1, y))
.map(|cell| cell.set_char(VERTICAL).set_style(border_style));
}
buf.cell_mut((area.x, area.y + area.height - 1))
.map(|cell| cell.set_char(CORNER_BOTTOM_LEFT).set_style(border_style));
for x in (area.x + 1)..(area.x + area.width - 1) {
buf.cell_mut((x, area.y + area.height - 1))
.map(|cell| cell.set_char(HORIZONTAL).set_style(border_style));
}
buf.cell_mut((area.x + area.width - 1, area.y + area.height - 1))
.map(|cell| cell.set_char(CORNER_BOTTOM_RIGHT).set_style(border_style));
Rect {
x: area.x + 1,
y: area.y + 1,
width: area.width.saturating_sub(2),
height: area.height.saturating_sub(2),
}
}
}