mirror of
https://github.com/ratatui-org/ratatui
synced 2025-02-16 14:08:44 +00:00
fix: ignore newlines in Span's Display impl (#1270)
This commit is contained in:
parent
c2d38509b4
commit
29c8c84fd0
3 changed files with 29 additions and 3 deletions
|
@ -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 {
|
||||
|
|
|
@ -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(())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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]
|
||||
|
@ -579,6 +584,7 @@ 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");
|
||||
|
@ -586,6 +592,12 @@ mod tests {
|
|||
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());
|
||||
|
@ -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
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
use std::cmp::max;
|
||||
|
||||
use strum::{Display, EnumString};
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
use crate::{
|
||||
layout::Flex,
|
||||
|
@ -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,
|
||||
|
|
Loading…
Add table
Reference in a new issue