diff --git a/src/list.rs b/src/list.rs index 754c5e2..27a31d1 100644 --- a/src/list.rs +++ b/src/list.rs @@ -63,7 +63,9 @@ fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()> list_state.reset_selected()?; } KeyCode::Char('c') => { - return list_state.selected_to_current_exercise(); + if list_state.selected_to_current_exercise()? { + return Ok(()); + } } // Redraw to remove the message. KeyCode::Esc => (), diff --git a/src/list/state.rs b/src/list/state.rs index cf147b4..645c768 100644 --- a/src/list/state.rs +++ b/src/list/state.rs @@ -250,25 +250,27 @@ impl<'a> ListState<'a> { Ok(()) } - pub fn selected_to_current_exercise(&mut self) -> Result<()> { + // Return `true` if there was something to select. + pub fn selected_to_current_exercise(&mut self) -> Result { let Some(selected) = self.selected else { - // TODO: Don't exit list - return Ok(()); + self.message.push_str("Nothing selected to continue at!"); + return Ok(false); }; - let ind = self + let (ind, _) = self .app_state .exercises() .iter() .enumerate() - .filter_map(|(ind, exercise)| match self.filter { - Filter::Done => exercise.done.then_some(ind), - Filter::Pending => (!exercise.done).then_some(ind), - Filter::None => Some(ind), + .filter(|(_, exercise)| match self.filter { + Filter::Done => exercise.done, + Filter::Pending => !exercise.done, + Filter::None => true, }) .nth(selected) .context("Invalid selection index")?; - self.app_state.set_current_exercise_ind(ind) + self.app_state.set_current_exercise_ind(ind)?; + Ok(true) } }