1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use {
    super::*,
    crossterm::tty::IsTty,
    minimad::{OwningTemplateExpander, TextTemplate},
    termimad::*,
};

/// A small helper to print using markdown templates
pub struct Printer {
    pub skin: MadSkin,
    pub terminal_width: usize,
}

impl Printer {
    /// create a new printer
    ///
    /// The skin will be without style and color if the
    /// output is piped.
    pub fn new() -> Self {
        let terminal_width = terminal_size().0 as usize;
        let color = !is_output_piped();
        let skin = skin::make_skin(color);
        Self {
            skin,
            terminal_width,
        }
    }
    pub fn print(&self, expander: OwningTemplateExpander, template: &str) {
        let template = TextTemplate::from(template);
        let text = expander.expand(&template);
        let fmt_text = FmtText::from_text(&self.skin, text, Some(self.terminal_width));
        print!("{}", fmt_text);
    }
}

fn is_output_piped() -> bool {
    !std::io::stdout().is_tty()
}