termimad 0.35.2

Markdown Renderer for the Terminal
Documentation
use {
    std::borrow::Cow,
    unicode_width::UnicodeWidthChar,
};

pub static TAB_REPLACEMENT: &str = "  ";

/// Information about the fitting of a string into a given
/// width in cols.
///
/// The implementation here properly takes into account
/// the width of special characters.
///
/// Backspaces are considered as having a width of -1.
///
/// This implementation is based on a replacement of the
/// tab character.
#[derive(Debug, Clone, Copy)]
pub struct StrFit {
    bytes_count: usize,
    cols_count: usize,
    has_tab: bool,
}

impl StrFit {
    pub fn from(s: &str, cols_max: usize) -> Self {
        let mut bytes_count = 0;
        let mut cols_count: i32 = 0;
        let mut has_tab = false;
        for (idx, c) in s.char_indices() {
            let char_width: i32 = match c {
                '\t' => {
                    // tab
                    has_tab = true;
                    TAB_REPLACEMENT.len() as i32
                }
                '\x08' => {
                    // backspace
                    -1
                }
                _ => UnicodeWidthChar::width(c).map(|w| w as i32).unwrap_or(0),
            };
            let next_str_width = cols_count + char_width;
            if next_str_width > 0 && next_str_width as usize > cols_max {
                break;
            }
            cols_count = next_str_width;
            bytes_count = idx + c.len_utf8();
        }
        Self {
            bytes_count,
            cols_count: cols_count.max(0) as usize,
            has_tab,
        }
    }

    /// return the counts in bytes and columns of the longest substring
    /// fitting the given number of columns
    pub fn count_fitting(s: &str, cols_max: usize) -> (usize, usize) {
        let fit = StrFit::from(s, cols_max);
        (fit.bytes_count, fit.cols_count)
    }

    /// return both the longest fitting string and the number of cols
    /// it takes on screen.
    ///
    /// We don't build a string around the whole str, which could be costly
    /// if it's very big
    pub fn make_string(s: &str, cols_max: usize) -> (String, usize) {
        let fit = StrFit::from(s, cols_max);
        if fit.has_tab {
            let string = (s[0..fit.bytes_count]).replace('\t', TAB_REPLACEMENT);
            (string, fit.cols_count)
        } else {
            (s[0..fit.bytes_count].to_string(), fit.cols_count)
        }
    }
    /// return both the longest fitting string and the number of cols
    /// it takes on screen.
    ///
    /// We don't build a string around the whole str, which could be costly
    /// if it's very big
    /// In case there's no tab in the input string, we can return a pointer over
    /// part of the original str)
    pub fn make_cow(s: &str, cols_max: usize) -> (Cow<'_, str>, usize) {
        let fit = StrFit::from(s, cols_max);
        if fit.has_tab {
            // we can't just borrow, as we insert chars
            let string = (s[0..fit.bytes_count]).replace('\t', TAB_REPLACEMENT);
            (Cow::Owned(string), fit.cols_count)
        } else {
            (Cow::Borrowed(&s[0..fit.bytes_count]), fit.cols_count)
        }
    }
}