Skip to content

Commit

Permalink
fix: ignore newlines in Span's Display impl (ratatui#1270)
Browse files Browse the repository at this point in the history
  • Loading branch information
SUPERCILEX authored and joshka committed Oct 14, 2024
1 parent bd4e466 commit 29d4205
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/text/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,13 @@ mod tests {
line.render_ref(buf.area, &mut buf);
assert_eq!(buf, Buffer::with_lines([expected]));
}

#[test]
fn render_with_newlines() {
let mut buf = Buffer::empty(Rect::new(0, 0, 11, 1));
Line::from("Hello\nworld!").render(Rect::new(0, 0, 11, 1), &mut buf);
assert_eq!(buf, Buffer::with_lines(["Helloworld!"]));
}
}

mod iterators {
Expand Down
22 changes: 21 additions & 1 deletion src/text/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,10 @@ impl<T: fmt::Display> ToSpan for T {

impl fmt::Display for Span<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.content, f)
for line in self.content.lines() {
fmt::Display::fmt(line, f)?;
}
Ok(())
}
}

Expand Down Expand Up @@ -566,6 +569,8 @@ mod tests {
assert_eq!(Span::raw("").width(), 0);
assert_eq!(Span::raw("test").width(), 4);
assert_eq!(Span::raw("test content").width(), 12);
// Needs reconsideration: https://github.com/ratatui-org/ratatui/issues/1271
assert_eq!(Span::raw("test\ncontent").width(), 12);
}

#[test]
Expand All @@ -579,13 +584,20 @@ mod tests {
assert_eq!(stylized.content, Cow::Borrowed("test content"));
assert_eq!(stylized.style, Style::new().green().on_yellow().bold());
}

#[test]
fn display_span() {
let span = Span::raw("test content");
assert_eq!(format!("{span}"), "test content");
assert_eq!(format!("{span:.4}"), "test");
}

#[test]
fn display_newline_span() {
let span = Span::raw("test\ncontent");
assert_eq!(format!("{span}"), "testcontent");
}

#[test]
fn display_styled_span() {
let stylized_span = Span::styled("stylized test content", Style::new().green());
Expand Down Expand Up @@ -764,6 +776,14 @@ mod tests {
[Cell::new("a"), Cell::new("b"), Cell::new("c\u{200B}")]
);
}

#[test]
fn render_with_newlines() {
let span = Span::raw("a\nb");
let mut buf = Buffer::empty(Rect::new(0, 0, 2, 1));
span.render(buf.area, &mut buf);
assert_eq!(buf.content(), [Cell::new("a"), Cell::new("b")]);
}
}

/// Regression test for <https://github.com/ratatui-org/ratatui/issues/1160> One line contains
Expand Down
3 changes: 1 addition & 2 deletions src/widgets/chart.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::cmp::max;

use strum::{Display, EnumString};
use unicode_width::UnicodeWidthStr;

use crate::{
layout::Flex,
Expand Down Expand Up @@ -817,7 +816,7 @@ impl<'a> Chart<'a> {
.as_ref()
.and_then(|labels| labels.first())
{
let first_label_width = first_x_label.content.width() as u16;
let first_label_width = first_x_label.width() as u16;
let width_left_of_y_axis = match self.x_axis.labels_alignment {
Alignment::Left => {
// The last character of the label should be below the Y-Axis when it exists,
Expand Down

0 comments on commit 29d4205

Please sign in to comment.