mirror of
https://github.com/ratatui-org/ratatui
synced 2025-02-16 14:08:44 +00:00
fix: only apply style to first line when rendering a Line
(#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.
This commit is contained in:
parent
b304bb99bd
commit
b344f95b7c
1 changed files with 12 additions and 0 deletions
|
@ -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;
|
||||
|
@ -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));
|
||||
|
|
Loading…
Add table
Reference in a new issue