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
use std::io::Write;
use anyhow::Context as _;
pub use termcolor::{Color, ColorChoice};
use termcolor::{ColorSpec, StandardStream, WriteColor};
use crate::error::CargoResult;
fn colorize_stderr() -> ColorChoice {
if concolor_control::get(concolor_control::Stream::Stderr).color() {
ColorChoice::Always
} else {
ColorChoice::Never
}
}
pub fn print(
status: &str,
message: impl std::fmt::Display,
color: Color,
justified: bool,
) -> CargoResult<()> {
let color_choice = colorize_stderr();
let mut output = StandardStream::stderr(color_choice);
output.set_color(ColorSpec::new().set_fg(Some(color)).set_bold(true))?;
if justified {
write!(output, "{status:>12}")?;
} else {
write!(output, "{}", status)?;
output.set_color(ColorSpec::new().set_bold(true))?;
write!(output, ":")?;
}
output.reset()?;
writeln!(output, " {message}").with_context(|| "Failed to write message")?;
Ok(())
}
pub fn status(action: &str, message: impl std::fmt::Display) -> CargoResult<()> {
print(action, message, Color::Green, true)
}
pub fn error(message: impl std::fmt::Display) -> CargoResult<()> {
print("error", message, Color::Red, false)
}
pub fn warn(message: impl std::fmt::Display) -> CargoResult<()> {
print("warning", message, Color::Yellow, false)
}
pub fn note(message: impl std::fmt::Display) -> CargoResult<()> {
print("note", message, Color::Cyan, false)
}
pub fn write_stderr(fragment: impl std::fmt::Display, spec: &ColorSpec) -> CargoResult<()> {
let color_choice = colorize_stderr();
let mut output = StandardStream::stderr(color_choice);
output.set_color(spec)?;
write!(output, "{}", fragment)?;
output.reset()?;
Ok(())
}