Fix selection after applying filters

This commit is contained in:
mo8it 2024-04-08 03:08:05 +02:00
parent b5fc06bd56
commit 1db5de9653

View file

@ -26,14 +26,16 @@ pub struct UiState<'a> {
} }
impl<'a> UiState<'a> { impl<'a> UiState<'a> {
fn rows<'s, 'i>( fn rows<'s, 'c, 'i>(
state_file: &'s StateFile, state_file: &'s StateFile,
exercises: &'a [Exercise], exercises: &'a [Exercise],
rows_counter: &'c mut usize,
filter: Filter, filter: Filter,
) -> impl Iterator<Item = Row<'a>> + 'i ) -> impl Iterator<Item = Row<'a>> + 'i
where where
's: 'i, 's: 'i,
'a: 'i, 'a: 'i,
'c: 'i,
{ {
exercises exercises
.iter() .iter()
@ -45,6 +47,8 @@ impl<'a> UiState<'a> {
_ => (), _ => (),
} }
*rows_counter += 1;
let next = if ind == state_file.next_exercise_ind() { let next = if ind == state_file.next_exercise_ind() {
">>>>".bold().red() ">>>>".bold().red()
} else { } else {
@ -67,8 +71,13 @@ impl<'a> UiState<'a> {
} }
pub fn with_updated_rows(mut self, state_file: &StateFile) -> Self { pub fn with_updated_rows(mut self, state_file: &StateFile) -> Self {
let rows = Self::rows(state_file, self.exercises, self.filter); let mut rows_counter = 0;
let rows = Self::rows(state_file, self.exercises, &mut rows_counter, self.filter);
self.table = self.table.rows(rows); self.table = self.table.rows(rows);
self.last_ind = rows_counter.saturating_sub(1);
self.select(self.selected.min(self.last_ind));
self self
} }
@ -89,7 +98,8 @@ impl<'a> UiState<'a> {
]; ];
let filter = Filter::None; let filter = Filter::None;
let rows = Self::rows(state_file, exercises, filter); let mut rows_counter = 0;
let rows = Self::rows(state_file, exercises, &mut rows_counter, filter);
let table = Table::new(rows, widths) let table = Table::new(rows, widths)
.header(header) .header(header)
@ -111,7 +121,7 @@ impl<'a> UiState<'a> {
exercises, exercises,
selected, selected,
table_state, table_state,
last_ind: exercises.len() - 1, last_ind: rows_counter.saturating_sub(1),
} }
} }