pub struct List<'a> { /* private fields */ }Expand description
A widget to display several items among which one can be selected (optional)
A list is a collection of ListItems.
This is different from a Table because it does not handle columns, headers or footers and
the item’s height is automatically determined. A List can also be put in reverse order (i.e.
bottom to top) whereas a Table cannot.
List items can be aligned using Text::alignment, for more details see ListItem.
List implements Widget and so it can be drawn using
Frame::render_widget.
List is also a StatefulWidget, which means you can use it with ListState to allow
the user to scroll through items and select one of them.
See the list in the Examples directory for a more in depth example of the various configuration options and for how to handle state.
§Fluent setters
List::highlight_stylesets the style of the selected item.List::highlight_symbolsets the symbol to be displayed in front of the selected item.List::repeat_highlight_symbolsets whether to repeat the symbol and style over selected multi-line itemsList::directionsets the list direction
§Examples
use ratatui::{
layout::Rect,
style::{Style, Stylize},
widgets::{Block, List, ListDirection, ListItem},
Frame,
};
let items = ["Item 1", "Item 2", "Item 3"];
let list = List::new(items)
.block(Block::bordered().title("List"))
.style(Style::new().white())
.highlight_style(Style::new().italic())
.highlight_symbol(">>")
.repeat_highlight_symbol(true)
.direction(ListDirection::BottomToTop);
frame.render_widget(list, area);§Stateful example
use ratatui::{
layout::Rect,
style::{Style, Stylize},
widgets::{Block, List, ListState},
Frame,
};
// This should be stored outside of the function in your application state.
let mut state = ListState::default();
let items = ["Item 1", "Item 2", "Item 3"];
let list = List::new(items)
.block(Block::bordered().title("List"))
.highlight_style(Style::new().reversed())
.highlight_symbol(">>")
.repeat_highlight_symbol(true);
frame.render_stateful_widget(list, area, &mut state);In addition to List::new, any iterator whose element is convertible to ListItem can be
collected into List.
use ratatui::widgets::List;
(0..5).map(|i| format!("Item{i}")).collect::<List>();Implementations§
Source§impl<'a> List<'a>
impl<'a> List<'a>
Sourcepub fn new<T>(items: T) -> Self
pub fn new<T>(items: T) -> Self
Creates a new list from ListItems
The items parameter accepts any value that can be converted into an iterator of
Into<ListItem>. This includes arrays of &str or Vecs of Text.
§Example
From a slice of &str
use ratatui::widgets::List;
let list = List::new(["Item 1", "Item 2"]);From Text
use ratatui::{
style::{Style, Stylize},
text::Text,
widgets::List,
};
let list = List::new([
Text::styled("Item 1", Style::new().red()),
Text::styled("Item 2", Style::new().red()),
]);You can also create an empty list using the Default implementation and use the
List::items fluent setter.
use ratatui::widgets::List;
let empty_list = List::default();
let filled_list = empty_list.items(["Item 1"]);Sourcepub fn items<T>(self, items: T) -> Self
pub fn items<T>(self, items: T) -> Self
Set the items
The items parameter accepts any value that can be converted into an iterator of
Into<ListItem>. This includes arrays of &str or Vecs of Text.
This is a fluent setter method which must be chained or used as it consumes self.
§Example
use ratatui::widgets::List;
let list = List::default().items(["Item 1", "Item 2"]);Sourcepub fn block(self, block: Block<'a>) -> Self
pub fn block(self, block: Block<'a>) -> Self
Wraps the list with a custom Block widget.
The block parameter holds the specified Block to be created around the List
This is a fluent setter method which must be chained or used as it consumes self
§Examples
use ratatui::widgets::{Block, List};
let items = ["Item 1"];
let block = Block::bordered().title("List");
let list = List::new(items).block(block);Sourcepub fn style<S: Into<Style>>(self, style: S) -> Self
pub fn style<S: Into<Style>>(self, style: S) -> Self
Sets the base style of the widget
style accepts any type that is convertible to Style (e.g. Style, Color, or
your own type that implements Into<Style>).
All text rendered by the widget will use this style, unless overridden by Block::style,
ListItem::style, or the styles of the ListItem’s content.
This is a fluent setter method which must be chained or used as it consumes self
§Examples
use ratatui::{
style::{Style, Stylize},
widgets::List,
};
let items = ["Item 1"];
let list = List::new(items).style(Style::new().red().italic());List 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::List};
let items = ["Item 1"];
let list = List::new(items).red().italic();Sourcepub const fn highlight_symbol(self, highlight_symbol: &'a str) -> Self
pub const fn highlight_symbol(self, highlight_symbol: &'a str) -> Self
Set the symbol to be displayed in front of the selected item
By default there are no highlight symbol.
This is a fluent setter method which must be chained or used as it consumes self
§Examples
use ratatui::widgets::List;
let items = ["Item 1", "Item 2"];
let list = List::new(items).highlight_symbol(">>");Sourcepub fn highlight_style<S: Into<Style>>(self, style: S) -> Self
pub fn highlight_style<S: Into<Style>>(self, style: S) -> Self
Set the style of the selected item
style accepts any type that is convertible to Style (e.g. Style, Color, or
your own type that implements Into<Style>).
This style will be applied to the entire item, including the highlight symbol if it is displayed, and will override any style set on the item or on the individual cells.
This is a fluent setter method which must be chained or used as it consumes self
§Examples
use ratatui::{
style::{Style, Stylize},
widgets::List,
};
let items = ["Item 1", "Item 2"];
let list = List::new(items).highlight_style(Style::new().red().italic());Sourcepub const fn repeat_highlight_symbol(self, repeat: bool) -> Self
pub const fn repeat_highlight_symbol(self, repeat: bool) -> Self
Set whether to repeat the highlight symbol and style over selected multi-line items
This is false by default.
This is a fluent setter method which must be chained or used as it consumes self
Sourcepub const fn highlight_spacing(self, value: HighlightSpacing) -> Self
pub const fn highlight_spacing(self, value: HighlightSpacing) -> Self
Set when to show the highlight spacing
The highlight spacing is the spacing that is allocated for the selection symbol (if enabled) and is used to shift the list when an item is selected. This method allows you to configure when this spacing is allocated.
HighlightSpacing::Alwayswill always allocate the spacing, regardless of whether an item is selected or not. This means that the table will never change size, regardless of if an item is selected or not.HighlightSpacing::WhenSelectedwill only allocate the spacing if an item is selected. This means that the table will shift when an item is selected. This is the default setting for backwards compatibility, but it is recommended to useHighlightSpacing::Alwaysfor a better user experience.HighlightSpacing::Neverwill never allocate the spacing, regardless of whether an item is selected or not. This means that the highlight symbol will never be drawn.
This is a fluent setter method which must be chained or used as it consumes self
§Examples
use ratatui::widgets::{HighlightSpacing, List};
let items = ["Item 1"];
let list = List::new(items).highlight_spacing(HighlightSpacing::Always);Sourcepub const fn direction(self, direction: ListDirection) -> Self
pub const fn direction(self, direction: ListDirection) -> Self
Defines the list direction (up or down)
Defines if the List is displayed top to bottom (default) or bottom to top.
If there is too few items to fill the screen, the list will stick to the starting edge.
This is a fluent setter method which must be chained or used as it consumes self
§Example
Bottom to top
use ratatui::widgets::{List, ListDirection};
let items = ["Item 1"];
let list = List::new(items).direction(ListDirection::BottomToTop);Sourcepub const fn scroll_padding(self, padding: usize) -> Self
pub const fn scroll_padding(self, padding: usize) -> Self
Sets the number of items around the currently selected item that should be kept visible
This is a fluent setter method which must be chained or used as it consumes self
§Example
A padding value of 1 will keep 1 item above and 1 item bellow visible if possible
use ratatui::widgets::List;
let items = ["Item 1"];
let list = List::new(items).scroll_padding(1);Trait Implementations§
Source§impl<'a, Item> FromIterator<Item> for List<'a>
impl<'a, Item> FromIterator<Item> for List<'a>
Source§fn from_iter<Iter: IntoIterator<Item = Item>>(iter: Iter) -> Self
fn from_iter<Iter: IntoIterator<Item = Item>>(iter: Iter) -> Self
Source§impl StatefulWidget for &List<'_>
impl StatefulWidget for &List<'_>
Source§impl StatefulWidget for List<'_>
impl StatefulWidget for List<'_>
Source§impl StatefulWidgetRef for List<'_>
impl StatefulWidgetRef for List<'_>
Source§impl WidgetRef for List<'_>
impl WidgetRef for List<'_>
Source§fn render_ref(&self, area: Rect, buf: &mut Buffer)
fn render_ref(&self, area: Rect, buf: &mut Buffer)
impl<'a> Eq for List<'a>
impl<'a> StructuralPartialEq for List<'a>
Auto Trait Implementations§
impl<'a> Freeze for List<'a>
impl<'a> RefUnwindSafe for List<'a>
impl<'a> Send for List<'a>
impl<'a> Sync for List<'a>
impl<'a> Unpin for List<'a>
impl<'a> UnwindSafe for List<'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.