Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .next()/.prev() to ListState to make traversing a list of selectable items easier #1156

Open
sparky8251 opened this issue May 31, 2024 · 4 comments
Assignees
Labels
Status: In Progress Someone is actively working on this Type: Enhancement New feature or request

Comments

@sparky8251
Copy link

Problem

When using a ListState to display selectable items in a TUI, there is no easy way to advance the selection right now requiring a wrapper struct to handle it currently.

Solution

As an example, this is the wrapper code I've been using

#[derive(Debug, Default)]
pub struct StatefulList<T> {
    state: ListState,
    items: Vec<T>,
}

impl<T> StatefulList<T> {
    pub fn with_items(items: Vec<T>) -> Self {
        Self {
            state: ListState::default(),
            items,
        }
    }
    pub fn next(&mut self) {
        let i = match self.state.selected() {
            Some(i) => {
                if i >= self.items.len() - 1 {
                    0
                } else {
                    i + 1
                }
            }
            None => 0,
        };
        self.state.select(Some(i));
    }
    pub fn previous(&mut self) {
        let i = match self.state.selected() {
            Some(i) => {
                if i == 0 {
                    self.items.len() - 1
                } else {
                    i - 1
                }
            }
            None => 0,
        };
        self.state.select(Some(i));
    }
}

But if ListState just had .next()/.prev() implemented on it, I wouldn't need this code at all.

Additional context

It's likely other stateful widgets that are often used to make selectable "lists" would benefit from similar functions, but I'm not sure which ones exist and are missing them as of right now.

@sparky8251 sparky8251 added the Type: Enhancement New feature or request label May 31, 2024
@sparky8251 sparky8251 changed the title Add .next()/.prev() to ListState to make traversing a list easier Add .next()/.prev() to ListState to make traversing a list of selectable items easier May 31, 2024
@kdheepak kdheepak added the Effort: Good First Issue Good for newcomers without much Rust or Ratatui experiance. Apply only if willing to help implement label Jun 1, 2024
@hexavik
Copy link

hexavik commented Jun 15, 2024

Hi, can I take this?

@orhun
Copy link
Member

orhun commented Jun 15, 2024

@hexavik you got it!

@hexavik
Copy link

hexavik commented Jun 16, 2024

Thanks, I shall start working on this from Monday.

@joshka
Copy link
Member

joshka commented Jun 16, 2024

Already in flight - #1159

@joshka joshka removed the Effort: Good First Issue Good for newcomers without much Rust or Ratatui experiance. Apply only if willing to help implement label Jun 16, 2024
@joshka joshka self-assigned this Jun 16, 2024
@joshka joshka added Type: Enhancement Type: Enhancement New feature or request Status: In Progress Someone is actively working on this and removed Type: Enhancement New feature or request Type: zEnhancement labels Jun 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: In Progress Someone is actively working on this Type: Enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants