Skip to content
Open
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 src/markdown/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ impl<'a> InlinesParser<'a> {
}
};
style = base_style.clone();
for html_style in html_styles.iter().rev() {
for html_style in html_styles.iter() {
style.merge(&html_style.0);
}
}
Expand Down
35 changes: 32 additions & 3 deletions src/markdown/text_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,14 @@ where
}

/// Merge this style with another one.
///
/// If `other` defines a background or foreground color, that overwrites the respective color
/// in `self`.
pub(crate) fn merge(&mut self, other: &TextStyle<C>) {
self.flags |= other.flags;
self.size = self.size.max(other.size);
self.colors.background = self.colors.background.clone().or(other.colors.background.clone());
self.colors.foreground = self.colors.foreground.clone().or(other.colors.foreground.clone());
self.colors.background = other.colors.background.clone().or(self.colors.background.clone());
self.colors.foreground = other.colors.foreground.clone().or(self.colors.foreground.clone());
}

/// Return a new style merged with the one passed in.
Expand Down Expand Up @@ -502,7 +505,7 @@ mod tests {
#[case::strikethrough(TextStyle::default().strikethrough(), &[TextAttribute::Strikethrough])]
#[case::underlined(TextStyle::default().underlined(), &[TextAttribute::Underlined])]
#[case::bg_color(TextStyle::default().bg_color(Color::Red), &[TextAttribute::BackgroundColor(Color::Red)])]
#[case::bg_color(TextStyle::default().fg_color(Color::Red), &[TextAttribute::ForegroundColor(Color::Red)])]
#[case::fg_color(TextStyle::default().fg_color(Color::Red), &[TextAttribute::ForegroundColor(Color::Red)])]
#[case::all(
TextStyle::default().bold().code().italics().strikethrough().underlined().bg_color(Color::Black).fg_color(Color::Red),
&[
Expand All @@ -518,4 +521,30 @@ mod tests {
let attrs: Vec<_> = style.iter_attributes().collect();
assert_eq!(attrs, expected);
}

#[rstest]
#[case::fg_color(
TextStyle::default().bold().fg_color(Color::Red),
TextStyle::default().fg_color(Color::Blue),
TextStyle::default().bold().fg_color(Color::Blue)
)]
#[case::fg_color(
TextStyle::default().fg_color(Color::Red),
TextStyle::default().bold().fg_color(Color::Blue),
TextStyle::default().bold().fg_color(Color::Blue)
)]
#[case::bg_color(
TextStyle::default().bold().bg_color(Color::Yellow),
TextStyle::default().bg_color(Color::Green),
TextStyle::default().bold().bg_color(Color::Green)
)]
#[case::bg_color(
TextStyle::default().bg_color(Color::Yellow),
TextStyle::default().bold().bg_color(Color::Green),
TextStyle::default().bold().bg_color(Color::Green)
)]
fn merge_overwrites_colors(#[case] mut some: TextStyle, #[case] other: TextStyle, #[case] output: TextStyle) {
some.merge(&other);
assert_eq!(some, output);
}
}
2 changes: 1 addition & 1 deletion src/terminal/virt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl VirtualTerminal {
}

fn print_text(&mut self, content: &str, style: &TextStyle) -> io::Result<()> {
let style = style.merged(&TextStyle::default().colors(self.colors));
let style = TextStyle::default().colors(self.colors).merged(style);
for c in content.chars() {
let Some(cell) = self.current_cell_mut() else {
continue;
Expand Down
4 changes: 4 additions & 0 deletions src/ui/modals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ struct ContentRow {
}

impl ContentRow {
/// Overwrite the style of a content row.
///
/// The current background and foreground style is overwritten with the respective definitions
/// of `style`, if they are set.
fn with_style(mut self, style: TextStyle) -> ContentRow {
for chunk in &mut self.content {
chunk.style.merge(&style);
Expand Down