diff --git a/src/list/state.rs b/src/list/state.rs index 48c90d2..902e7a6 100644 --- a/src/list/state.rs +++ b/src/list/state.rs @@ -26,14 +26,16 @@ pub struct UiState<'a> { } impl<'a> UiState<'a> { - fn rows<'s, 'i>( + fn rows<'s, 'c, 'i>( state_file: &'s StateFile, exercises: &'a [Exercise], + rows_counter: &'c mut usize, filter: Filter, ) -> impl Iterator> + 'i where 's: 'i, 'a: 'i, + 'c: 'i, { exercises .iter() @@ -45,6 +47,8 @@ impl<'a> UiState<'a> { _ => (), } + *rows_counter += 1; + let next = if ind == state_file.next_exercise_ind() { ">>>>".bold().red() } else { @@ -67,8 +71,13 @@ impl<'a> UiState<'a> { } 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.last_ind = rows_counter.saturating_sub(1); + self.select(self.selected.min(self.last_ind)); + self } @@ -89,7 +98,8 @@ impl<'a> UiState<'a> { ]; 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) .header(header) @@ -111,7 +121,7 @@ impl<'a> UiState<'a> { exercises, selected, table_state, - last_ind: exercises.len() - 1, + last_ind: rows_counter.saturating_sub(1), } }