Skip to content

Commit

Permalink
feat(line): impl Styled for Line (#968)
Browse files Browse the repository at this point in the history
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();
```
  • Loading branch information
joshka committed Feb 25, 2024
1 parent b5bdde0 commit 1cff511
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/text/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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!");
Expand Down

0 comments on commit 1cff511

Please sign in to comment.