mirror of
https://github.com/ratatui-org/ratatui
synced 2024-11-25 14:10:31 +00:00
feat(line): impl Styled for Line (#968)
This adds `FromIterator` impls for `Line` and `Text` that allow creating `Line` and `Text` instances from iterators of `Span` and `Line` instances, respectively. ```rust let line = Line::from_iter(vec!["Hello".blue(), " world!".green()]); let line: Line = iter::once("Hello".blue()) .chain(iter::once(" world!".green())) .collect(); let text = Text::from_iter(vec!["The first line", "The second line"]); let text: Text = iter::once("The first line") .chain(iter::once("The second line")) .collect(); ```
This commit is contained in:
parent
b5bdde079e
commit
1cff511934
1 changed files with 22 additions and 0 deletions
|
@ -495,6 +495,18 @@ impl std::fmt::Display for Line<'_> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a> Styled for Line<'a> {
|
||||
type Item = Line<'a>;
|
||||
|
||||
fn style(&self) -> Style {
|
||||
self.style
|
||||
}
|
||||
|
||||
fn set_style<S: Into<Style>>(self, style: S) -> Self::Item {
|
||||
self.style(style)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::iter;
|
||||
|
@ -612,6 +624,16 @@ mod tests {
|
|||
assert_eq!(Style::reset(), line.style);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stylize() {
|
||||
assert_eq!(Line::default().green().style, Color::Green.into());
|
||||
assert_eq!(
|
||||
Line::default().on_green().style,
|
||||
Style::new().bg(Color::Green)
|
||||
);
|
||||
assert_eq!(Line::default().italic().style, Modifier::ITALIC.into());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_string() {
|
||||
let s = String::from("Hello, world!");
|
||||
|
|
Loading…
Reference in a new issue