Skip to content

Commit

Permalink
fix(block): Fixed title_style not rendered (#349) (#363)
Browse files Browse the repository at this point in the history
Fixes #349
  • Loading branch information
Valentin271 committed Aug 4, 2023
1 parent 181706c commit 49a82e0
Showing 1 changed file with 71 additions and 3 deletions.
74 changes: 71 additions & 3 deletions src/widgets/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
});
Expand All @@ -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,
);
});
Expand All @@ -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,
);
});
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 49a82e0

Please sign in to comment.