ratatui::widgets

Struct ListState

Source
pub struct ListState { /* private fields */ }
Expand description

State of the List widget

This state can be used to scroll through items and select one. When the list is rendered as a stateful widget, the selected item will be highlighted and the list will be shifted to ensure that the selected item is visible. This will modify the ListState object passed to the Frame::render_stateful_widget method.

The state consists of two fields:

  • offset: the index of the first item to be displayed
  • selected: the index of the selected item, which can be None if no item is selected

See the list in the Examples directory for a more in depth example of the various configuration options and for how to handle state.

§Example

use ratatui::{
    layout::Rect,
    widgets::{List, ListState},
    Frame,
};

let items = ["Item 1"];
let list = List::new(items);

// This should be stored outside of the function in your application state.
let mut state = ListState::default();

*state.offset_mut() = 1; // display the second item and onwards
state.select(Some(3)); // select the forth item (0-indexed)

frame.render_stateful_widget(list, area, &mut state);

Implementations§

Source§

impl ListState

Source

pub const fn with_offset(self, offset: usize) -> Self

Sets the index of the first item to be displayed

This is a fluent setter method which must be chained or used as it consumes self

§Examples
use ratatui::widgets::ListState;

let state = ListState::default().with_offset(1);
Source

pub const fn with_selected(self, selected: Option<usize>) -> Self

Sets the index of the selected item

This is a fluent setter method which must be chained or used as it consumes self

§Examples
use ratatui::widgets::ListState;

let state = ListState::default().with_selected(Some(1));
Source

pub const fn offset(&self) -> usize

Index of the first item to be displayed

§Examples
use ratatui::widgets::ListState;

let state = ListState::default();
assert_eq!(state.offset(), 0);
Source

pub fn offset_mut(&mut self) -> &mut usize

Mutable reference to the index of the first item to be displayed

§Examples
use ratatui::widgets::ListState;

let mut state = ListState::default();
*state.offset_mut() = 1;
Source

pub const fn selected(&self) -> Option<usize>

Index of the selected item

Returns None if no item is selected

§Examples
use ratatui::widgets::ListState;

let state = ListState::default();
assert_eq!(state.selected(), None);
Source

pub fn selected_mut(&mut self) -> &mut Option<usize>

Mutable reference to the index of the selected item

Returns None if no item is selected

§Examples
use ratatui::widgets::ListState;

let mut state = ListState::default();
*state.selected_mut() = Some(1);
Source

pub fn select(&mut self, index: Option<usize>)

Sets the index of the selected item

Set to None if no item is selected. This will also reset the offset to 0.

§Examples
use ratatui::widgets::ListState;

let mut state = ListState::default();
state.select(Some(1));
Source

pub fn select_next(&mut self)

Selects the next item or the first one if no item is selected

Note: until the list is rendered, the number of items is not known, so the index is set to 0 and will be corrected when the list is rendered

§Examples
use ratatui::widgets::ListState;

let mut state = ListState::default();
state.select_next();
Source

pub fn select_previous(&mut self)

Selects the previous item or the last one if no item is selected

Note: until the list is rendered, the number of items is not known, so the index is set to usize::MAX and will be corrected when the list is rendered

§Examples
use ratatui::widgets::ListState;

let mut state = ListState::default();
state.select_previous();
Source

pub fn select_first(&mut self)

Selects the first item

Note: until the list is rendered, the number of items is not known, so the index is set to 0 and will be corrected when the list is rendered

§Examples
use ratatui::widgets::ListState;

let mut state = ListState::default();
state.select_first();
Source

pub fn select_last(&mut self)

Selects the last item

Note: until the list is rendered, the number of items is not known, so the index is set to usize::MAX and will be corrected when the list is rendered

§Examples
use ratatui::widgets::ListState;

let mut state = ListState::default();
state.select_last();
Source

pub fn scroll_down_by(&mut self, amount: u16)

Scrolls down by a specified amount in the list.

This method updates the selected index by moving it down by the given amount. If the amount causes the index to go out of bounds (i.e., if the index is greater than the length of the list), the last item in the list will be selected.

§Examples
use ratatui::widgets::ListState;

let mut state = ListState::default();
state.scroll_down_by(4);
Source

pub fn scroll_up_by(&mut self, amount: u16)

Scrolls up by a specified amount in the list.

This method updates the selected index by moving it up by the given amount. If the amount causes the index to go out of bounds (i.e., less than zero), the first item in the list will be selected.

§Examples
use ratatui::widgets::ListState;

let mut state = ListState::default();
state.scroll_up_by(4);

Trait Implementations§

Source§

impl Clone for ListState

Source§

fn clone(&self) -> ListState

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ListState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ListState

Source§

fn default() -> ListState

Returns the “default value” for a type. Read more
Source§

impl Hash for ListState

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for ListState

Source§

fn eq(&self, other: &ListState) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for ListState

Source§

impl StructuralPartialEq for ListState

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.