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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::io::{self, Write};
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};

pub struct Shell {
    stderr: StandardStream,
    // stdout: StandardStream,
}

impl Shell {
    pub fn new() -> Self {
        Self {
            stderr: StandardStream::stderr(ColorChoice::Auto),
            // stdout: StandardStream::stdout(ColorChoice::Auto),
        }
    }

    pub fn status<S, M>(&mut self, status: S, message: M) -> io::Result<()>
    where
        S: AsRef<str>,
        M: AsRef<str>,
    {
        self.stderr
            .set_color(ColorSpec::new().set_fg(Some(Color::Green)).set_bold(true))?;
        write!(self.stderr, "{:>12}", status.as_ref())?;

        self.stderr.reset()?;
        writeln!(self.stderr, " {}", message.as_ref())
    }

    pub fn error<M>(&mut self, message: M) -> io::Result<()>
    where
        M: AsRef<str>,
    {
        self.stderr
            .set_color(ColorSpec::new().set_fg(Some(Color::Red)).set_bold(true))?;
        write!(self.stderr, "error")?;

        self.stderr.reset()?;
        writeln!(self.stderr, ": {}", message.as_ref().trim_end())
    }

    pub fn warning<M>(&mut self, message: M) -> io::Result<()>
    where
        M: AsRef<str>,
    {
        self.stderr
            .set_color(ColorSpec::new().set_fg(Some(Color::Yellow)).set_bold(true))?;
        write!(self.stderr, "warning")?;

        self.stderr.set_color(ColorSpec::new().set_bold(true))?;
        writeln!(self.stderr, ": {}", message.as_ref().trim_end())
    }

    pub fn note<M>(&mut self, message: M) -> io::Result<()>
    where
        M: AsRef<str>,
    {
        self.stderr
            .set_color(ColorSpec::new().set_fg(Some(Color::Blue)).set_bold(true))?;
        write!(self.stderr, "   = ")?;

        self.stderr.set_color(ColorSpec::new().set_bold(true))?;
        write!(self.stderr, "note:")?;

        self.stderr.reset()?;
        writeln!(self.stderr, " {}", message.as_ref().trim_end())
    }
}