Implement "continue at"

This commit is contained in:
mo8it 2024-04-07 04:59:22 +02:00
parent 4f69285375
commit b0a4750624
4 changed files with 65 additions and 38 deletions

View file

@ -16,6 +16,36 @@ use std::io;
use crate::{exercise::Exercise, state::State}; use crate::{exercise::Exercise, state::State};
fn rows<'s, 'e>(state: &'s State, exercises: &'e [Exercise]) -> impl Iterator<Item = Row<'e>> + 's
where
'e: 's,
{
exercises
.iter()
.zip(state.progress())
.enumerate()
.map(|(ind, (exercise, done))| {
let exercise_state = if *done {
"DONE".green()
} else {
"PENDING".yellow()
};
let next = if ind == state.next_exercise_ind() {
">>>>".bold().red()
} else {
Span::default()
};
Row::new([
next,
exercise_state,
Span::raw(&exercise.name),
Span::raw(exercise.path.to_string_lossy()),
])
})
}
fn table<'a>(state: &State, exercises: &'a [Exercise]) -> Table<'a> { fn table<'a>(state: &State, exercises: &'a [Exercise]) -> Table<'a> {
let header = Row::new(["Next", "State", "Name", "Path"]); let header = Row::new(["Next", "State", "Name", "Path"]);
@ -32,33 +62,7 @@ fn table<'a>(state: &State, exercises: &'a [Exercise]) -> Table<'a> {
Constraint::Fill(1), Constraint::Fill(1),
]; ];
let rows = exercises Table::new(rows(state, exercises), widths)
.iter()
.zip(&state.progress)
.enumerate()
.map(|(ind, (exercise, done))| {
let exercise_state = if *done {
"DONE".green()
} else {
"PENDING".yellow()
};
let next = if ind == state.next_exercise_ind {
">>>>".bold().red()
} else {
Span::default()
};
Row::new([
next,
exercise_state,
Span::raw(&exercise.name),
Span::raw(exercise.path.to_string_lossy()),
])
})
.collect::<Vec<_>>();
Table::new(rows, widths)
.header(header) .header(header)
.column_spacing(2) .column_spacing(2)
.highlight_spacing(HighlightSpacing::Always) .highlight_spacing(HighlightSpacing::Always)
@ -67,7 +71,7 @@ fn table<'a>(state: &State, exercises: &'a [Exercise]) -> Table<'a> {
.block(Block::default().borders(Borders::BOTTOM)) .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(); let mut stdout = io::stdout().lock();
stdout.execute(EnterAlternateScreen)?; stdout.execute(EnterAlternateScreen)?;
@ -76,7 +80,7 @@ pub fn list(state: &State, exercises: &[Exercise]) -> Result<()> {
let mut terminal = Terminal::new(CrosstermBackend::new(&mut stdout))?; let mut terminal = Terminal::new(CrosstermBackend::new(&mut stdout))?;
terminal.clear()?; terminal.clear()?;
let table = table(state, exercises); let mut table = table(state, exercises);
let last_ind = exercises.len() - 1; let last_ind = exercises.len() - 1;
let mut selected = 0; let mut selected = 0;
@ -143,6 +147,10 @@ pub fn list(state: &State, exercises: &[Exercise]) -> Result<()> {
selected = last_ind; selected = last_ind;
table_state.select(Some(selected)); table_state.select(Some(selected));
} }
KeyCode::Char('c') => {
state.set_next_exercise_ind(selected)?;
table = table.rows(rows(state, exercises));
}
_ => (), _ => (),
} }
} }

View file

@ -85,7 +85,7 @@ If you are just starting with Rustlings, run the command `rustlings init` to ini
exit(1); exit(1);
} }
let state = State::read_or_default(&exercises); let mut state = State::read_or_default(&exercises);
match args.command { match args.command {
None | Some(Subcommands::Watch) => { None | Some(Subcommands::Watch) => {
@ -94,7 +94,7 @@ If you are just starting with Rustlings, run the command `rustlings init` to ini
// `Init` is handled above. // `Init` is handled above.
Some(Subcommands::Init) => (), Some(Subcommands::Init) => (),
Some(Subcommands::List) => { Some(Subcommands::List) => {
list::list(&state, &exercises)?; list::list(&mut state, &exercises)?;
} }
Some(Subcommands::Run { name }) => { Some(Subcommands::Run { name }) => {
let exercise = find_exercise(&name, &exercises)?; let exercise = find_exercise(&name, &exercises)?;

View file

@ -1,4 +1,4 @@
use anyhow::{Context, Result}; use anyhow::{bail, Context, Result};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::fs; use std::fs;
@ -6,8 +6,8 @@ use crate::exercise::Exercise;
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct State { pub struct State {
pub next_exercise_ind: usize, next_exercise_ind: usize,
pub progress: Vec<bool>, progress: Vec<bool>,
} }
impl State { impl State {
@ -30,11 +30,30 @@ impl State {
}) })
} }
pub fn write(&self) -> Result<()> { fn write(&self) -> Result<()> {
// TODO: Capacity // TODO: Capacity
let mut buf = Vec::with_capacity(1 << 12); let mut buf = Vec::with_capacity(1024);
serde_json::ser::to_writer(&mut buf, self).context("Failed to serialize the state")?; serde_json::ser::to_writer(&mut buf, self).context("Failed to serialize the state")?;
dbg!(buf.len());
Ok(()) Ok(())
} }
#[inline]
pub fn next_exercise_ind(&self) -> usize {
self.next_exercise_ind
}
pub fn set_next_exercise_ind(&mut self, ind: usize) -> Result<()> {
if ind >= self.progress.len() {
bail!("The next exercise index is higher than the number of exercises");
}
self.next_exercise_ind = ind;
self.write()
}
#[inline]
pub fn progress(&self) -> &[bool] {
&self.progress
}
} }

View file

@ -158,7 +158,7 @@ pub fn watch(state: &State, exercises: &[Exercise]) -> Result<()> {
.watcher() .watcher()
.watch(Path::new("exercises"), RecursiveMode::Recursive)?; .watch(Path::new("exercises"), RecursiveMode::Recursive)?;
let current_exercise_ind = state.progress.iter().position(|done| *done).unwrap_or(0); let current_exercise_ind = state.next_exercise_ind();
let exercise = &exercises[current_exercise_ind]; let exercise = &exercises[current_exercise_ind];