Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure that paragraph correctly renders styled text #992

Merged
merged 2 commits into from
Mar 22, 2024
Merged
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
25 changes: 24 additions & 1 deletion src/widgets/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,9 @@ impl Paragraph<'_> {
return;
}

buf.set_style(text_area, self.style);
let styled = self.text.iter().map(|line| {
let graphemes = line.styled_graphemes(self.style);
let graphemes = line.styled_graphemes(self.text.style);
let alignment = line.alignment.unwrap_or(self.alignment);
(graphemes, alignment)
});
Expand Down Expand Up @@ -1011,4 +1012,26 @@ mod test {
let p = Paragraph::new("Hello, world!").right_aligned();
assert_eq!(p.alignment, Alignment::Right);
}

/// Regression test for <https://github.com/ratatui-org/ratatui/issues/990>
///
/// This test ensures that paragraphs with a block and styled text are rendered correctly.
/// It has been simplified from the original issue but tests the same functionality.
#[test]
fn paragraph_block_text_style() {
let text = Text::styled("Styled text", Color::Green);
let paragraph = Paragraph::new(text).block(Block::bordered());

let mut buf = Buffer::empty(Rect::new(0, 0, 20, 3));
paragraph.render(Rect::new(0, 0, 20, 3), &mut buf);

let mut expected = Buffer::with_lines(vec![
"┌──────────────────┐",
"│Styled text │",
"└──────────────────┘",
]);
expected.set_style(Rect::new(1, 1, 11, 1), Style::default().fg(Color::Green));

assert_eq!(buf, expected);
}
}