termimad/
text.rs

1use {
2    crate::{
3        code,
4        fit::wrap,
5        line::FmtLine,
6        skin::MadSkin,
7        tbl,
8    },
9    minimad::{
10        parse_text,
11        Options,
12        Text,
13    },
14    std::fmt,
15};
16
17/// a formatted text, implementing Display.
18///
19/// The text is wrapped for the width given at build, which
20/// means the rendering height is the number of lines.
21///
22/// ```
23/// use termimad::*;
24/// let skin = MadSkin::default();
25/// let my_markdown = "#title\n* item 1\n* item 2";
26/// let text = FmtText::from(&skin, &my_markdown, Some(80));
27/// println!("{}", &text);
28/// ```
29#[derive(Debug)]
30pub struct FmtText<'k, 's> {
31    pub skin: &'k MadSkin,
32    pub lines: Vec<FmtLine<'s>>,
33    pub width: Option<usize>, // available width
34}
35
36impl<'k, 's> FmtText<'k, 's> {
37    /// build a displayable text for the specified width and skin
38    ///
39    /// This can be called directly or using one of the skin helper
40    /// method.
41    pub fn from(skin: &'k MadSkin, src: &'s str, width: Option<usize>) -> FmtText<'k, 's> {
42        let mt = parse_text(src, Options::default());
43        Self::from_text(skin, mt, width)
44    }
45    /// build a text as raw (with no markdown interpretation)
46    pub fn raw_str(skin: &'k MadSkin, src: &'s str, width: Option<usize>) -> FmtText<'k, 's> {
47        let mt = Text::raw_str(src);
48        Self::from_text(skin, mt, width)
49    }
50
51    /// build a fmt_text from a minimad text
52    pub fn from_text(
53        skin: &'k MadSkin,
54        mut text: Text<'s>,
55        width: Option<usize>,
56    ) -> FmtText<'k, 's> {
57        let mut lines = text
58            .lines
59            .drain(..)
60            .map(|mline| FmtLine::from(mline, skin))
61            .collect();
62        tbl::fix_all_tables(&mut lines, width.unwrap_or(usize::MAX), skin);
63        code::justify_blocks(&mut lines);
64        if let Some(width) = width {
65            if width >= 3 {
66                lines =
67                    wrap::hard_wrap_lines(lines, width, skin).expect("width should be wide enough");
68            }
69        }
70        FmtText { skin, lines, width }
71    }
72    /// set the width to render the text to.
73    ///
74    /// It's preferable to set it no smaller than content_width and
75    /// no wider than the terminal's width.
76    ///
77    /// If you want the text to be wrapped, pass a width on construction
78    /// (ie in FmtText::from or FmtText::from_text) instead.
79    /// The main purpose of this function is to optimize the rendering
80    /// of a text (or several ones) to a content width, for example to
81    /// have centered titles centered not based on the terminal's width
82    /// but on the content width
83    pub fn set_rendering_width(&mut self, width: usize) {
84        self.width = Some(width);
85    }
86    pub fn content_width(&self) -> usize {
87        self.lines
88            .iter()
89            .fold(0, |cw, line| cw.max(line.visible_length()))
90    }
91}
92
93impl fmt::Display for FmtText<'_, '_> {
94    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95        for line in &self.lines {
96            self.skin.write_fmt_line(f, line, self.width, false)?;
97            writeln!(f)?;
98        }
99        Ok(())
100    }
101}