fix(paragraph): render Line::styled correctly inside a paragraph (#930)

Renders the styled graphemes of the line instead of the contained spans.
This commit is contained in:
may 2024-02-06 18:42:17 +01:00 committed by GitHub
parent 74a051147a
commit 0dcdbea083
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -340,9 +340,7 @@ impl Paragraph<'_> {
}
let styled = self.text.iter().map(|line| {
let graphemes = line
.iter()
.flat_map(|span| span.styled_graphemes(self.style));
let graphemes = line.styled_graphemes(self.style);
let alignment = line.alignment.unwrap_or(self.alignment);
(graphemes, alignment)
});
@ -593,6 +591,40 @@ mod test {
);
}
#[test]
fn test_render_line_styled() {
let l0 = Line::raw("unformatted");
let l1 = Line::styled("bold text", Style::new().bold());
let l2 = Line::styled("cyan text", Style::new().cyan());
let l3 = Line::styled("dim text", Style::new().dim());
let paragraph = Paragraph::new(vec![l0, l1, l2, l3]);
let mut expected =
Buffer::with_lines(vec!["unformatted", "bold text", "cyan text", "dim text"]);
expected.set_style(Rect::new(0, 1, 9, 1), Style::new().bold());
expected.set_style(Rect::new(0, 2, 9, 1), Style::new().cyan());
expected.set_style(Rect::new(0, 3, 8, 1), Style::new().dim());
test_case(&paragraph, expected);
}
#[test]
fn test_render_line_spans_styled() {
let l0 = Line::default().spans(vec![
Span::styled("bold", Style::new().bold()),
Span::raw(" and "),
Span::styled("cyan", Style::new().cyan()),
]);
let l1 = Line::default().spans(vec![Span::raw("unformatted")]);
let paragraph = Paragraph::new(vec![l0, l1]);
let mut expected = Buffer::with_lines(vec!["bold and cyan", "unformatted"]);
expected.set_style(Rect::new(0, 0, 4, 1), Style::new().bold());
expected.set_style(Rect::new(9, 0, 4, 1), Style::new().cyan());
test_case(&paragraph, expected);
}
#[test]
fn test_render_paragraph_with_block_with_bottom_title_and_border() {
let block = Block::default()