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:
@@ -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!");
|
||||
|
||||
Reference in New Issue
Block a user