Skip to content

Commit

Permalink
Check if stderr is a terminal for error messages (#1766)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vkleen authored Jan 18, 2024
1 parent 28b6c71 commit 22a0274
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions core/src/error/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,11 @@ pub enum ErrorFormat {
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct ColorOpt(pub(crate) clap::ColorChoice);

impl From<clap::ColorChoice> for ColorOpt {
fn from(color_choice: clap::ColorChoice) -> Self {
Self(color_choice)
}
}

impl From<ColorOpt> 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
Expand All @@ -50,6 +42,12 @@ impl From<ColorOpt> for ColorChoice {
}
}

impl From<clap::ColorChoice> for ColorOpt {
fn from(color_choice: clap::ColorChoice) -> Self {
Self(color_choice)
}
}

impl Default for ColorOpt {
fn default() -> Self {
Self(clap::ColorChoice::Auto)
Expand All @@ -68,9 +66,11 @@ pub fn report<E: IntoDiagnostics<FileId>>(
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,
Expand Down

0 comments on commit 22a0274

Please sign in to comment.