pub struct ListItem<'a> { /* private fields */ }
Expand description
A single item in a List
The item’s height is defined by the number of lines it contains. This can be queried using
ListItem::height
. Similarly, ListItem::width
will return the maximum width of all
lines.
You can set the style of an item with ListItem::style
or using the Stylize
trait.
This Style
will be combined with the Style
of the inner Text
. The Style
of the Text
will be added to the Style
of the ListItem
.
You can also align a ListItem
by aligning its underlying Text
and Line
s. For that,
see Text::alignment
and Line::alignment
. On a multiline Text
, one Line
can override
the alignment by setting it explicitly.
§Examples
You can create ListItem
s from simple &str
use ratatui::widgets::ListItem;
let item = ListItem::new("Item 1");
Anything that can be converted to Text
can be a ListItem
.
use ratatui::{text::Line, widgets::ListItem};
let item1: ListItem = "Item 1".into();
let item2: ListItem = Line::raw("Item 2").into();
A ListItem
styled with Stylize
use ratatui::{style::Stylize, widgets::ListItem};
let item = ListItem::new("Item 1").red().on_white();
If you need more control over the item’s style, you can explicitly style the underlying
Text
use ratatui::{
style::Stylize,
text::{Span, Text},
widgets::ListItem,
};
let mut text = Text::default();
text.extend(["Item".blue(), Span::raw(" "), "1".bold().red()]);
let item = ListItem::new(text);
A right-aligned ListItem
use ratatui::{text::Text, widgets::ListItem};
ListItem::new(Text::from("foo").right_aligned());
Implementations§
Source§impl<'a> ListItem<'a>
impl<'a> ListItem<'a>
Sourcepub fn new<T>(content: T) -> Self
pub fn new<T>(content: T) -> Self
Creates a new ListItem
The content
parameter accepts any value that can be converted into Text
.
§Examples
You can create ListItem
s from simple &str
use ratatui::widgets::ListItem;
let item = ListItem::new("Item 1");
Anything that can be converted to Text
can be a ListItem
.
use ratatui::{text::Line, widgets::ListItem};
let item1: ListItem = "Item 1".into();
let item2: ListItem = Line::raw("Item 2").into();
You can also create multiline items
use ratatui::widgets::ListItem;
let item = ListItem::new("Multi-line\nitem");
§See also
Sourcepub fn style<S: Into<Style>>(self, style: S) -> Self
pub fn style<S: Into<Style>>(self, style: S) -> Self
Sets the item style
style
accepts any type that is convertible to Style
(e.g. Style
, Color
, or
your own type that implements Into<Style>
).
This Style
can be overridden by the Style
of the Text
content.
This is a fluent setter method which must be chained or used as it consumes self
§Example
use ratatui::{
style::{Style, Stylize},
widgets::ListItem,
};
let item = ListItem::new("Item 1").style(Style::new().red().italic());
ListItem
also implements the Styled
trait, which means you can use style shorthands
from the Stylize
trait to set the style of the widget more
concisely.
use ratatui::{style::Stylize, widgets::ListItem};
let item = ListItem::new("Item 1").red().italic();
Sourcepub fn height(&self) -> usize
pub fn height(&self) -> usize
Returns the item height
§Examples
One line item
use ratatui::widgets::ListItem;
let item = ListItem::new("Item 1");
assert_eq!(item.height(), 1);
Two lines item (note the \n
)
use ratatui::widgets::ListItem;
let item = ListItem::new("Multi-line\nitem");
assert_eq!(item.height(), 2);
Trait Implementations§
impl<'a> Eq for ListItem<'a>
impl<'a> StructuralPartialEq for ListItem<'a>
Auto Trait Implementations§
impl<'a> Freeze for ListItem<'a>
impl<'a> RefUnwindSafe for ListItem<'a>
impl<'a> Send for ListItem<'a>
impl<'a> Sync for ListItem<'a>
impl<'a> Unpin for ListItem<'a>
impl<'a> UnwindSafe for ListItem<'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.