|
1 |
| -use std::{borrow::Cow, fmt, ops::Deref, sync::Arc}; |
| 1 | +use std::{ |
| 2 | + borrow::Cow, |
| 3 | + fmt, |
| 4 | + ops::Deref, |
| 5 | + sync::{Arc, LazyLock}, |
| 6 | +}; |
2 | 7 |
|
3 | 8 | use cow_utils::CowUtils;
|
4 | 9 | use miette::{GraphicalTheme, IntoDiagnostic, MietteDiagnostic};
|
@@ -165,23 +170,29 @@ impl Diagnostic {
|
165 | 170 | }
|
166 | 171 | }
|
167 | 172 |
|
| 173 | +static COLORED_GRAPHICAL_REPORT_HANDLER: LazyLock<GraphicalReportHandler> = LazyLock::new(|| { |
| 174 | + GraphicalReportHandler::new() |
| 175 | + .with_theme(GraphicalTheme::unicode()) |
| 176 | + .with_context_lines(2) |
| 177 | + .with_width(usize::MAX) |
| 178 | +}); |
| 179 | + |
| 180 | +static NO_COLOR_GRAPHICAL_REPORT_HANDLER: LazyLock<GraphicalReportHandler> = LazyLock::new(|| { |
| 181 | + GraphicalReportHandler::new() |
| 182 | + .with_theme(GraphicalTheme::unicode_nocolor()) |
| 183 | + .with_context_lines(2) |
| 184 | + .with_width(usize::MAX) |
| 185 | + .without_syntax_highlighting() |
| 186 | +}); |
| 187 | + |
168 | 188 | impl Diagnostic {
|
169 | 189 | pub fn render_report(&self, colored: bool) -> crate::Result<String> {
|
170 | 190 | let mut buf = String::new();
|
171 |
| - let theme = if colored { |
172 |
| - GraphicalTheme::unicode() |
173 |
| - } else { |
174 |
| - GraphicalTheme::unicode_nocolor() |
175 |
| - }; |
176 |
| - let h = GraphicalReportHandler::new() |
177 |
| - .with_theme(theme) |
178 |
| - .with_context_lines(2) |
179 |
| - .with_width(usize::MAX); |
180 | 191 |
|
181 |
| - let h = if !colored { |
182 |
| - h.without_syntax_highlighting() |
| 192 | + let h = if colored { |
| 193 | + &COLORED_GRAPHICAL_REPORT_HANDLER |
183 | 194 | } else {
|
184 |
| - h |
| 195 | + &NO_COLOR_GRAPHICAL_REPORT_HANDLER |
185 | 196 | };
|
186 | 197 |
|
187 | 198 | h.render_report(&mut buf, self.as_ref()).into_diagnostic()?;
|
|
0 commit comments