Skip to content

Commit 1cff511

Browse files
authored
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(); ```
1 parent b5bdde0 commit 1cff511

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/text/line.rs

+22
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,18 @@ impl std::fmt::Display for Line<'_> {
495495
}
496496
}
497497

498+
impl<'a> Styled for Line<'a> {
499+
type Item = Line<'a>;
500+
501+
fn style(&self) -> Style {
502+
self.style
503+
}
504+
505+
fn set_style<S: Into<Style>>(self, style: S) -> Self::Item {
506+
self.style(style)
507+
}
508+
}
509+
498510
#[cfg(test)]
499511
mod tests {
500512
use std::iter;
@@ -612,6 +624,16 @@ mod tests {
612624
assert_eq!(Style::reset(), line.style);
613625
}
614626

627+
#[test]
628+
fn stylize() {
629+
assert_eq!(Line::default().green().style, Color::Green.into());
630+
assert_eq!(
631+
Line::default().on_green().style,
632+
Style::new().bg(Color::Green)
633+
);
634+
assert_eq!(Line::default().italic().style, Modifier::ITALIC.into());
635+
}
636+
615637
#[test]
616638
fn from_string() {
617639
let s = String::from("Hello, world!");

0 commit comments

Comments
 (0)