feat(graphical): add support for stylization on titles

This commit is contained in:
Gabe Weiner 2026-05-26 14:03:35 -04:00
parent df7bcfa17d
commit 2995233ab9
No known key found for this signature in database
GPG Key ID: 4625DB8D550830D8
3 changed files with 25 additions and 1 deletions

View File

@ -351,7 +351,8 @@ impl GraphicalReportHandler {
opts = opts.word_splitter(word_splitter);
}
writeln!(f, "{}", self.wrap(&diagnostic.to_string(), opts))?;
let styled = diagnostic.style(self.theme.styles.title).to_string();
writeln!(f, "{}", self.wrap(&styled, opts))?;
if !self.with_cause_chain {
return Ok(());

View File

@ -85,6 +85,8 @@ Styles for various parts of graphical rendering for the
*/
#[derive(Debug, Clone)]
pub struct ThemeStyles {
/// Style to apply to the title.
pub title: Style,
/// Style to apply to things highlighted as "error".
pub error: Style,
/// Style to apply to things highlighted as "warning".
@ -111,6 +113,7 @@ impl ThemeStyles {
/// [Credit](http://terminal.sexy/#FRUV0NDQFRUVrEFCkKlZ9L91ap-1qnWfdbWq0NDQUFBQrEFCkKlZ9L91ap-1qnWfdbWq9fX1).
pub fn rgb() -> Self {
Self {
title: style(),
error: style().fg_rgb::<255, 30, 30>(),
warning: style().fg_rgb::<244, 191, 117>(),
advice: style().fg_rgb::<106, 159, 181>(),
@ -128,6 +131,7 @@ impl ThemeStyles {
/// ANSI color-based styles.
pub fn ansi() -> Self {
Self {
title: style(),
error: style().red(),
warning: style().yellow(),
advice: style().cyan(),
@ -145,6 +149,7 @@ impl ThemeStyles {
/// No styling. Just regular ol' monochrome.
pub fn none() -> Self {
Self {
title: style(),
error: style(),
warning: style(),
advice: style(),

View File

@ -2030,6 +2030,24 @@ fn syntax_highlighter_on_real_file() {
assert_eq!(expected, strip_ansi_escapes::strip_str(out));
}
#[test]
fn title_style_is_applied() {
use miette::{GraphicalReportHandler, GraphicalTheme, Report, ThemeCharacters, ThemeStyles};
let theme = GraphicalTheme {
characters: ThemeCharacters::unicode(),
styles: ThemeStyles {
title: owo_colors::Style::new().bold(),
..ThemeStyles::none()
},
};
let mut out = String::new();
GraphicalReportHandler::new_themed(theme)
.render_report(&mut out, Report::msg("hello").as_ref())
.unwrap();
assert_eq!(out, " × \u{1b}[1mhello\u{1b}[0m\n",);
}
#[test]
fn triple_adjacent_highlight() -> Result<(), MietteError> {
#[derive(Debug, Diagnostic, Error)]