Skip to content

Commit

Permalink
fix: render of &str and String doesn't respect area.width (#1177)
Browse files Browse the repository at this point in the history
  • Loading branch information
thscharler committed Jun 15, 2024
1 parent 7d175f8 commit 4bfdc15
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub use self::{
table::{Cell, HighlightSpacing, Row, Table, TableState},
tabs::Tabs,
};
use crate::{buffer::Buffer, layout::Rect};
use crate::{buffer::Buffer, layout::Rect, style::Style};

/// A `Widget` is a type that can be drawn on a [`Buffer`] in a given [`Rect`].
///
Expand Down Expand Up @@ -444,7 +444,7 @@ impl Widget for &str {
/// [`Rect`].
impl WidgetRef for &str {
fn render_ref(&self, area: Rect, buf: &mut Buffer) {
buf.set_string(area.x, area.y, self, crate::style::Style::default());
buf.set_stringn(area.x, area.y, self, area.width as usize, Style::new());
}
}

Expand All @@ -465,7 +465,7 @@ impl Widget for String {
/// without the need to give up ownership of the underlying text.
impl WidgetRef for String {
fn render_ref(&self, area: Rect, buf: &mut Buffer) {
buf.set_string(area.x, area.y, self, crate::style::Style::default());
buf.set_stringn(area.x, area.y, self, area.width as usize, Style::new());
}
}

Expand Down Expand Up @@ -650,6 +650,13 @@ mod tests {
assert_eq!(buf, Buffer::with_lines(["hello world "]));
}

#[rstest]
fn render_area(mut buf: Buffer) {
let area = Rect::new(buf.area.x, buf.area.y, 11, buf.area.height);
"hello world, just hello".render(area, &mut buf);
assert_eq!(buf, Buffer::with_lines(["hello world "]));
}

#[rstest]
fn render_ref(mut buf: Buffer) {
"hello world".render_ref(buf.area, &mut buf);
Expand Down Expand Up @@ -677,6 +684,13 @@ mod tests {
assert_eq!(buf, Buffer::with_lines(["hello world "]));
}

#[rstest]
fn render_area(mut buf: Buffer) {
let area = Rect::new(buf.area.x, buf.area.y, 11, buf.area.height);
String::from("hello world, just hello").render(area, &mut buf);
assert_eq!(buf, Buffer::with_lines(["hello world "]));
}

#[rstest]
fn render_ref(mut buf: Buffer) {
String::from("hello world").render_ref(buf.area, &mut buf);
Expand Down

0 comments on commit 4bfdc15

Please sign in to comment.