Skip to content

Commit

Permalink
fix: only apply style to first line when rendering a Line (ratatui#…
Browse files Browse the repository at this point in the history
…1247)

A `Line` widget should only apply its style to the first line when
rendering and not the entire area. This is because the `Line` widget
should only render a single line of text. This commit fixes the issue by
clamping the area to a single line before rendering the text.
  • Loading branch information
joshka committed Oct 14, 2024
1 parent 04e521d commit 1377d9c
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/text/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,7 @@ impl WidgetRef for Line<'_> {
if area.is_empty() {
return;
}
let area = Rect { height: 1, ..area };
let line_width = self.width();
if line_width == 0 {
return;
Expand Down Expand Up @@ -1123,6 +1124,17 @@ mod tests {
assert_eq!(buf, expected);
}

#[test]
fn render_only_styles_first_line() {
let mut buf = Buffer::empty(Rect::new(0, 0, 20, 2));
hello_world().render(buf.area, &mut buf);
let mut expected = Buffer::with_lines(["Hello world! ", " "]);
expected.set_style(Rect::new(0, 0, 20, 1), ITALIC);
expected.set_style(Rect::new(0, 0, 6, 1), BLUE);
expected.set_style(Rect::new(6, 0, 6, 1), GREEN);
assert_eq!(buf, expected);
}

#[test]
fn render_truncates() {
let mut buf = Buffer::empty(Rect::new(0, 0, 10, 1));
Expand Down

0 comments on commit 1377d9c

Please sign in to comment.