ratatui_toolkit/widgets/markdown_widget/extensions/toc/methods/required_compact_height.rs
1//! Calculate required height for compact TOC mode.
2
3use super::super::Toc;
4
5impl<'a> Toc<'a> {
6 /// Calculate the required height for compact mode.
7 ///
8 /// With Braille markers, we have 4 vertical dots per cell.
9 /// Height = ceil(entries * spacing / 4) + border_height.
10 ///
11 /// # Arguments
12 ///
13 /// * `content` - The markdown content to scan.
14 /// * `line_spacing` - Spacing between lines in dot units.
15 /// * `show_border` - Whether the border is shown.
16 ///
17 /// # Returns
18 ///
19 /// The required height in rows.
20 pub fn required_compact_height(content: &str, line_spacing: u8, show_border: bool) -> u16 {
21 let entry_count = Self::count_headings(content);
22 if entry_count == 0 {
23 return if show_border { 3 } else { 1 };
24 }
25
26 let spacing = line_spacing.max(1) as f64;
27 // Total dots needed = entries * spacing
28 // Cells needed = ceil(dots / 4)
29 let dots_needed = entry_count as f64 * spacing;
30 let cells_needed = (dots_needed / 4.0).ceil() as u16;
31
32 let border_height = if show_border { 2 } else { 0 };
33 cells_needed + border_height
34 }
35}