terminal/
lib.rs

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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//! A helper library for terminal output.
//!
//! This library is used by Spin to print out messages in an appropriate format
//! that is easy for users to read. This is not meant as a general purpose library.

use std::{io::IsTerminal, sync::OnceLock};
use termcolor::{ColorSpec, StandardStream, StandardStreamLock, WriteColor};

static COLOR_OUT: OnceLock<StandardStream> = OnceLock::new();
static COLOR_ERR: OnceLock<StandardStream> = OnceLock::new();

/// A wrapper around a standard stream lock that resets the color on drop
pub struct ColorText(StandardStreamLock<'static>);

impl ColorText {
    /// Create a `ColorText` tied to stdout
    pub fn stdout(spec: ColorSpec) -> ColorText {
        let stream =
            COLOR_OUT.get_or_init(|| StandardStream::stdout(color_choice(std::io::stdout())));
        set_color(stream, spec)
    }

    /// Create a `ColorText` tied to stderr
    pub fn stderr(spec: ColorSpec) -> ColorText {
        let stream =
            COLOR_ERR.get_or_init(|| StandardStream::stderr(color_choice(std::io::stderr())));
        set_color(stream, spec)
    }
}

impl std::io::Write for ColorText {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.0.write(buf)
    }

    fn flush(&mut self) -> std::io::Result<()> {
        self.0.flush()
    }
}

impl WriteColor for ColorText {
    fn supports_color(&self) -> bool {
        self.0.supports_color()
    }

    fn set_color(&mut self, spec: &ColorSpec) -> std::io::Result<()> {
        self.0.set_color(spec)
    }

    fn reset(&mut self) -> std::io::Result<()> {
        self.0.reset()
    }
}

impl Drop for ColorText {
    fn drop(&mut self) {
        let _ = self.reset();
    }
}

fn set_color(stream: &'static StandardStream, spec: ColorSpec) -> ColorText {
    let mut lock = stream.lock();
    let _ = lock.set_color(&spec);
    ColorText(lock)
}

fn color_choice(stream: impl IsTerminal) -> termcolor::ColorChoice {
    if stream.is_terminal() {
        termcolor::ColorChoice::Auto
    } else {
        termcolor::ColorChoice::Never
    }
}

#[macro_export]
macro_rules! step {
    ($step:expr, $($arg:tt)*) => {{
        $crate::cprint!($crate::colors::bold_green(), $step);
        print!(" ");
        println!($($arg)*);
    }};
}

#[macro_export]
macro_rules! warn {
    ($($arg:tt)*) => {{
        $crate::ceprint!($crate::colors::bold_yellow(), "Warning");
        eprint!(": ");
        eprintln!($($arg)*);
    }};
}

#[macro_export]
macro_rules! error {
    ($($arg:tt)*) => {{
        $crate::ceprint!($crate::colors::bold_red(), "Error");
        eprint!(": ");
        eprintln!($($arg)*);
    }};
}

#[macro_export]
macro_rules! einfo {
    ($highlight:expr, $($arg:tt)*) => {{
        $crate::ceprint!($crate::colors::bold_cyan(), $highlight);
        eprint!(" ");
        eprintln!($($arg)*);
    }};
}

#[macro_export]
macro_rules! cprint {
    ($color:expr, $($arg:tt)*) => {
        use std::io::Write;
        let mut out = $crate::ColorText::stdout($color);
        let _ = write!(out, $($arg)*);
        drop(out); // Reset colors
    };
}

#[macro_export]
macro_rules! ceprint {
    ($color:expr, $($arg:tt)*) => {
        use std::io::Write;
        let mut out = $crate::ColorText::stderr($color);
        let _ = write!(out, $($arg)*);
        drop(out); // Reset colors
    };
}

#[macro_export]
macro_rules! ceprintln {
    ($color:expr, $($arg:tt)*) => {
        use std::io::Write;
        let mut out = $crate::ColorText::stderr($color);
        let _ = writeln!(out, $($arg)*);
        drop(out); // Reset colors
    };
}

pub mod colors {
    use termcolor::{Color, ColorSpec};

    pub fn bold_red() -> ColorSpec {
        new(Color::Red, true)
    }

    pub fn bold_green() -> ColorSpec {
        new(Color::Green, true)
    }

    pub fn bold_cyan() -> ColorSpec {
        new(Color::Cyan, true)
    }

    pub fn bold_yellow() -> ColorSpec {
        new(Color::Yellow, true)
    }

    fn new(color: Color, bold: bool) -> ColorSpec {
        let mut s = ColorSpec::new();
        s.set_fg(Some(color)).set_bold(bold);
        s
    }
}