harness_cli/utils/
md.rs

1use std::io::IsTerminal;
2
3pub fn print_md(s: impl AsRef<str>) {
4    let mut printer = MarkdownPrinter::new();
5    printer.add(s);
6    printer.dump();
7}
8
9pub struct MarkdownPrinter {
10    content: String,
11}
12
13impl MarkdownPrinter {
14    pub fn new() -> Self {
15        Self {
16            content: String::new(),
17        }
18    }
19
20    fn is_tty(&self) -> bool {
21        std::io::stdout().is_terminal()
22    }
23
24    pub fn dump(&self) {
25        if self.is_tty() {
26            let mut skin = termimad::MadSkin::default();
27            for i in 0..8 {
28                skin.headers[i].align = termimad::Alignment::Left;
29                skin.headers[i].add_attr(termimad::crossterm::style::Attribute::Bold);
30                skin.headers[i].set_fg(termimad::crossterm::style::Color::Blue);
31            }
32            skin.headers[0].set_bg(termimad::crossterm::style::Color::Blue);
33            skin.headers[0].add_attr(termimad::crossterm::style::Attribute::NoUnderline);
34            skin.print_text(&self.content);
35        } else {
36            println!("{}", self.content);
37        }
38    }
39
40    pub fn add(&mut self, s: impl AsRef<str>) {
41        self.content.push_str(s.as_ref());
42    }
43}
44
45#[macro_export]
46macro_rules! print_md {
47    ($($arg:tt)*) => {
48        $crate::utils::md::print_md(format!($($arg)*));
49    };
50}