pub struct Span<'a> {
pub content: Cow<'a, str>,
pub style: Style,
}
Expand description
A string where all graphemes have the same style.
Fields§
§content: Cow<'a, str>
§style: Style
Implementations§
source§impl<'a> Span<'a>
impl<'a> Span<'a>
sourcepub fn styled<T>(content: T, style: Style) -> Span<'a>where
T: Into<Cow<'a, str>>,
pub fn styled<T>(content: T, style: Style) -> Span<'a>where T: Into<Cow<'a, str>>,
Create a span with a style.
Examples
let style = Style::default().fg(Color::Yellow).add_modifier(Modifier::ITALIC);
Span::styled("My text", style);
Span::styled(String::from("My text"), style);
sourcepub fn styled_graphemes(
&'a self,
base_style: Style
) -> impl Iterator<Item = StyledGrapheme<'a>>
pub fn styled_graphemes( &'a self, base_style: Style ) -> impl Iterator<Item = StyledGrapheme<'a>>
Returns an iterator over the graphemes held by this span.
base_style
is the Style
that will be patched with each grapheme Style
to get
the resulting Style
.
Examples
let style = Style::default().fg(Color::Yellow);
let span = Span::styled("Text", style);
let style = Style::default().fg(Color::Green).bg(Color::Black);
let styled_graphemes = span.styled_graphemes(style);
assert_eq!(
vec![
StyledGrapheme {
symbol: "T",
style: Style {
fg: Some(Color::Yellow),
bg: Some(Color::Black),
add_modifier: Modifier::empty(),
sub_modifier: Modifier::empty(),
},
},
StyledGrapheme {
symbol: "e",
style: Style {
fg: Some(Color::Yellow),
bg: Some(Color::Black),
add_modifier: Modifier::empty(),
sub_modifier: Modifier::empty(),
},
},
StyledGrapheme {
symbol: "x",
style: Style {
fg: Some(Color::Yellow),
bg: Some(Color::Black),
add_modifier: Modifier::empty(),
sub_modifier: Modifier::empty(),
},
},
StyledGrapheme {
symbol: "t",
style: Style {
fg: Some(Color::Yellow),
bg: Some(Color::Black),
add_modifier: Modifier::empty(),
sub_modifier: Modifier::empty(),
},
},
],
styled_graphemes.collect::<Vec<StyledGrapheme>>()
);