summaryrefslogtreecommitdiff
path: root/cli/src/main.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2023-05-22 15:27:41 +0200
committerLaurenz <laurmaedje@gmail.com>2023-05-22 15:28:30 +0200
commitef1bf742f6b73e60a8adb40281140314dc3e7ff4 (patch)
treea1528ba6d40ea950dcc3aad4acfa879b7b7173d9 /cli/src/main.rs
parent0214569f3a6d1284c42bc83c5fc3d79fd3e7fa1b (diff)
Don't emit color codes if stderr isn't a TTY
Fixes #521
Diffstat (limited to 'cli/src/main.rs')
-rw-r--r--cli/src/main.rs14
1 files changed, 12 insertions, 2 deletions
diff --git a/cli/src/main.rs b/cli/src/main.rs
index df5c5c82..408fe2f2 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,