Implement done/pending filters

This commit is contained in:
mo8it 2024-04-08 02:41:48 +02:00
parent 05729b27a0
commit 7c4d33654f
2 changed files with 59 additions and 11 deletions

View file

@ -11,7 +11,7 @@ mod state;
use crate::{exercise::Exercise, state_file::StateFile}; use crate::{exercise::Exercise, state_file::StateFile};
use self::state::UiState; use self::state::{Filter, UiState};
pub fn list(state_file: &mut StateFile, exercises: &[Exercise]) -> Result<()> { pub fn list(state_file: &mut StateFile, exercises: &[Exercise]) -> Result<()> {
let mut stdout = io::stdout().lock(); let mut stdout = io::stdout().lock();
@ -50,20 +50,44 @@ pub fn list(state_file: &mut StateFile, exercises: &[Exercise]) -> Result<()> {
KeyCode::Up | KeyCode::Char('k') => ui_state.select_previous(), KeyCode::Up | KeyCode::Char('k') => ui_state.select_previous(),
KeyCode::Home | KeyCode::Char('g') => ui_state.select_first(), KeyCode::Home | KeyCode::Char('g') => ui_state.select_first(),
KeyCode::End | KeyCode::Char('G') => ui_state.select_last(), KeyCode::End | KeyCode::Char('G') => ui_state.select_last(),
KeyCode::Char('d') => {
let message = if ui_state.filter == Filter::Done {
ui_state.filter = Filter::None;
"Disabled filter DONE"
} else {
ui_state.filter = Filter::Done;
"Enabled filter DONE │ Press d again to disable the filter"
};
ui_state = ui_state.with_updated_rows(state_file);
ui_state.message.push_str(message);
}
KeyCode::Char('p') => {
let message = if ui_state.filter == Filter::Pending {
ui_state.filter = Filter::None;
"Disabled filter PENDING"
} else {
ui_state.filter = Filter::Pending;
"Enabled filter PENDING │ Press p again to disable the filter"
};
ui_state = ui_state.with_updated_rows(state_file);
ui_state.message.push_str(message);
}
KeyCode::Char('r') => { KeyCode::Char('r') => {
let selected = ui_state.selected(); let selected = ui_state.selected();
let exercise = &exercises[selected]; let exercise = &exercises[selected];
exercise.reset()?; exercise.reset()?;
state_file.reset(selected)?; state_file.reset(selected)?;
ui_state.table = ui_state.table.rows(UiState::rows(state_file, exercises)); ui_state = ui_state.with_updated_rows(state_file);
ui_state ui_state
.message .message
.write_fmt(format_args!("The exercise {exercise} has been reset!"))?; .write_fmt(format_args!("The exercise {exercise} has been reset!"))?;
} }
KeyCode::Char('c') => { KeyCode::Char('c') => {
state_file.set_next_exercise_ind(ui_state.selected())?; state_file.set_next_exercise_ind(ui_state.selected())?;
ui_state.table = ui_state.table.rows(UiState::rows(state_file, exercises)); ui_state = ui_state.with_updated_rows(state_file);
} }
_ => (), _ => (),
} }

View file

@ -8,18 +8,28 @@ use ratatui::{
use crate::{exercise::Exercise, state_file::StateFile}; use crate::{exercise::Exercise, state_file::StateFile};
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum Filter {
Done,
Pending,
None,
}
pub struct UiState<'a> { pub struct UiState<'a> {
pub table: Table<'a>, pub table: Table<'a>,
pub message: String, pub message: String,
pub filter: Filter,
exercises: &'a [Exercise],
selected: usize, selected: usize,
table_state: TableState, table_state: TableState,
last_ind: usize, last_ind: usize,
} }
impl<'a> UiState<'a> { impl<'a> UiState<'a> {
pub fn rows<'s, 'i>( fn rows<'s, 'i>(
state_file: &'s StateFile, state_file: &'s StateFile,
exercises: &'a [Exercise], exercises: &'a [Exercise],
filter: Filter,
) -> impl Iterator<Item = Row<'a>> + 'i ) -> impl Iterator<Item = Row<'a>> + 'i
where where
's: 'i, 's: 'i,
@ -27,30 +37,41 @@ impl<'a> UiState<'a> {
{ {
exercises exercises
.iter() .iter()
.zip(state_file.progress()) .zip(state_file.progress().iter().copied())
.enumerate() .enumerate()
.map(|(ind, (exercise, done))| { .filter_map(move |(ind, (exercise, done))| {
match (filter, done) {
(Filter::Done, false) | (Filter::Pending, true) => return None,
_ => (),
}
let next = if ind == state_file.next_exercise_ind() { let next = if ind == state_file.next_exercise_ind() {
">>>>".bold().red() ">>>>".bold().red()
} else { } else {
Span::default() Span::default()
}; };
let exercise_state = if *done { let exercise_state = if done {
"DONE".green() "DONE".green()
} else { } else {
"PENDING".yellow() "PENDING".yellow()
}; };
Row::new([ Some(Row::new([
next, next,
exercise_state, exercise_state,
Span::raw(&exercise.name), Span::raw(&exercise.name),
Span::raw(exercise.path.to_string_lossy()), Span::raw(exercise.path.to_string_lossy()),
]) ]))
}) })
} }
pub fn with_updated_rows(mut self, state_file: &StateFile) -> Self {
let rows = Self::rows(state_file, self.exercises, self.filter);
self.table = self.table.rows(rows);
self
}
pub fn new(state_file: &StateFile, exercises: &'a [Exercise]) -> Self { pub fn new(state_file: &StateFile, exercises: &'a [Exercise]) -> Self {
let header = Row::new(["Next", "State", "Name", "Path"]); let header = Row::new(["Next", "State", "Name", "Path"]);
@ -67,7 +88,8 @@ impl<'a> UiState<'a> {
Constraint::Fill(1), Constraint::Fill(1),
]; ];
let rows = Self::rows(state_file, exercises); let filter = Filter::None;
let rows = Self::rows(state_file, exercises, filter);
let table = Table::new(rows, widths) let table = Table::new(rows, widths)
.header(header) .header(header)
@ -84,10 +106,12 @@ impl<'a> UiState<'a> {
Self { Self {
table, table,
message: String::with_capacity(128),
filter,
exercises,
selected, selected,
table_state, table_state,
last_ind: exercises.len() - 1, last_ind: exercises.len() - 1,
message: String::with_capacity(128),
} }
} }