Struct ratatui::text::Span

source ·
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>

source

pub fn raw<T>(content: T) -> Span<'a>where T: Into<Cow<'a, str>>,

Create a span with no style.

Examples
Span::raw("My text");
Span::raw(String::from("My text"));
source

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);
source

pub fn width(&self) -> usize

Returns the width of the content held by this span.

source

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>>()
);

Trait Implementations§

source§

impl<'a> Clone for Span<'a>

source§

fn clone(&self) -> Span<'a>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a> Debug for Span<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a> From<&'a str> for Span<'a>

source§

fn from(s: &'a str) -> Span<'a>

Converts to this type from the input type.
source§

impl<'a> From<Span<'a>> for Spans<'a>

source§

fn from(span: Span<'a>) -> Spans<'a>

Converts to this type from the input type.
source§

impl<'a> From<Span<'a>> for Text<'a>

source§

fn from(span: Span<'a>) -> Text<'a>

Converts to this type from the input type.
source§

impl<'a> From<String> for Span<'a>

source§

fn from(s: String) -> Span<'a>

Converts to this type from the input type.
source§

impl<'a> PartialEq<Span<'a>> for Span<'a>

source§

fn eq(&self, other: &Span<'a>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> Eq for Span<'a>

source§

impl<'a> StructuralEq for Span<'a>

source§

impl<'a> StructuralPartialEq for Span<'a>

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for Span<'a>

§

impl<'a> Send for Span<'a>

§

impl<'a> Sync for Span<'a>

§

impl<'a> Unpin for Span<'a>

§

impl<'a> UnwindSafe for Span<'a>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.