From b0a475062445705853b4f861ee9e3135065f0660 Mon Sep 17 00:00:00 2001 From: mo8it Date: Sun, 7 Apr 2024 04:59:22 +0200 Subject: Implement "continue at" --- src/list.rs | 54 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 23 deletions(-) (limited to 'src/list.rs') diff --git a/src/list.rs b/src/list.rs index ff031cb..bb5ba1c 100644 --- a/src/list.rs +++ b/src/list.rs @@ -16,25 +16,13 @@ use std::io; use crate::{exercise::Exercise, state::State}; -fn table<'a>(state: &State, exercises: &'a [Exercise]) -> Table<'a> { - let header = Row::new(["Next", "State", "Name", "Path"]); - - let max_name_len = exercises +fn rows<'s, 'e>(state: &'s State, exercises: &'e [Exercise]) -> impl Iterator> + 's +where + 'e: 's, +{ + exercises .iter() - .map(|exercise| exercise.name.len()) - .max() - .unwrap_or(4) as u16; - - let widths = [ - Constraint::Length(4), - Constraint::Length(7), - Constraint::Length(max_name_len), - Constraint::Fill(1), - ]; - - let rows = exercises - .iter() - .zip(&state.progress) + .zip(state.progress()) .enumerate() .map(|(ind, (exercise, done))| { let exercise_state = if *done { @@ -43,7 +31,7 @@ fn table<'a>(state: &State, exercises: &'a [Exercise]) -> Table<'a> { "PENDING".yellow() }; - let next = if ind == state.next_exercise_ind { + let next = if ind == state.next_exercise_ind() { ">>>>".bold().red() } else { Span::default() @@ -56,9 +44,25 @@ fn table<'a>(state: &State, exercises: &'a [Exercise]) -> Table<'a> { Span::raw(exercise.path.to_string_lossy()), ]) }) - .collect::>(); +} - Table::new(rows, widths) +fn table<'a>(state: &State, exercises: &'a [Exercise]) -> Table<'a> { + let header = Row::new(["Next", "State", "Name", "Path"]); + + let max_name_len = exercises + .iter() + .map(|exercise| exercise.name.len()) + .max() + .unwrap_or(4) as u16; + + let widths = [ + Constraint::Length(4), + Constraint::Length(7), + Constraint::Length(max_name_len), + Constraint::Fill(1), + ]; + + Table::new(rows(state, exercises), widths) .header(header) .column_spacing(2) .highlight_spacing(HighlightSpacing::Always) @@ -67,7 +71,7 @@ fn table<'a>(state: &State, exercises: &'a [Exercise]) -> Table<'a> { .block(Block::default().borders(Borders::BOTTOM)) } -pub fn list(state: &State, exercises: &[Exercise]) -> Result<()> { +pub fn list(state: &mut State, exercises: &[Exercise]) -> Result<()> { let mut stdout = io::stdout().lock(); stdout.execute(EnterAlternateScreen)?; @@ -76,7 +80,7 @@ pub fn list(state: &State, exercises: &[Exercise]) -> Result<()> { let mut terminal = Terminal::new(CrosstermBackend::new(&mut stdout))?; terminal.clear()?; - let table = table(state, exercises); + let mut table = table(state, exercises); let last_ind = exercises.len() - 1; let mut selected = 0; @@ -143,6 +147,10 @@ pub fn list(state: &State, exercises: &[Exercise]) -> Result<()> { selected = last_ind; table_state.select(Some(selected)); } + KeyCode::Char('c') => { + state.set_next_exercise_ind(selected)?; + table = table.rows(rows(state, exercises)); + } _ => (), } } -- cgit v1.2.3