pub struct Span<'a> {
pub style: Style,
pub content: Cow<'a, str>,
}
Expand description
Represents a part of a line that is contiguous and where all characters share the same style.
A Span
is the smallest unit of text that can be styled. It is usually combined in the Line
type to represent a line of text where each Span
may have a different style.
§Constructor Methods
Span::default
creates an span with empty content and the default style.Span::raw
creates an span with the specified content and the default style.Span::styled
creates an span with the specified content and style.
§Setter Methods
These methods are fluent setters. They return a new Span
with the specified property set.
Span::content
sets the content of the span.Span::style
sets the style of the span.
§Other Methods
Span::patch_style
patches the style of the span, adding modifiers from the given style.Span::reset_style
resets the style of the span.Span::width
returns the unicode width of the content held by this span.Span::styled_graphemes
returns an iterator over the graphemes held by this span.
§Examples
A Span
with style
set to Style::default()
can be created from a &str
, a String
, or
any type convertible to Cow<str>
.
use ratatui::text::Span;
let span = Span::raw("test content");
let span = Span::raw(String::from("test content"));
let span = Span::from("test content");
let span = Span::from(String::from("test content"));
let span: Span = "test content".into();
let span: Span = String::from("test content").into();
Styled spans can be created using Span::styled
or by converting strings using methods from
the Stylize
trait.
use ratatui::{
style::{Style, Stylize},
text::Span,
};
let span = Span::styled("test content", Style::new().green());
let span = Span::styled(String::from("test content"), Style::new().green());
// using Stylize trait shortcuts
let span = "test content".green();
let span = String::from("test content").green();
Span
implements the Styled
trait, which allows it to be styled using the shortcut methods
defined in the Stylize
trait.
use ratatui::{style::Stylize, text::Span};
let span = Span::raw("test content").green().on_yellow().italic();
let span = Span::raw(String::from("test content"))
.green()
.on_yellow()
.italic();
Span
implements the Widget
trait, which allows it to be rendered to a Buffer
. Usually
apps will use the Paragraph
widget instead of rendering Span
directly, as it handles text
wrapping and alignment for you.
use ratatui::{style::Stylize, Frame};
frame.render_widget("test content".green().on_yellow().italic(), frame.area());
Fields§
§style: Style
The style of the span.
content: Cow<'a, str>
The content of the span as a Clone-on-write string.
Implementations§
Source§impl<'a> Span<'a>
impl<'a> Span<'a>
Sourcepub fn raw<T>(content: T) -> Self
pub fn raw<T>(content: T) -> Self
Create a span with the default style.
§Examples
use ratatui::text::Span;
Span::raw("test content");
Span::raw(String::from("test content"));
Sourcepub fn styled<T, S>(content: T, style: S) -> Self
pub fn styled<T, S>(content: T, style: S) -> Self
Create a span with the specified style.
content
accepts any type that is convertible to Cow<str>
(e.g. &str
, String
,
&String
, etc.).
style
accepts any type that is convertible to Style
(e.g. Style
, Color
, or
your own type that implements Into<Style>
).
§Examples
use ratatui::{
style::{Style, Stylize},
text::Span,
};
let style = Style::new().yellow().on_green().italic();
Span::styled("test content", style);
Span::styled(String::from("test content"), style);
Sourcepub fn style<S: Into<Style>>(self, style: S) -> Self
pub fn style<S: Into<Style>>(self, style: S) -> Self
Sets the style of the span.
This is a fluent setter method which must be chained or used as it consumes self
In contrast to Span::patch_style
, this method replaces the style of the span instead of
patching it.
style
accepts any type that is convertible to Style
(e.g. Style
, Color
, or
your own type that implements Into<Style>
).
§Examples
use ratatui::{
style::{Style, Stylize},
text::Span,
};
let mut span = Span::default().style(Style::new().green());
Sourcepub fn patch_style<S: Into<Style>>(self, style: S) -> Self
pub fn patch_style<S: Into<Style>>(self, style: S) -> Self
Patches the style of the Span, adding modifiers from the given style.
style
accepts any type that is convertible to Style
(e.g. Style
, Color
, or
your own type that implements Into<Style>
).
This is a fluent setter method which must be chained or used as it consumes self
§Example
use ratatui::{
style::{Style, Stylize},
text::Span,
};
let span = Span::styled("test content", Style::new().green().italic())
.patch_style(Style::new().red().on_yellow().bold());
assert_eq!(span.style, Style::new().red().on_yellow().italic().bold());
Sourcepub fn reset_style(self) -> Self
pub fn reset_style(self) -> Self
Resets the style of the Span.
This is Equivalent to calling patch_style(Style::reset())
.
This is a fluent setter method which must be chained or used as it consumes self
§Example
use ratatui::{
style::{Style, Stylize},
text::Span,
};
let span = Span::styled(
"Test Content",
Style::new().dark_gray().on_yellow().italic(),
)
.reset_style();
assert_eq!(span.style, Style::reset());
Sourcepub fn styled_graphemes<S: Into<Style>>(
&'a self,
base_style: S,
) -> impl Iterator<Item = StyledGrapheme<'a>>
pub fn styled_graphemes<S: Into<Style>>( &'a self, base_style: S, ) -> 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 the Span
’s style
to get the
resulting Style
.
base_style
accepts any type that is convertible to Style
(e.g. Style
, Color
,
or your own type that implements Into<Style>
).
§Example
use std::iter::Iterator;
use ratatui::{
style::{Style, Stylize},
text::{Span, StyledGrapheme},
};
let span = Span::styled("Test", Style::new().green().italic());
let style = Style::new().red().on_yellow();
assert_eq!(
span.styled_graphemes(style)
.collect::<Vec<StyledGrapheme>>(),
vec![
StyledGrapheme::new("T", Style::new().green().on_yellow().italic()),
StyledGrapheme::new("e", Style::new().green().on_yellow().italic()),
StyledGrapheme::new("s", Style::new().green().on_yellow().italic()),
StyledGrapheme::new("t", Style::new().green().on_yellow().italic()),
],
);
Sourcepub fn into_left_aligned_line(self) -> Line<'a>
pub fn into_left_aligned_line(self) -> Line<'a>
pub fn to_left_aligned_line(self) -> Line<'a>
Sourcepub fn into_centered_line(self) -> Line<'a>
pub fn into_centered_line(self) -> Line<'a>
pub fn to_centered_line(self) -> Line<'a>
Sourcepub fn into_right_aligned_line(self) -> Line<'a>
pub fn into_right_aligned_line(self) -> Line<'a>
pub fn to_right_aligned_line(self) -> Line<'a>
Trait Implementations§
Source§impl<'a> Add<Span<'a>> for Line<'a>
impl<'a> Add<Span<'a>> for Line<'a>
Adds a Span
to a Line
, returning a new Line
with the Span
added.
Source§impl<'a> AddAssign<Span<'a>> for Line<'a>
impl<'a> AddAssign<Span<'a>> for Line<'a>
Source§fn add_assign(&mut self, rhs: Span<'a>)
fn add_assign(&mut self, rhs: Span<'a>)
+=
operation. Read moreSource§impl<'a> Extend<Span<'a>> for Line<'a>
impl<'a> Extend<Span<'a>> for Line<'a>
Source§fn extend<T: IntoIterator<Item = Span<'a>>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = Span<'a>>>(&mut self, iter: T)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl WidgetRef for Span<'_>
impl WidgetRef for Span<'_>
Source§fn render_ref(&self, area: Rect, buf: &mut Buffer)
fn render_ref(&self, area: Rect, buf: &mut Buffer)
impl<'a> Eq for Span<'a>
impl<'a> StructuralPartialEq for Span<'a>
Auto Trait Implementations§
impl<'a> Freeze for Span<'a>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<'a, T, U> Stylize<'a, T> for Uwhere
U: Styled<Item = T>,
impl<'a, T, U> Stylize<'a, T> for Uwhere
U: Styled<Item = T>,
fn bg<C>(self, color: C) -> T
fn fg<C>(self, color: C) -> T
fn add_modifier(self, modifier: Modifier) -> T
fn remove_modifier(self, modifier: Modifier) -> T
fn reset(self) -> T
Source§fn on_magenta(self) -> T
fn on_magenta(self) -> T
magenta
.Source§fn on_dark_gray(self) -> T
fn on_dark_gray(self) -> T
dark_gray
.Source§fn on_light_red(self) -> T
fn on_light_red(self) -> T
light_red
.Source§fn light_green(self) -> T
fn light_green(self) -> T
light_green
.Source§fn on_light_green(self) -> T
fn on_light_green(self) -> T
light_green
.Source§fn light_yellow(self) -> T
fn light_yellow(self) -> T
light_yellow
.Source§fn on_light_yellow(self) -> T
fn on_light_yellow(self) -> T
light_yellow
.Source§fn light_blue(self) -> T
fn light_blue(self) -> T
light_blue
.Source§fn on_light_blue(self) -> T
fn on_light_blue(self) -> T
light_blue
.Source§fn light_magenta(self) -> T
fn light_magenta(self) -> T
light_magenta
.Source§fn on_light_magenta(self) -> T
fn on_light_magenta(self) -> T
light_magenta
.Source§fn light_cyan(self) -> T
fn light_cyan(self) -> T
light_cyan
.Source§fn on_light_cyan(self) -> T
fn on_light_cyan(self) -> T
light_cyan
.Source§fn not_italic(self) -> T
fn not_italic(self) -> T
ITALIC
modifier.Source§fn underlined(self) -> T
fn underlined(self) -> T
UNDERLINED
modifier.Source§fn not_underlined(self) -> T
fn not_underlined(self) -> T
UNDERLINED
modifier.Source§fn slow_blink(self) -> T
fn slow_blink(self) -> T
SLOW_BLINK
modifier.Source§fn not_slow_blink(self) -> T
fn not_slow_blink(self) -> T
SLOW_BLINK
modifier.Source§fn rapid_blink(self) -> T
fn rapid_blink(self) -> T
RAPID_BLINK
modifier.Source§fn not_rapid_blink(self) -> T
fn not_rapid_blink(self) -> T
RAPID_BLINK
modifier.Source§fn not_reversed(self) -> T
fn not_reversed(self) -> T
REVERSED
modifier.HIDDEN
modifier.HIDDEN
modifier.Source§fn crossed_out(self) -> T
fn crossed_out(self) -> T
CROSSED_OUT
modifier.Source§fn not_crossed_out(self) -> T
fn not_crossed_out(self) -> T
CROSSED_OUT
modifier.Source§impl<T> ToCompactString for Twhere
T: Display,
impl<T> ToCompactString for Twhere
T: Display,
Source§fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
fn try_to_compact_string(&self) -> Result<CompactString, ToCompactStringError>
ToCompactString::to_compact_string()
Read moreSource§fn to_compact_string(&self) -> CompactString
fn to_compact_string(&self) -> CompactString
CompactString
. Read more