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: various newline handling improvements #1270

Merged
merged 3 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions src/text/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,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 @@ -439,7 +439,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 @@ -562,6 +565,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);
joshka marked this conversation as resolved.
Show resolved Hide resolved
}

#[test]
Expand All @@ -575,13 +580,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 @@ -757,6 +769,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