1
0
mirror of https://github.com/rust-lang/rustlings.git synced 2024-09-16 10:51:42 +02:00

Take care of filters when resolving the selected exercise

This commit is contained in:
mo8it 2024-04-11 14:58:56 +02:00
parent f53a0e8700
commit 2e1a87d7d3

View File

@ -1,4 +1,4 @@
use anyhow::Result;
use anyhow::{Context, Result};
use ratatui::{
layout::{Constraint, Rect},
style::{Style, Stylize},
@ -217,21 +217,44 @@ impl<'a> UiState<'a> {
return Ok(None);
};
self.app_state.set_pending(selected)?;
// TODO: Take care of filters!
let exercise = &self.app_state.exercises()[selected];
let (ind, exercise) = self
.app_state
.exercises()
.iter()
.zip(self.app_state.progress())
.enumerate()
.filter_map(|(ind, (exercise, done))| match self.filter {
Filter::Done => done.then_some((ind, exercise)),
Filter::Pending => (!done).then_some((ind, exercise)),
Filter::None => Some((ind, exercise)),
})
.nth(selected)
.context("Invalid selection index")?;
self.app_state.set_pending(ind)?;
exercise.reset()?;
Ok(Some(exercise))
}
#[inline]
pub fn selected_to_current_exercise(&mut self) -> Result<()> {
let Some(selected) = self.table_state.selected() else {
return Ok(());
};
// TODO: Take care of filters!
self.app_state.set_current_exercise_ind(selected)
let ind = self
.app_state
.progress()
.iter()
.enumerate()
.filter_map(|(ind, done)| match self.filter {
Filter::Done => done.then_some(ind),
Filter::Pending => (!done).then_some(ind),
Filter::None => Some(ind),
})
.nth(selected)
.context("Invalid selection index")?;
self.app_state.set_current_exercise_ind(ind)
}
}