feat: add ListState / TableState scroll_down_by() / scroll_up_by() methods (#1267)

Implement new methods `scroll_down_by(u16)` and `scroll_up_by(u16)` for
both `Liststate` and `Tablestate`.

Closes: #1207
This commit is contained in:
josueBarretogit 2024-08-02 21:09:26 -05:00 committed by GitHub
parent 476ac87c99
commit b70cd03c02
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 104 additions and 0 deletions

View file

@ -216,6 +216,42 @@ impl ListState {
pub fn select_last(&mut self) {
self.select(Some(usize::MAX));
}
/// 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
///
/// ```rust
/// # use ratatui::{prelude::*, widgets::*};
/// let mut state = ListState::default();
/// state.scroll_down_by(4);
/// ```
pub fn scroll_down_by(&mut self, amount: u16) {
let selected = self.selected.unwrap_or_default();
self.select(Some(selected.saturating_add(amount as usize)));
}
/// 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
///
/// ```rust
/// # use ratatui::{prelude::*, widgets::*};
/// let mut state = ListState::default();
/// state.scroll_up_by(4);
/// ```
pub fn scroll_up_by(&mut self, amount: u16) {
let selected = self.selected.unwrap_or_default();
self.select(Some(selected.saturating_sub(amount as usize)));
}
}
#[cfg(test)]
@ -285,5 +321,21 @@ mod tests {
let mut state = ListState::default();
state.select_previous();
assert_eq!(state.selected, Some(usize::MAX));
let mut state = ListState::default();
state.select(Some(2));
state.scroll_down_by(4);
assert_eq!(state.selected, Some(6));
let mut state = ListState::default();
state.scroll_up_by(3);
assert_eq!(state.selected, Some(0));
state.select(Some(6));
state.scroll_up_by(4);
assert_eq!(state.selected, Some(2));
state.scroll_up_by(4);
assert_eq!(state.selected, Some(0));
}
}

View file

@ -241,6 +241,42 @@ impl TableState {
pub fn select_last(&mut self) {
self.select(Some(usize::MAX));
}
/// Scrolls down by a specified `amount` in the table.
///
/// 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 table), the last item in the table will be selected.
///
/// # Examples
///
/// ```rust
/// # use ratatui::{prelude::*, widgets::*};
/// let mut state = TableState::default();
/// state.scroll_down_by(4);
/// ```
pub fn scroll_down_by(&mut self, amount: u16) {
let selected = self.selected.unwrap_or_default();
self.select(Some(selected.saturating_add(amount as usize)));
}
/// Scrolls up by a specified `amount` in the table.
///
/// 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 table will be selected.
///
/// # Examples
///
/// ```rust
/// # use ratatui::{prelude::*, widgets::*};
/// let mut state = TableState::default();
/// state.scroll_up_by(4);
/// ```
pub fn scroll_up_by(&mut self, amount: u16) {
let selected = self.selected.unwrap_or_default();
self.select(Some(selected.saturating_sub(amount as usize)));
}
}
#[cfg(test)]
@ -340,5 +376,21 @@ mod tests {
let mut state = TableState::default();
state.select_previous();
assert_eq!(state.selected, Some(usize::MAX));
let mut state = TableState::default();
state.select(Some(2));
state.scroll_down_by(4);
assert_eq!(state.selected, Some(6));
let mut state = TableState::default();
state.scroll_up_by(3);
assert_eq!(state.selected, Some(0));
state.select(Some(6));
state.scroll_up_by(4);
assert_eq!(state.selected, Some(2));
state.scroll_up_by(4);
assert_eq!(state.selected, Some(0));
}
}