zkat / miette

Fancy extension for std::error::Error with pretty, detailed diagnostic printing.
https://docs.rs/miette
Apache License 2.0
1.85k stars 106 forks source link

NO_COLOR is printing ansi in non-terminals #380

Closed Boshen closed 3 weeks ago

Boshen commented 3 weeks ago

Background: https://github.com/oxc-project/oxc/issues/3539

Currently, if you use oxc_diagnostics with GraphicalReportHandler, and you print to a non-terminal, the output will contain color escape codes, even with NO_COLOR=1. This is because if stdout or stderr are not a terminal, GraphicalTheme::default() will return GraphicalTheme::ascii() regardless of the NO_COLOR value, and ascii() does have colors. This means that when printing to a non-terminal, there is no way to turn off the color escapes.

When printing to a non-terminal, you usually don't want color output, so presumably the default GraphicalTheme on non-terminals should be GraphicalTheme::none(). But in some cases it might be useful to enable color, even if not by default. Maybe FORCE_COLOR could be used in that case.

https://github.com/zkat/miette/blob/b82cc81b8ea32a1cf1b4598ed5832bc8e3b0e161/src/handlers/theme.rs#L72-L75

When it's non-terminal, Self::ascii()

https://github.com/zkat/miette/blob/b82cc81b8ea32a1cf1b4598ed5832bc8e3b0e161/src/handlers/theme.rs#L25-L31

still uses ansi colors.

If the intention of NO_COLOR is to always not print ansi color codes, I can PR a change to GraphicalTheme::none().

zkat commented 3 weeks ago

yeah uh, that seems like a bug.