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 ListItem
s.
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
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_style
sets the style of the selected item.List::highlight_symbol
sets the symbol to be displayed in front of the selected item.List::repeat_highlight_symbol
sets whether to repeat the symbol and style over selected multi-line itemsList::direction
sets the list direction
§Examples
use ratatui::layout::Rect;
use ratatui::style::{Style, Stylize};
use ratatui::widgets::{Block, List, ListDirection, ListItem};
use ratatui::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;
use ratatui::style::{Style, Stylize};
use ratatui::widgets::{Block, List, ListState};
use ratatui::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) -> List<'a>
pub fn new<T>(items: T) -> List<'a>
Creates a new list from ListItem
s
The items
parameter accepts any value that can be converted into an iterator of
Into<ListItem>
. This includes arrays of &str
or Vec
s 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};
use ratatui::text::Text;
use ratatui::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) -> List<'a>
pub fn items<T>(self, items: T) -> List<'a>
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 Vec
s 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>) -> List<'a>
pub fn block(self, block: Block<'a>) -> List<'a>
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>(self, style: S) -> List<'a>
pub fn style<S>(self, style: S) -> List<'a>
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};
use ratatui::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;
use ratatui::widgets::List;
let items = ["Item 1"];
let list = List::new(items).red().italic();
Sourcepub fn highlight_symbol<L>(self, highlight_symbol: L) -> List<'a>
pub fn highlight_symbol<L>(self, highlight_symbol: L) -> List<'a>
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>(self, style: S) -> List<'a>
pub fn highlight_style<S>(self, style: S) -> List<'a>
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};
use ratatui::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) -> List<'a>
pub const fn repeat_highlight_symbol(self, repeat: bool) -> List<'a>
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) -> List<'a>
pub const fn highlight_spacing(self, value: HighlightSpacing) -> List<'a>
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::Always
will 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::WhenSelected
will 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::Always
for a better user experience.HighlightSpacing::Never
will 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) -> List<'a>
pub const fn direction(self, direction: ListDirection) -> List<'a>
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) -> List<'a>
pub const fn scroll_padding(self, padding: usize) -> List<'a>
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§impl StatefulWidget for &List<'_>
impl StatefulWidget for &List<'_>
Source§impl StatefulWidget for List<'_>
impl StatefulWidget for List<'_>
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<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
Source§fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
Source§fn adapt_into(self) -> D
fn adapt_into(self) -> D
Source§impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
Source§fn arrays_from(colors: C) -> T
fn arrays_from(colors: C) -> T
Source§impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
Source§fn arrays_into(self) -> C
fn arrays_into(self) -> C
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<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
Source§type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
parameters
when converting.Source§fn cam16_into_unclamped(
self,
parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>,
) -> T
fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T
self
into C
, using the provided parameters.Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
Source§fn components_from(colors: C) -> T
fn components_from(colors: C) -> T
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> FromAngle<T> for T
impl<T> FromAngle<T> for T
Source§fn from_angle(angle: T) -> T
fn from_angle(angle: T) -> T
angle
.Source§impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
Source§fn from_stimulus(other: U) -> T
fn from_stimulus(other: U) -> T
other
into Self
, while performing the appropriate scaling,
rounding and clamping.Source§impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
Source§fn into_angle(self) -> U
fn into_angle(self) -> U
T
.Source§impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
Source§type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
parameters
when converting.Source§fn into_cam16_unclamped(
self,
parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>,
) -> T
fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T
self
into C
, using the provided parameters.Source§impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
Source§fn into_color(self) -> U
fn into_color(self) -> U
Source§impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
Source§fn into_color_unclamped(self) -> U
fn into_color_unclamped(self) -> U
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<T> IntoStimulus<T> for T
impl<T> IntoStimulus<T> for T
Source§fn into_stimulus(self) -> T
fn into_stimulus(self) -> T
self
into T
, while performing the appropriate scaling,
rounding and clamping.Source§impl<T, U> Stylize<'_, T> for Uwhere
U: Styled<Item = T>,
impl<T, U> Stylize<'_, 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, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
Source§type Error = <C as TryFromComponents<T>>::Error
type Error = <C as TryFromComponents<T>>::Error
try_into_colors
fails to cast.Source§fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
Source§impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
Source§fn try_into_color(self) -> Result<U, OutOfBounds<U>>
fn try_into_color(self) -> Result<U, OutOfBounds<U>>
OutOfBounds
error is returned which contains
the unclamped color. Read more