Skip to content

Commit efa965e

Browse files
authored
fix(line): remove newlines when converting strings to Lines (#1191)
`Line::from("a\nb")` now returns a line with two `Span`s instead of 1 Fixes: #1111
1 parent 127d706 commit efa965e

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/text/line.rs

+18-7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ use crate::{prelude::*, style::Styled, text::StyledGrapheme};
1212
/// text. When a [`Line`] is rendered, it is rendered as a single line of text, with each [`Span`]
1313
/// being rendered in order (left to right).
1414
///
15+
/// Any newlines in the content are removed when creating a [`Line`] using the constructor or
16+
/// conversion methods.
17+
///
1518
/// # Constructor Methods
1619
///
1720
/// - [`Line::default`] creates a line with empty content and the default style.
@@ -502,13 +505,13 @@ impl<'a> IntoIterator for &'a mut Line<'a> {
502505

503506
impl<'a> From<String> for Line<'a> {
504507
fn from(s: String) -> Self {
505-
Self::from(vec![Span::from(s)])
508+
Self::raw(s)
506509
}
507510
}
508511

509512
impl<'a> From<&'a str> for Line<'a> {
510513
fn from(s: &'a str) -> Self {
511-
Self::from(vec![Span::from(s)])
514+
Self::raw(s)
512515
}
513516
}
514517

@@ -803,14 +806,22 @@ mod tests {
803806
fn from_string() {
804807
let s = String::from("Hello, world!");
805808
let line = Line::from(s);
806-
assert_eq!(vec![Span::from("Hello, world!")], line.spans);
809+
assert_eq!(line.spans, vec![Span::from("Hello, world!")]);
810+
811+
let s = String::from("Hello\nworld!");
812+
let line = Line::from(s);
813+
assert_eq!(line.spans, vec![Span::from("Hello"), Span::from("world!")]);
807814
}
808815

809816
#[test]
810817
fn from_str() {
811818
let s = "Hello, world!";
812819
let line = Line::from(s);
813-
assert_eq!(vec![Span::from("Hello, world!")], line.spans);
820+
assert_eq!(line.spans, vec![Span::from("Hello, world!")]);
821+
822+
let s = "Hello\nworld!";
823+
let line = Line::from(s);
824+
assert_eq!(line.spans, vec![Span::from("Hello"), Span::from("world!")]);
814825
}
815826

816827
#[test]
@@ -820,7 +831,7 @@ mod tests {
820831
Span::styled(" world!", Style::default().fg(Color::Green)),
821832
];
822833
let line = Line::from(spans.clone());
823-
assert_eq!(spans, line.spans);
834+
assert_eq!(line.spans, spans);
824835
}
825836

826837
#[test]
@@ -853,7 +864,7 @@ mod tests {
853864
fn from_span() {
854865
let span = Span::styled("Hello, world!", Style::default().fg(Color::Yellow));
855866
let line = Line::from(span.clone());
856-
assert_eq!(vec![span], line.spans);
867+
assert_eq!(line.spans, vec![span],);
857868
}
858869

859870
#[test]
@@ -863,7 +874,7 @@ mod tests {
863874
Span::styled(" world!", Style::default().fg(Color::Green)),
864875
]);
865876
let s: String = line.into();
866-
assert_eq!("Hello, world!", s);
877+
assert_eq!(s, "Hello, world!");
867878
}
868879

869880
#[test]

0 commit comments

Comments
 (0)