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:
Josh McKinney 2024-02-25 05:14:19 -08:00 committed by GitHub
parent b5bdde079e
commit 1cff511934
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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!");