Skip to content

Commit

Permalink
fix(diagnostics): check for terminal when displaying links
Browse files Browse the repository at this point in the history
fixes #5819
  • Loading branch information
Boshen committed Sep 24, 2024
1 parent e3c8a12 commit 2f114c6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
6 changes: 4 additions & 2 deletions crates/oxc_diagnostics/src/graphic_reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
22 changes: 8 additions & 14 deletions crates/oxc_diagnostics/src/graphical_theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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() }
Expand Down Expand Up @@ -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).
Expand Down

0 comments on commit 2f114c6

Please sign in to comment.