Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if stderr is a terminal for error messages #1766

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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