diff --git a/crates/oxc_diagnostics/src/graphic_reporter.rs b/crates/oxc_diagnostics/src/graphic_reporter.rs index ea55f7fd31b5d7..0a9bade367cd70 100644 --- a/crates/oxc_diagnostics/src/graphic_reporter.rs +++ b/crates/oxc_diagnostics/src/graphic_reporter.rs @@ -6,6 +6,7 @@ /// origin file: https://github.com/zkat/miette/blob/75fea0935e495d0215518c80d32dd820910982e3/src/handlers/graphical.rs#L1 use std::fmt::{self, Write}; +use std::io::IsTerminal; use miette::{ Diagnostic, LabeledSpan, ReportHandler, Severity, SourceCode, SourceSpan, SpanContents, @@ -66,10 +67,11 @@ impl GraphicalReportHandler { /// Create a new `GraphicalReportHandler` with the default /// [`GraphicalTheme`]. This will use both unicode characters and colors. pub fn new() -> Self { + let is_terminal = std::io::stdout().is_terminal() && std::io::stderr().is_terminal(); Self { - links: LinkStyle::Link, + links: if is_terminal { LinkStyle::Link } else { LinkStyle::Text }, termwidth: 400, - theme: GraphicalTheme::default(), + theme: GraphicalTheme::new(is_terminal), footer: None, context_lines: 1, tab_width: 4, diff --git a/crates/oxc_diagnostics/src/graphical_theme.rs b/crates/oxc_diagnostics/src/graphical_theme.rs index f5b6f8d85e1d00..e74bdfcc65e807 100644 --- a/crates/oxc_diagnostics/src/graphical_theme.rs +++ b/crates/oxc_diagnostics/src/graphical_theme.rs @@ -4,8 +4,6 @@ #![allow(dead_code)] /// origin file: https://github.com/zkat/miette/blob/75fea0935e495d0215518c80d32dd820910982e3/src/handlers/theme.rs -use std::io::IsTerminal; - use miette::ThemeCharacters; use owo_colors::Style; @@ -32,6 +30,14 @@ pub struct GraphicalTheme { } impl GraphicalTheme { + pub fn new(is_terminal: bool) -> Self { + match std::env::var("NO_COLOR") { + _ if !is_terminal => Self::none(), + Ok(string) if string != "0" => Self::unicode_nocolor(), + _ => Self::unicode(), + } + } + /// ASCII-art-based graphical drawing, with ANSI styling. pub fn ascii() -> Self { Self { characters: ThemeCharacters::ascii(), styles: ThemeStyles::ansi() } @@ -63,18 +69,6 @@ impl GraphicalTheme { } } -impl Default for GraphicalTheme { - fn default() -> Self { - match std::env::var("NO_COLOR") { - _ if !std::io::stdout().is_terminal() || !std::io::stderr().is_terminal() => { - Self::none() - } - Ok(string) if string != "0" => Self::unicode_nocolor(), - _ => Self::unicode(), - } - } -} - /** Styles for various parts of graphical rendering for the [`GraphicalReportHandler`](crate::GraphicalReportHandler).