Skip to content

Commit

Permalink
fix: select - incorrect number of pages when filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
roele committed Jan 22, 2024
1 parent 1fdb59b commit fba0158
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
13 changes: 12 additions & 1 deletion src/multiselect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl<'a, T: Display> MultiSelect<'a, T> {
pub fn run(mut self) -> io::Result<Vec<T>> {
let max_height = self.term.size().0 as usize;
self.capacity = max_height.max(8) - 4;
self.pages = ((self.options.len() as f64) / self.capacity as f64).ceil() as usize;
self.pages = self.get_pages();

self.max = self.max.min(self.options.len());
self.min = self.min.min(self.max);
Expand Down Expand Up @@ -292,17 +292,28 @@ impl<'a, T: Display> MultiSelect<'a, T> {
}
if !save {
self.filter.clear();
self.pages = self.get_pages();
}
}

fn handle_filter_key(&mut self, c: char) {
self.err = None;
self.filter.push(c);
self.pages = self.get_pages();
}

fn handle_filter_backspace(&mut self) {
self.err = None;
self.filter.pop();
self.pages = self.get_pages();
}

fn get_pages(&self) -> usize {
if self.filtering {
((self.filtered_options().len() as f64) / self.capacity as f64).ceil() as usize
} else {
((self.options.len() as f64) / self.capacity as f64).ceil() as usize
}
}

fn render(&self) -> io::Result<String> {
Expand Down
18 changes: 12 additions & 6 deletions src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl<'a, T: Display> Select<'a, T> {
pub fn run(mut self) -> io::Result<T> {
let max_height = self.term.size().0 as usize;
self.capacity = max_height.max(8) - 4;
self.pages = ((self.options.len() as f64) / self.capacity as f64).ceil() as usize;
self.pages = self.get_pages();

loop {
self.clear()?;
Expand All @@ -122,11 +122,6 @@ impl<'a, T: Display> Select<'a, T> {
Key::Char('/') if self.filterable => self.handle_start_filtering(),
Key::Escape => self.handle_stop_filtering(false),
Key::Enter => {
// if the cursor is on an empty option, don't return it
if self.visible_options().get(self.cursor).is_none() {
self.handle_stop_filtering(false);
continue;
}
self.clear()?;
self.term.show_cursor()?;
let id = self.visible_options().get(self.cursor).unwrap().id;
Expand Down Expand Up @@ -212,15 +207,26 @@ impl<'a, T: Display> Select<'a, T> {
}
if !save {
self.filter.clear();
self.pages = self.get_pages();
}
}

fn handle_filter_key(&mut self, c: char) {
self.filter.push(c);
self.pages = self.get_pages();
}

fn handle_filter_backspace(&mut self) {
self.filter.pop();
self.pages = self.get_pages();
}

fn get_pages(&self) -> usize {
if self.filtering {
((self.filtered_options().len() as f64) / self.capacity as f64).ceil() as usize
} else {
((self.options.len() as f64) / self.capacity as f64).ceil() as usize
}
}

fn render(&self) -> io::Result<String> {
Expand Down

0 comments on commit fba0158

Please sign in to comment.