pub struct Line<'a> {
pub style: Style,
pub alignment: Option<Alignment>,
pub spans: Vec<Span<'a>>,
}Expand description
A line of text, consisting of one or more Spans.
Lines are used wherever text is displayed in the terminal and represent a single line of
text. When a Line is rendered, it is rendered as a single line of text, with each Span
being rendered in order (left to right).
Any newlines in the content are removed when creating a Line using the constructor or
conversion methods.
§Constructor Methods
Line::defaultcreates a line with empty content and the default style.Line::rawcreates a line with the given content and the default style.Line::styledcreates a line with the given content and style.
§Conversion Methods
Line::fromcreates aLinefrom aString.Line::fromcreates aLinefrom a&str.Line::fromcreates aLinefrom aVecofSpans.Line::fromcreates aLinefrom singleSpan.String::fromconverts a line into aString.Line::from_itercreates a line from an iterator of items that are convertible toSpan.
§Setter Methods
These methods are fluent setters. They return a Line with the property set.
Line::spanssets the content of the line.Line::stylesets the style of the line.Line::alignmentsets the alignment of the line.Line::left_alignedsets the alignment of the line toAlignment::Left.Line::centeredsets the alignment of the line toAlignment::Center.Line::right_alignedsets the alignment of the line toAlignment::Right.
§Iteration Methods
Line::iterreturns an iterator over the spans of this line.Line::iter_mutreturns a mutable iterator over the spans of this line.Line::into_iterreturns an iterator over the spans of this line.
§Other Methods
Line::patch_stylepatches the style of the line, adding modifiers from the given style.Line::reset_styleresets the style of the line.Line::widthreturns the unicode width of the content held by this line.Line::styled_graphemesreturns an iterator over the graphemes held by this line.Line::push_spanadds a span to the line.
§Compatibility Notes
Before v0.26.0, Line did not have a style field and instead relied on only the styles that
were set on each Span contained in the spans field. The Line::patch_style method was
the only way to set the overall style for individual lines. For this reason, this field may not
be supported yet by all widgets (outside of the ratatui crate itself).
§Examples
§Creating Lines
Lines can be created from Spans, Strings, and &strs. They can be styled with a
Style.
use ratatui::{
style::{Color, Modifier, Style, Stylize},
text::{Line, Span},
};
let style = Style::new().yellow();
let line = Line::raw("Hello, world!").style(style);
let line = Line::styled("Hello, world!", style);
let line = Line::styled("Hello, world!", (Color::Yellow, Modifier::BOLD));
let line = Line::from("Hello, world!");
let line = Line::from(String::from("Hello, world!"));
let line = Line::from(vec![
Span::styled("Hello", Style::new().blue()),
Span::raw(" world!"),
]);§Styling Lines
The line’s Style is used by the rendering widget to determine how to style the line. Each
Span in the line will be styled with the Style of the line, and then with its own
Style. If the line is longer than the available space, the style is applied to the entire
line, and the line is truncated. Line also implements Styled which means you can use the
methods of the Stylize trait.
use ratatui::{
style::{Color, Modifier, Style, Stylize},
text::Line,
};
let line = Line::from("Hello world!").style(Style::new().yellow().italic());
let line = Line::from("Hello world!").style(Color::Yellow);
let line = Line::from("Hello world!").style((Color::Yellow, Color::Black));
let line = Line::from("Hello world!").style((Color::Yellow, Modifier::ITALIC));
let line = Line::from("Hello world!").yellow().italic();§Aligning Lines
The line’s Alignment is used by the rendering widget to determine how to align the line
within the available space. If the line is longer than the available space, the alignment is
ignored and the line is truncated.
use ratatui::{layout::Alignment, text::Line};
let line = Line::from("Hello world!").alignment(Alignment::Right);
let line = Line::from("Hello world!").centered();
let line = Line::from("Hello world!").left_aligned();
let line = Line::from("Hello world!").right_aligned();§Rendering Lines
Line implements the Widget trait, which means it can be rendered to a Buffer.
use ratatui::{
buffer::Buffer,
layout::Rect,
style::{Style, Stylize},
text::Line,
widgets::Widget,
Frame,
};
// in another widget's render method
let line = Line::from("Hello world!").style(Style::new().yellow().italic());
line.render(area, buf);
// in a terminal.draw closure
let line = Line::from("Hello world!").style(Style::new().yellow().italic());
frame.render_widget(line, area);§Rendering Lines with a Paragraph widget
Usually apps will use the Paragraph widget instead of rendering a Line directly as it
provides more functionality.
use ratatui::{
buffer::Buffer,
layout::Rect,
style::Stylize,
text::Line,
widgets::{Paragraph, Widget, Wrap},
};
let line = Line::from("Hello world!").yellow().italic();
Paragraph::new(line)
.wrap(Wrap { trim: true })
.render(area, buf);Fields§
§style: StyleThe style of this line of text.
alignment: Option<Alignment>The alignment of this line of text.
spans: Vec<Span<'a>>The spans that make up this line of text.
Implementations§
Source§impl<'a> Line<'a>
impl<'a> Line<'a>
Sourcepub fn raw<T>(content: T) -> Self
pub fn raw<T>(content: T) -> Self
Create a line with the default style.
content can be any type that is convertible to Cow<str> (e.g. &str, String,
Cow<str>, or your own type that implements Into<Cow<str>>).
A Line can specify a Style, which will be applied before the style of each Span
in the line.
Any newlines in the content are removed.
§Examples
use std::borrow::Cow;
use ratatui::text::Line;
Line::raw("test content");
Line::raw(String::from("test content"));
Line::raw(Cow::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 line with the given style.
content can be any type that is convertible to Cow<str> (e.g. &str, String,
Cow<str>, or your own type that implements Into<Cow<str>>).
style accepts any type that is convertible to Style (e.g. Style, Color, or
your own type that implements Into<Style>).
§Examples
Any newlines in the content are removed.
use std::borrow::Cow;
use ratatui::{
style::{Style, Stylize},
text::Line,
};
let style = Style::new().yellow().italic();
Line::styled("My text", style);
Line::styled(String::from("My text"), style);
Line::styled(Cow::from("test content"), style);Sourcepub fn spans<I>(self, spans: I) -> Self
pub fn spans<I>(self, spans: I) -> Self
Sets the spans of this line of text.
spans accepts any iterator that yields items that are convertible to Span (e.g.
&str, String, Span, or your own type that implements Into<Span>).
§Examples
use ratatui::{style::Stylize, text::Line};
let line = Line::default().spans(vec!["Hello".blue(), " world!".green()]);
let line = Line::default().spans([1, 2, 3].iter().map(|i| format!("Item {}", i)));Sourcepub fn style<S: Into<Style>>(self, style: S) -> Self
pub fn style<S: Into<Style>>(self, style: S) -> Self
Sets the style of this line of text.
Defaults to Style::default().
Note: This field was added in v0.26.0. Prior to that, the style of a line was determined
only by the style of each Span contained in the line. For this reason, this field may
not be supported by all widgets (outside of the ratatui crate itself).
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::Line,
};
let mut line = Line::from("foo").style(Style::new().red());Sourcepub fn alignment(self, alignment: Alignment) -> Self
pub fn alignment(self, alignment: Alignment) -> Self
Sets the target alignment for this line of text.
Defaults to: None, meaning the alignment is determined by the rendering widget.
Setting the alignment of a Line generally overrides the alignment of its
parent Text or Widget.
§Examples
use ratatui::{layout::Alignment, text::Line};
let mut line = Line::from("Hi, what's up?");
assert_eq!(None, line.alignment);
assert_eq!(
Some(Alignment::Right),
line.alignment(Alignment::Right).alignment
)Sourcepub fn left_aligned(self) -> Self
pub fn left_aligned(self) -> Self
Left-aligns this line of text.
Convenience shortcut for Line::alignment(Alignment::Left).
Setting the alignment of a Line generally overrides the alignment of its
parent Text or Widget, with the default alignment being inherited from the parent.
§Examples
use ratatui::text::Line;
let line = Line::from("Hi, what's up?").left_aligned();Sourcepub fn centered(self) -> Self
pub fn centered(self) -> Self
Center-aligns this line of text.
Convenience shortcut for Line::alignment(Alignment::Center).
Setting the alignment of a Line generally overrides the alignment of its
parent Text or Widget, with the default alignment being inherited from the parent.
§Examples
use ratatui::text::Line;
let line = Line::from("Hi, what's up?").centered();Sourcepub fn right_aligned(self) -> Self
pub fn right_aligned(self) -> Self
Right-aligns this line of text.
Convenience shortcut for Line::alignment(Alignment::Right).
Setting the alignment of a Line generally overrides the alignment of its
parent Text or Widget, with the default alignment being inherited from the parent.
§Examples
use ratatui::text::Line;
let line = Line::from("Hi, what's up?").right_aligned();Sourcepub fn width(&self) -> usize
pub fn width(&self) -> usize
Returns the width of the underlying string.
§Examples
use ratatui::{style::Stylize, text::Line};
let line = Line::from(vec!["Hello".blue(), " world!".green()]);
assert_eq!(12, line.width());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 line.
base_style is the Style that will be patched with each grapheme 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>).
§Examples
use std::iter::Iterator;
use ratatui::{
style::{Color, Style},
text::{Line, StyledGrapheme},
};
let line = Line::styled("Text", Style::default().fg(Color::Yellow));
let style = Style::default().fg(Color::Green).bg(Color::Black);
assert_eq!(
line.styled_graphemes(style)
.collect::<Vec<StyledGrapheme>>(),
vec![
StyledGrapheme::new("T", Style::default().fg(Color::Yellow).bg(Color::Black)),
StyledGrapheme::new("e", Style::default().fg(Color::Yellow).bg(Color::Black)),
StyledGrapheme::new("x", Style::default().fg(Color::Yellow).bg(Color::Black)),
StyledGrapheme::new("t", Style::default().fg(Color::Yellow).bg(Color::Black)),
]
);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 this Line, adding modifiers from the given style.
This is useful for when you want to apply a style to a line that already has some styling.
In contrast to Line::style, this method will not overwrite the existing style, but
instead will add the given style’s modifiers to this Line’s 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
§Examples
use ratatui::{
style::{Color, Modifier},
text::Line,
};
let line = Line::styled("My text", Modifier::ITALIC);
let styled_line = Line::styled("My text", (Color::Yellow, Modifier::ITALIC));
assert_eq!(styled_line, line.patch_style(Color::Yellow));Sourcepub fn reset_style(self) -> Self
pub fn reset_style(self) -> Self
Resets the style of this Line.
Equivalent to calling patch_style(Style::reset()).
This is a fluent setter method which must be chained or used as it consumes self
§Examples
use ratatui::{
style::{Style, Stylize},
text::Line,
};
let line = Line::styled("My text", style);
assert_eq!(Style::reset(), line.reset_style().style);Sourcepub fn iter_mut(&mut self) -> IterMut<'_, Span<'a>>
pub fn iter_mut(&mut self) -> IterMut<'_, Span<'a>>
Returns a mutable iterator over the spans of this line.
Sourcepub fn push_span<T: Into<Span<'a>>>(&mut self, span: T)
pub fn push_span<T: Into<Span<'a>>>(&mut self, span: T)
Adds a span to the line.
span can be any type that is convertible into a Span. For example, you can pass a
&str, a String, or a Span.
§Examples
use ratatui::text::{Line, Span};
let mut line = Line::from("Hello, ");
line.push_span(Span::raw("world!"));
line.push_span(" How are you?");Trait Implementations§
Source§impl<'a> Add<Span<'a>> for Line<'a>
Adds a Span to a Line, returning a new Line with the Span added.
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> Add for Line<'a>
Adds two Lines together, returning a new Text with the contents of the two Lines.
impl<'a> Add for Line<'a>
Adds two Lines together, returning a new Text with the contents of the two Lines.
Source§impl<'a> AddAssign<Line<'a>> for Text<'a>
impl<'a> AddAssign<Line<'a>> for Text<'a>
Source§fn add_assign(&mut self, line: Line<'a>)
fn add_assign(&mut self, line: Line<'a>)
+= operation. Read moreSource§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<'a, T> FromIterator<T> for Line<'a>
impl<'a, T> FromIterator<T> for Line<'a>
Source§fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
Source§impl<'a> IntoIterator for &'a Line<'a>
impl<'a> IntoIterator for &'a Line<'a>
Source§impl<'a> IntoIterator for &'a mut Line<'a>
impl<'a> IntoIterator for &'a mut Line<'a>
Source§impl<'a> IntoIterator for Line<'a>
impl<'a> IntoIterator for Line<'a>
Source§impl WidgetRef for Line<'_>
impl WidgetRef for Line<'_>
Source§fn render_ref(&self, area: Rect, buf: &mut Buffer)
fn render_ref(&self, area: Rect, buf: &mut Buffer)
impl<'a> Eq for Line<'a>
impl<'a> StructuralPartialEq for Line<'a>
Auto Trait Implementations§
impl<'a> Freeze for Line<'a>
impl<'a> RefUnwindSafe for Line<'a>
impl<'a> Send for Line<'a>
impl<'a> Sync for Line<'a>
impl<'a> Unpin for Line<'a>
impl<'a> UnwindSafe for Line<'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