Skip to content

Commit

Permalink
feat(line): add Line::raw constructor (#511)
Browse files Browse the repository at this point in the history
* feat(line): add `Line::raw` constructor

There is already `Span::raw` and `Text::raw` methods
and this commit simply adds `Line::raw` method for symmetry.

Multi-line content is converted to multiple spans with the new line removed
  • Loading branch information
orhun authored Sep 22, 2023
1 parent c3155a2 commit d67fa2c
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/text/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,29 @@ pub struct Line<'a> {
}

impl<'a> Line<'a> {
/// Create a line with the default style.
///
/// # Examples
///
/// ```rust
/// # use ratatui::prelude::*;
/// Line::raw("test content");
/// Line::raw(String::from("test content"));
/// ```
pub fn raw<T>(content: T) -> Line<'a>
where
T: Into<Cow<'a, str>>,
{
Line {
spans: content
.into()
.lines()
.map(|v| Span::raw(v.to_string()))
.collect(),
alignment: None,
}
}

/// Create a line with a style.
///
/// # Examples
Expand Down Expand Up @@ -307,4 +330,15 @@ mod tests {
],
);
}

#[test]
fn raw_str() {
let line = Line::raw("test content");
assert_eq!(line.spans, vec![Span::raw("test content")]);
assert_eq!(line.alignment, None);

let line = Line::raw("a\nb");
assert_eq!(line.spans, vec![Span::raw("a"), Span::raw("b")]);
assert_eq!(line.alignment, None);
}
}

0 comments on commit d67fa2c

Please sign in to comment.