Skip to content

Commit

Permalink
fix(paragraph): render Line::styled correctly inside a paragraph (#930)
Browse files Browse the repository at this point in the history
Renders the styled graphemes of the line instead of the contained spans.
  • Loading branch information
m4rch3n1ng committed Feb 6, 2024
1 parent 74a0511 commit 0dcdbea
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions src/widgets/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 0dcdbea

Please sign in to comment.