From ef1bf742f6b73e60a8adb40281140314dc3e7ff4 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 22 May 2023 15:27:41 +0200 Subject: [PATCH] Don't emit color codes if stderr isn't a TTY Fixes #521 --- cli/src/main.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index df5c5c82c..408fe2f27 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -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,