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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use std::io::{Result, Write};
use std::sync::{Mutex, MutexGuard, OnceLock, PoisonError};
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream as Stream, WriteColor};

static TERM: OnceLock<Mutex<Term>> = OnceLock::new();

pub(crate) fn lock() -> MutexGuard<'static, Term> {
    TERM.get_or_init(|| Mutex::new(Term::new()))
        .lock()
        .unwrap_or_else(PoisonError::into_inner)
}

pub(crate) fn bold() {
    lock().set_color(ColorSpec::new().set_bold(true));
}

pub(crate) fn color(color: Color) {
    lock().set_color(ColorSpec::new().set_fg(Some(color)));
}

pub(crate) fn bold_color(color: Color) {
    lock().set_color(ColorSpec::new().set_bold(true).set_fg(Some(color)));
}

pub(crate) fn reset() {
    lock().reset();
}

#[deny(unused_macros)]
macro_rules! print {
    ($($args:tt)*) => {{
        use std::io::Write;
        let _ = std::write!($crate::term::lock(), $($args)*);
    }};
}

#[deny(unused_macros)]
macro_rules! println {
    ($($args:tt)*) => {{
        use std::io::Write;
        let _ = std::writeln!($crate::term::lock(), $($args)*);
    }};
}

pub(crate) struct Term {
    spec: ColorSpec,
    stream: Stream,
    start_of_line: bool,
}

impl Term {
    fn new() -> Self {
        Term {
            spec: ColorSpec::new(),
            stream: Stream::stderr(ColorChoice::Auto),
            start_of_line: true,
        }
    }

    fn set_color(&mut self, spec: &ColorSpec) {
        if self.spec != *spec {
            self.spec = spec.clone();
            self.start_of_line = true;
        }
    }

    fn reset(&mut self) {
        self.spec = ColorSpec::new();
        let _ = self.stream.reset();
    }
}

impl Write for Term {
    // Color one line at a time because Travis does not preserve color setting
    // across output lines.
    fn write(&mut self, mut buf: &[u8]) -> Result<usize> {
        if self.spec.is_none() {
            return self.stream.write(buf);
        }

        let len = buf.len();
        while !buf.is_empty() {
            if self.start_of_line {
                let _ = self.stream.set_color(&self.spec);
            }
            match buf.iter().position(|byte| *byte == b'\n') {
                Some(line_len) => {
                    self.stream.write_all(&buf[..line_len + 1])?;
                    self.start_of_line = true;
                    buf = &buf[line_len + 1..];
                }
                None => {
                    self.stream.write_all(buf)?;
                    self.start_of_line = false;
                    break;
                }
            }
        }
        Ok(len)
    }

    fn flush(&mut self) -> Result<()> {
        self.stream.flush()
    }
}