Don't emit color codes if stderr isn't a TTY

Fixes #521
This commit is contained in:
Laurenz 2023-05-22 15:27:41 +02:00
parent 0214569f3a
commit ef1bf742f6

View File

@ -71,7 +71,7 @@ fn set_failed() {
/// Print an application-level error (independent from a source file).
fn print_error(msg: &str) -> io::Result<()> {
let mut w = StandardStream::stderr(ColorChoice::Auto);
let mut w = color_stream();
let styles = term::Styles::default();
w.set_color(&styles.header_error)?;
@ -296,8 +296,9 @@ fn status(command: &CompileSettings, status: Status) -> io::Result<()> {
let message = status.message();
let color = status.color();
let mut w = StandardStream::stderr(ColorChoice::Auto);
let mut w = color_stream();
if atty::is(Stream::Stderr) {
// Clear the terminal.
write!(w, "{esc}c{esc}[1;1H")?;
}
@ -318,6 +319,15 @@ fn status(command: &CompileSettings, status: Status) -> io::Result<()> {
w.flush()
}
/// Get stderr with color support if desirable.
fn color_stream() -> termcolor::StandardStream {
termcolor::StandardStream::stderr(if atty::is(Stream::Stderr) {
ColorChoice::Auto
} else {
ColorChoice::Never
})
}
/// The status in which the watcher can be.
enum Status {
Compiling,