From 22a02743b23ea7337c6632799d5ca6d6c83031cd Mon Sep 17 00:00:00 2001 From: Viktor Kleen Date: Thu, 18 Jan 2024 14:17:36 +0000 Subject: [PATCH] Check if stderr is a terminal for error messages (#1766) Prveiusly we checked whether `stdout` is a terminal to make our coloring decision, and then we proceeded to happily assume the same state applies to `stderr` for ad mctual error messages. However, if someone redirects `stdout` to capture evaluation results, `stderr` may still be a terminal and should receive colored output in this case. This PR omplements this behavior and makes the choice of which output stream to check explicit. --- core/src/error/report.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/core/src/error/report.rs b/core/src/error/report.rs index 28a9e7b093..501f032bc8 100644 --- a/core/src/error/report.rs +++ b/core/src/error/report.rs @@ -26,19 +26,11 @@ pub enum ErrorFormat { #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct ColorOpt(pub(crate) clap::ColorChoice); -impl From for ColorOpt { - fn from(color_choice: clap::ColorChoice) -> Self { - Self(color_choice) - } -} - -impl From for ColorChoice { - fn from(c: ColorOpt) -> Self { - use std::io::{stdout, IsTerminal}; - - match c.0 { +impl ColorOpt { + fn for_terminal(self, is_terminal: bool) -> ColorChoice { + match self.0 { clap::ColorChoice::Auto => { - if stdout().is_terminal() { + if is_terminal { ColorChoice::Auto } else { ColorChoice::Never @@ -50,6 +42,12 @@ impl From for ColorChoice { } } +impl From for ColorOpt { + fn from(color_choice: clap::ColorChoice) -> Self { + Self(color_choice) + } +} + impl Default for ColorOpt { fn default() -> Self { Self(clap::ColorChoice::Auto) @@ -68,9 +66,11 @@ pub fn report>( format: ErrorFormat, color_opt: ColorOpt, ) { + use std::io::{stderr, IsTerminal}; + let stdlib_ids = cache.get_all_stdlib_modules_file_id(); report_with( - &mut StandardStream::stderr(color_opt.into()).lock(), + &mut StandardStream::stderr(color_opt.for_terminal(stderr().is_terminal())).lock(), cache.files_mut(), stdlib_ids.as_ref(), error,