Skip to content

fix: Only bold the first title of a message #212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/custom_level.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions examples/footer.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/highlight_title.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 1 addition & 6 deletions src/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ impl<'a> Level<'a> {
groups: vec![Group::new().element(Element::Title(Title {
level: self,
title: header,
primary: true,
}))],
}
}
Expand All @@ -92,11 +91,7 @@ impl<'a> Level<'a> {
///
/// </div>
pub fn title(self, title: &'a str) -> Title<'a> {
Title {
level: self,
title,
primary: false,
}
Title { level: self, title }
}

pub(crate) fn as_str(&self) -> &'a str {
Expand Down
48 changes: 27 additions & 21 deletions src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,16 @@ impl Renderer {
let peek = message_iter.peek().map(|(_, s)| s).copied();
match &section {
Element::Title(title) => {
let title_style = match (i == 0, g == 0) {
(true, true) => TitleStyle::MainHeader,
(true, false) => TitleStyle::Header,
(false, _) => TitleStyle::Secondary,
};
self.render_title(
&mut buffer,
title,
peek,
max_line_num_len,
if i == 0 { false } else { !title.primary },
title_style,
message.id.as_ref().and_then(|id| {
if g == 0 && i == 0 {
Some(id)
Expand Down Expand Up @@ -433,26 +437,14 @@ impl Renderer {
&self,
buffer: &mut StyledBuffer,
title: &Title<'_>,
next_section: Option<&Element<'_>>,
max_line_num_len: usize,
is_secondary: bool,
title_style: TitleStyle,
id: Option<&&str>,
is_cont: bool,
) {
let line_offset = buffer.num_lines();

let (has_primary_spans, has_span_labels) =
next_section.map_or((false, false), |s| match s {
Element::Title(_) | Element::Padding(_) => (false, false),
Element::Cause(cause) => (
cause.markers.iter().any(|m| m.kind.is_primary()),
cause.markers.iter().any(|m| m.label.is_some()),
),
Element::Suggestion(_) => (true, false),
Element::Origin(_) => (false, true),
});

if !has_primary_spans && !has_span_labels && is_secondary {
if title_style == TitleStyle::Secondary {
// This is a secondary message with no span info
for _ in 0..max_line_num_len {
buffer.prepend(line_offset, " ", ElementStyle::NoStyle);
Expand Down Expand Up @@ -503,10 +495,10 @@ impl Renderer {
buffer.append(line_offset, "]", ElementStyle::Level(title.level.level));
label_width += 2 + id.len();
}
let header_style = if is_secondary {
ElementStyle::HeaderMsg
} else {
ElementStyle::MainHeaderMsg
let header_style = match title_style {
TitleStyle::MainHeader => ElementStyle::MainHeaderMsg,
TitleStyle::Header => ElementStyle::HeaderMsg,
TitleStyle::Secondary => unreachable!(),
};
if title.level.name != Some(None) {
buffer.append(line_offset, ": ", header_style);
Expand Down Expand Up @@ -661,7 +653,14 @@ impl Renderer {
max_line_num_len + 1,
);
let title = Level::NOTE.title(label);
self.render_title(buffer, &title, None, max_line_num_len, true, None, false);
self.render_title(
buffer,
&title,
max_line_num_len,
TitleStyle::Secondary,
None,
false,
);
}
}

Expand Down Expand Up @@ -2717,6 +2716,13 @@ pub enum OutputTheme {
Unicode,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum TitleStyle {
MainHeader,
Header,
Secondary,
}

#[cfg(test)]
mod test {
use super::OUTPUT_REPLACEMENTS;
Expand Down
8 changes: 0 additions & 8 deletions src/snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,6 @@ pub struct Padding;
pub struct Title<'a> {
pub(crate) level: Level<'a>,
pub(crate) title: &'a str,
pub(crate) primary: bool,
}

impl Title<'_> {
pub fn primary(mut self, primary: bool) -> Self {
self.primary = primary;
self
}
}

/// A source view [`Element`] in a [`Group`]
Expand Down
2 changes: 2 additions & 0 deletions tests/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1663,6 +1663,8 @@ zappy
let input_new = Level::ERROR
.header("the size for values of type `T` cannot be known at compilation time")
.id("E0277")
// We need an empty group here to ensure the HELP line is rendered correctly
.group(Group::new())
.group(
Group::new()
.element(Level::HELP.title(
Expand Down