fix(handler): do not call `supports_color::on` when color is forced

This reduces a sys call when color is forced.

I'm not sure whether the logic after the change is correct or not,
but the documentation seems to support it?

https://docs.rs/miette/latest/miette/struct.MietteHandlerOpts.html#method.color

> If true, colors will be used during graphical rendering, regardless of whether or not the terminal supports them.
>
> If false, colors will never be used.
>
> If unspecified, colors will be used only if the terminal supports them.
This commit is contained in:
Boshen 2023-12-23 23:43:41 +08:00
parent 7ff4f874d6
commit 16f37e4778
No known key found for this signature in database
GPG Key ID: 9C7A8C8AB22BEBD1
1 changed files with 5 additions and 5 deletions

View File

@ -222,17 +222,17 @@ impl MietteHandlerOpts {
};
let styles = if self.color == Some(false) {
ThemeStyles::none()
} else if self.color == Some(true) {
match self.rgb_colors {
RgbColors::Always => ThemeStyles::rgb(),
_ => ThemeStyles::ansi(),
}
} else if let Some(color) = supports_color::on(supports_color::Stream::Stderr) {
match self.rgb_colors {
RgbColors::Always => ThemeStyles::rgb(),
RgbColors::Preferred if color.has_16m => ThemeStyles::rgb(),
_ => ThemeStyles::ansi(),
}
} else if self.color == Some(true) {
match self.rgb_colors {
RgbColors::Always => ThemeStyles::rgb(),
_ => ThemeStyles::ansi(),
}
} else {
ThemeStyles::none()
};