fix(block): Fixed title_style not rendered (#349) (#363)

Fixes #349
This commit is contained in:
Valentin271 2023-08-04 11:47:55 +02:00 committed by GitHub
parent 181706c564
commit 49a82e062f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -415,10 +415,16 @@ impl<'a> Block<'a> {
let title_x = current_offset;
current_offset += title.content.width() as u16 + 1;
// Clone the title's content, applying block title style then the title style
let mut content = title.content.clone();
for span in content.spans.iter_mut() {
span.style = self.titles_style.patch(span.style);
}
buf.set_line(
title_x + area.left(),
self.get_title_y(position, area),
&title.content,
&content,
title_area_width,
);
});
@ -441,10 +447,16 @@ impl<'a> Block<'a> {
let title_x = current_offset;
current_offset += title.content.width() as u16 + 1;
// Clone the title's content, applying block title style then the title style
let mut content = title.content.clone();
for span in content.spans.iter_mut() {
span.style = self.titles_style.patch(span.style);
}
buf.set_line(
title_x + area.left(),
self.get_title_y(position, area),
&title.content,
&content,
title_area_width,
);
});
@ -462,10 +474,16 @@ impl<'a> Block<'a> {
current_offset += title.content.width() as u16 + 1;
let title_x = current_offset - 1; // First element isn't spaced
// Clone the title's content, applying block title style then the title style
let mut content = title.content.clone();
for span in content.spans.iter_mut() {
span.style = self.titles_style.patch(span.style);
}
buf.set_line(
area.width.saturating_sub(title_x) + area.left(),
self.get_title_y(position, area),
&title.content,
&content,
title_area_width,
);
});
@ -917,4 +935,54 @@ mod tests {
assert_buffer_eq!(buffer, Buffer::with_lines(vec![expected]));
}
}
#[test]
fn render_title_content_style() {
for alignment in [Alignment::Left, Alignment::Center, Alignment::Right] {
let mut buffer = Buffer::empty(Rect::new(0, 0, 4, 1));
Block::default()
.title("test".yellow())
.title_alignment(alignment)
.render(buffer.area, &mut buffer);
let mut expected_buffer = Buffer::with_lines(vec!["test"]);
expected_buffer.set_style(Rect::new(0, 0, 4, 1), Style::new().yellow());
assert_buffer_eq!(buffer, expected_buffer);
}
}
#[test]
fn render_block_title_style() {
for alignment in [Alignment::Left, Alignment::Center, Alignment::Right] {
let mut buffer = Buffer::empty(Rect::new(0, 0, 4, 1));
Block::default()
.title("test")
.title_style(Style::new().yellow())
.title_alignment(alignment)
.render(buffer.area, &mut buffer);
let mut expected_buffer = Buffer::with_lines(vec!["test"]);
expected_buffer.set_style(Rect::new(0, 0, 4, 1), Style::new().yellow());
assert_buffer_eq!(buffer, expected_buffer);
}
}
#[test]
fn title_style_overrides_block_title_style() {
for alignment in [Alignment::Left, Alignment::Center, Alignment::Right] {
let mut buffer = Buffer::empty(Rect::new(0, 0, 4, 1));
Block::default()
.title("test".yellow())
.title_style(Style::new().green().on_red())
.title_alignment(alignment)
.render(buffer.area, &mut buffer);
let mut expected_buffer = Buffer::with_lines(vec!["test"]);
expected_buffer.set_style(Rect::new(0, 0, 4, 1), Style::new().yellow().on_red());
assert_buffer_eq!(buffer, expected_buffer);
}
}
}