Compare commits

..

No commits in common. "c8d217ad50a7117fe35735b4083f2aa1e2b47d97" and "d0fcd8ae8aac43e0c0ac933bd810f11fa79d962e" have entirely different histories.

8 changed files with 131 additions and 252 deletions

View file

@ -13,7 +13,7 @@ use crate::{exercise::Exercise, state_file::StateFile};
use self::state::{Filter, UiState}; use self::state::{Filter, UiState};
pub fn list(state_file: &mut StateFile, exercises: &'static [Exercise]) -> Result<()> { pub fn list(state_file: &mut StateFile, exercises: &[Exercise]) -> Result<()> {
let mut stdout = io::stdout().lock(); let mut stdout = io::stdout().lock();
stdout.execute(EnterAlternateScreen)?; stdout.execute(EnterAlternateScreen)?;
enable_raw_mode()?; enable_raw_mode()?;
@ -24,7 +24,7 @@ pub fn list(state_file: &mut StateFile, exercises: &'static [Exercise]) -> Resul
let mut ui_state = UiState::new(state_file, exercises); let mut ui_state = UiState::new(state_file, exercises);
'outer: loop { 'outer: loop {
terminal.draw(|frame| ui_state.draw(frame).unwrap())?; terminal.draw(|frame| ui_state.draw(frame))?;
let key = loop { let key = loop {
match event::read()? { match event::read()? {

View file

@ -1,13 +1,12 @@
use anyhow::Result;
use ratatui::{ use ratatui::{
layout::{Constraint, Rect}, layout::{Constraint, Rect},
style::{Style, Stylize}, style::{Style, Stylize},
text::Span, text::Span,
widgets::{Block, Borders, HighlightSpacing, Paragraph, Row, Table, TableState}, widgets::{Block, Borders, HighlightSpacing, Row, Table, TableState},
Frame, Frame,
}; };
use crate::{exercise::Exercise, progress_bar::progress_bar, state_file::StateFile}; use crate::{exercise::Exercise, state_file::StateFile};
#[derive(Copy, Clone, PartialEq, Eq)] #[derive(Copy, Clone, PartialEq, Eq)]
pub enum Filter { pub enum Filter {
@ -16,43 +15,30 @@ pub enum Filter {
None, None,
} }
pub struct UiState { pub struct UiState<'a> {
pub table: Table<'static>, pub table: Table<'a>,
pub message: String, pub message: String,
pub filter: Filter, pub filter: Filter,
exercises: &'static [Exercise], exercises: &'a [Exercise],
progress: u16,
selected: usize, selected: usize,
table_state: TableState, table_state: TableState,
last_ind: usize, last_ind: usize,
} }
impl UiState { impl<'a> UiState<'a> {
pub fn with_updated_rows(mut self, state_file: &StateFile) -> Self { pub fn with_updated_rows(mut self, state_file: &StateFile) -> Self {
let mut rows_counter: usize = 0; let mut rows_counter: usize = 0;
let mut progress: u16 = 0;
let rows = self let rows = self
.exercises .exercises
.iter() .iter()
.zip(state_file.progress().iter().copied()) .zip(state_file.progress().iter().copied())
.enumerate() .enumerate()
.filter_map(|(ind, (exercise, done))| { .filter_map(|(ind, (exercise, done))| {
let exercise_state = if done { match (self.filter, done) {
progress += 1; (Filter::Done, false) | (Filter::Pending, true) => return None,
_ => (),
if self.filter == Filter::Pending {
return None;
} }
"DONE".green()
} else {
if self.filter == Filter::Done {
return None;
}
"PENDING".yellow()
};
rows_counter += 1; rows_counter += 1;
let next = if ind == state_file.next_exercise_ind() { let next = if ind == state_file.next_exercise_ind() {
@ -61,6 +47,12 @@ impl UiState {
Span::default() Span::default()
}; };
let exercise_state = if done {
"DONE".green()
} else {
"PENDING".yellow()
};
Some(Row::new([ Some(Row::new([
next, next,
exercise_state, exercise_state,
@ -74,12 +66,10 @@ impl UiState {
self.last_ind = rows_counter.saturating_sub(1); self.last_ind = rows_counter.saturating_sub(1);
self.select(self.selected.min(self.last_ind)); self.select(self.selected.min(self.last_ind));
self.progress = progress;
self self
} }
pub fn new(state_file: &StateFile, exercises: &'static [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"]);
let max_name_len = exercises let max_name_len = exercises
@ -114,7 +104,6 @@ impl UiState {
message: String::with_capacity(128), message: String::with_capacity(128),
filter: Filter::None, filter: Filter::None,
exercises, exercises,
progress: 0,
selected, selected,
table_state, table_state,
last_ind: 0, last_ind: 0,
@ -151,7 +140,7 @@ impl UiState {
self.select(self.last_ind); self.select(self.last_ind);
} }
pub fn draw(&mut self, frame: &mut Frame) -> Result<()> { pub fn draw(&mut self, frame: &mut Frame) {
let area = frame.size(); let area = frame.size();
frame.render_stateful_widget( frame.render_stateful_widget(
@ -160,26 +149,11 @@ impl UiState {
x: 0, x: 0,
y: 0, y: 0,
width: area.width, width: area.width,
height: area.height - 3, height: area.height - 1,
}, },
&mut self.table_state, &mut self.table_state,
); );
frame.render_widget(
Paragraph::new(Span::raw(progress_bar(
self.progress,
self.exercises.len() as u16,
area.width,
)?))
.block(Block::default().borders(Borders::BOTTOM)),
Rect {
x: 0,
y: area.height - 3,
width: area.width,
height: 2,
},
);
let message = if self.message.is_empty() { let message = if self.message.is_empty() {
// Help footer. // Help footer.
Span::raw( Span::raw(
@ -197,7 +171,5 @@ impl UiState {
height: 1, height: 1,
}, },
); );
Ok(())
} }
} }

View file

@ -7,7 +7,6 @@ mod embedded;
mod exercise; mod exercise;
mod init; mod init;
mod list; mod list;
mod progress_bar;
mod run; mod run;
mod state_file; mod state_file;
mod verify; mod verify;
@ -56,7 +55,7 @@ enum Subcommands {
List, List,
} }
fn find_exercise(name: &str, exercises: &'static [Exercise]) -> Result<(usize, &'static Exercise)> { fn find_exercise<'a>(name: &str, exercises: &'a [Exercise]) -> Result<(usize, &'a Exercise)> {
if name == "next" { if name == "next" {
for (ind, exercise) in exercises.iter().enumerate() { for (ind, exercise) in exercises.iter().enumerate() {
if !exercise.looks_done()? { if !exercise.looks_done()? {
@ -85,13 +84,10 @@ Did you already install Rust?
Try running `cargo --version` to diagnose the problem.", Try running `cargo --version` to diagnose the problem.",
)?; )?;
let mut info_file = InfoFile::parse()?; let exercises = InfoFile::parse()?.exercises;
info_file.exercises.shrink_to_fit();
// Leaking is not a problem since the exercises' slice is used until the end of the program.
let exercises = info_file.exercises.leak();
if matches!(args.command, Some(Subcommands::Init)) { if matches!(args.command, Some(Subcommands::Init)) {
init::init(exercises).context("Initialization failed")?; init::init(&exercises).context("Initialization failed")?;
println!( println!(
"\nDone initialization!\n "\nDone initialization!\n
Run `cd rustlings` to go into the generated directory. Run `cd rustlings` to go into the generated directory.
@ -109,32 +105,32 @@ If you are just starting with Rustlings, run the command `rustlings init` to ini
exit(1); exit(1);
} }
let mut state_file = StateFile::read_or_default(exercises); let mut state_file = StateFile::read_or_default(&exercises);
match args.command { match args.command {
None | Some(Subcommands::Watch) => { None | Some(Subcommands::Watch) => {
watch::watch(&state_file, exercises)?; watch::watch(&state_file, &exercises)?;
} }
// `Init` is handled above. // `Init` is handled above.
Some(Subcommands::Init) => (), Some(Subcommands::Init) => (),
Some(Subcommands::List) => { Some(Subcommands::List) => {
list::list(&mut state_file, exercises)?; list::list(&mut state_file, &exercises)?;
} }
Some(Subcommands::Run { name }) => { Some(Subcommands::Run { name }) => {
let (_, exercise) = find_exercise(&name, exercises)?; let (_, exercise) = find_exercise(&name, &exercises)?;
run(exercise).unwrap_or_else(|_| exit(1)); run(exercise).unwrap_or_else(|_| exit(1));
} }
Some(Subcommands::Reset { name }) => { Some(Subcommands::Reset { name }) => {
let (ind, exercise) = find_exercise(&name, exercises)?; let (ind, exercise) = find_exercise(&name, &exercises)?;
exercise.reset()?; exercise.reset()?;
state_file.reset(ind)?; state_file.reset(ind)?;
println!("The exercise {exercise} has been reset!"); println!("The exercise {exercise} has been reset!");
} }
Some(Subcommands::Hint { name }) => { Some(Subcommands::Hint { name }) => {
let (_, exercise) = find_exercise(&name, exercises)?; let (_, exercise) = find_exercise(&name, &exercises)?;
println!("{}", exercise.hint); println!("{}", exercise.hint);
} }
Some(Subcommands::Verify) => match verify(exercises, 0)? { Some(Subcommands::Verify) => match verify(&exercises, 0)? {
VerifyState::AllExercisesDone => println!("All exercises done!"), VerifyState::AllExercisesDone => println!("All exercises done!"),
VerifyState::Failed(exercise) => bail!("Exercise {exercise} failed"), VerifyState::Failed(exercise) => bail!("Exercise {exercise} failed"),
}, },

View file

@ -1,41 +0,0 @@
use anyhow::{bail, Result};
use std::fmt::Write;
pub fn progress_bar(progress: u16, total: u16, line_width: u16) -> Result<String> {
if progress > total {
bail!("The progress of the progress bar is higher than the maximum");
}
// "Progress: [".len() == 11
// "] xxx/xx exercises_".len() == 19 (leaving the last char empty for `total` > 99)
// 11 + 19 = 30
let wrapper_width = 30;
// If the line width is too low for a progress bar, just show the ratio.
if line_width < wrapper_width + 4 {
return Ok(format!("Progress: {progress}/{total} exercises"));
}
let mut line = String::with_capacity(usize::from(line_width));
line.push_str("Progress: [");
let remaining_width = line_width.saturating_sub(wrapper_width);
let filled = (remaining_width * progress) / total;
for _ in 0..filled {
line.push('=');
}
if filled < remaining_width {
line.push('>');
}
for _ in 0..(remaining_width - filled).saturating_sub(1) {
line.push(' ');
}
line.write_fmt(format_args!("] {progress:>3}/{total} exercises"))
.unwrap();
Ok(line)
}

View file

@ -1,5 +1,4 @@
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use crossterm::style::Stylize;
use std::io::{stdout, Write}; use std::io::{stdout, Write};
use crate::exercise::Exercise; use crate::exercise::Exercise;
@ -22,7 +21,8 @@ pub fn run(exercise: &Exercise) -> Result<()> {
bail!("Ran {exercise} with errors"); bail!("Ran {exercise} with errors");
} }
println!("{}", "✓ Successfully ran {exercise}".green()); // TODO: Color
println!("Successfully ran {exercise}");
Ok(()) Ok(())
} }

View file

@ -4,9 +4,9 @@ use std::io::{stdout, Write};
use crate::exercise::{Exercise, Mode, State}; use crate::exercise::{Exercise, Mode, State};
pub enum VerifyState { pub enum VerifyState<'a> {
AllExercisesDone, AllExercisesDone,
Failed(&'static Exercise), Failed(&'a Exercise),
} }
// Verify that the provided container of Exercise objects // Verify that the provided container of Exercise objects
@ -14,10 +14,7 @@ pub enum VerifyState {
// Any such failures will be reported to the end user. // Any such failures will be reported to the end user.
// If the Exercise being verified is a test, the verbose boolean // If the Exercise being verified is a test, the verbose boolean
// determines whether or not the test harness outputs are displayed. // determines whether or not the test harness outputs are displayed.
pub fn verify( pub fn verify(exercises: &[Exercise], mut current_exercise_ind: usize) -> Result<VerifyState<'_>> {
exercises: &'static [Exercise],
mut current_exercise_ind: usize,
) -> Result<VerifyState> {
while current_exercise_ind < exercises.len() { while current_exercise_ind < exercises.len() {
let exercise = &exercises[current_exercise_ind]; let exercise = &exercises[current_exercise_ind];

View file

@ -1,13 +1,9 @@
use anyhow::Result; use anyhow::Result;
use notify_debouncer_mini::{ use notify_debouncer_mini::{new_debouncer, notify::RecursiveMode};
new_debouncer,
notify::{self, RecursiveMode},
DebounceEventResult, DebouncedEventKind,
};
use std::{ use std::{
io::{self, BufRead, Write}, io::{self, BufRead, Write},
path::Path, path::Path,
sync::mpsc::{channel, Sender}, sync::mpsc::{channel, sync_channel},
thread, thread,
time::Duration, time::Duration,
}; };
@ -18,127 +14,63 @@ use crate::{exercise::Exercise, state_file::StateFile};
use self::state::WatchState; use self::state::WatchState;
enum InputEvent { enum Event {
Hint, Hint,
Clear, Clear,
Quit, Quit,
Unrecognized,
} }
enum WatchEvent { pub fn watch(state_file: &StateFile, exercises: &[Exercise]) -> Result<()> {
Input(InputEvent),
FileChange { exercise_ind: usize },
NotifyErr(notify::Error),
StdinErr(io::Error),
TerminalResize,
}
struct DebouceEventHandler {
tx: Sender<WatchEvent>,
exercises: &'static [Exercise],
}
impl notify_debouncer_mini::DebounceEventHandler for DebouceEventHandler {
fn handle_event(&mut self, event: DebounceEventResult) {
let event = match event {
Ok(event) => {
let Some(exercise_ind) = event
.iter()
.filter_map(|event| {
if event.kind != DebouncedEventKind::Any
|| !event.path.extension().is_some_and(|ext| ext == "rs")
{
return None;
}
self.exercises
.iter()
.position(|exercise| event.path.ends_with(&exercise.path))
})
.min()
else {
return;
};
WatchEvent::FileChange { exercise_ind }
}
Err(e) => WatchEvent::NotifyErr(e),
};
// An error occurs when the receiver is dropped.
// After dropping the receiver, the debouncer guard should also be dropped.
let _ = self.tx.send(event);
}
}
fn input_handler(tx: Sender<WatchEvent>) {
let mut stdin = io::stdin().lock();
let mut stdin_buf = String::with_capacity(8);
loop {
if let Err(e) = stdin.read_line(&mut stdin_buf) {
// If `send` returns an error, then the receiver is dropped and
// a shutdown has been already initialized.
let _ = tx.send(WatchEvent::StdinErr(e));
return;
}
let event = match stdin_buf.trim() {
"h" | "hint" => InputEvent::Hint,
"c" | "clear" => InputEvent::Clear,
"q" | "quit" => InputEvent::Quit,
_ => InputEvent::Unrecognized,
};
stdin_buf.clear();
if tx.send(WatchEvent::Input(event)).is_err() {
// The receiver was dropped.
return;
}
}
}
pub fn watch(state_file: &StateFile, exercises: &'static [Exercise]) -> Result<()> {
let (tx, rx) = channel(); let (tx, rx) = channel();
let mut debouncer = new_debouncer( let mut debouncer = new_debouncer(Duration::from_secs(1), tx)?;
Duration::from_secs(1),
DebouceEventHandler {
tx: tx.clone(),
exercises,
},
)?;
debouncer debouncer
.watcher() .watcher()
.watch(Path::new("exercises"), RecursiveMode::Recursive)?; .watch(Path::new("exercises"), RecursiveMode::Recursive)?;
let mut watch_state = WatchState::new(state_file, exercises); let mut watch_state = WatchState::new(state_file, exercises, rx);
// TODO: bool
watch_state.run_exercise()?; watch_state.run_exercise()?;
watch_state.render()?; watch_state.render()?;
thread::spawn(move || input_handler(tx)); let (tx, rx) = sync_channel(0);
thread::spawn(move || {
let mut stdin = io::stdin().lock();
let mut stdin_buf = String::with_capacity(8);
while let Ok(event) = rx.recv() { loop {
stdin.read_line(&mut stdin_buf).unwrap();
let event = match stdin_buf.trim() {
"h" | "hint" => Some(Event::Hint),
"c" | "clear" => Some(Event::Clear),
"q" | "quit" => Some(Event::Quit),
_ => None,
};
stdin_buf.clear();
if tx.send(event).is_err() {
break;
};
}
});
loop {
watch_state.try_recv_event()?;
if let Ok(event) = rx.try_recv() {
match event { match event {
WatchEvent::Input(InputEvent::Hint) => { Some(Event::Hint) => {
watch_state.show_hint()?; watch_state.show_hint()?;
} }
WatchEvent::Input(InputEvent::Clear) | WatchEvent::TerminalResize => { Some(Event::Clear) => {
watch_state.render()?; watch_state.render()?;
} }
WatchEvent::Input(InputEvent::Quit) => break, Some(Event::Quit) => break,
WatchEvent::Input(InputEvent::Unrecognized) => { None => {
watch_state.handle_invalid_cmd()?; watch_state.handle_invalid_cmd()?;
} }
WatchEvent::FileChange { exercise_ind } => {
// TODO: bool
watch_state.run_exercise_with_ind(exercise_ind)?;
watch_state.render()?;
} }
WatchEvent::NotifyErr(e) => return Err(e.into()),
WatchEvent::StdinErr(e) => return Err(e.into()),
} }
} }

View file

@ -1,24 +1,27 @@
use anyhow::{Context, Result}; use anyhow::Result;
use crossterm::{ use crossterm::{
style::{Attribute, ContentStyle, Stylize}, style::{Attribute, ContentStyle, Stylize},
terminal::{size, Clear, ClearType}, terminal::{Clear, ClearType},
ExecutableCommand, ExecutableCommand,
}; };
use notify_debouncer_mini::{DebounceEventResult, DebouncedEventKind};
use std::{ use std::{
fmt::Write as _, fmt::Write as _,
io::{self, StdoutLock, Write as _}, io::{self, StdoutLock, Write as _},
sync::mpsc::Receiver,
time::Duration,
}; };
use crate::{ use crate::{
exercise::{Exercise, State}, exercise::{Exercise, State},
progress_bar::progress_bar,
state_file::StateFile, state_file::StateFile,
}; };
pub struct WatchState<'a> { pub struct WatchState<'a> {
writer: StdoutLock<'a>, writer: StdoutLock<'a>,
exercises: &'static [Exercise], rx: Receiver<DebounceEventResult>,
exercise: &'static Exercise, exercises: &'a [Exercise],
exercise: &'a Exercise,
current_exercise_ind: usize, current_exercise_ind: usize,
stdout: Option<Vec<u8>>, stdout: Option<Vec<u8>>,
stderr: Option<Vec<u8>>, stderr: Option<Vec<u8>>,
@ -27,7 +30,11 @@ pub struct WatchState<'a> {
} }
impl<'a> WatchState<'a> { impl<'a> WatchState<'a> {
pub fn new(state_file: &StateFile, exercises: &'static [Exercise]) -> Self { pub fn new(
state_file: &StateFile,
exercises: &'a [Exercise],
rx: Receiver<DebounceEventResult>,
) -> Self {
let current_exercise_ind = state_file.next_exercise_ind(); let current_exercise_ind = state_file.next_exercise_ind();
let exercise = &exercises[current_exercise_ind]; let exercise = &exercises[current_exercise_ind];
@ -43,6 +50,7 @@ impl<'a> WatchState<'a> {
Self { Self {
writer, writer,
rx,
exercises, exercises,
exercise, exercise,
current_exercise_ind, current_exercise_ind,
@ -60,15 +68,13 @@ impl<'a> WatchState<'a> {
pub fn run_exercise(&mut self) -> Result<bool> { pub fn run_exercise(&mut self) -> Result<bool> {
let output = self.exercise.run()?; let output = self.exercise.run()?;
self.stdout = Some(output.stdout);
if !output.status.success() { if !output.status.success() {
self.stdout = Some(output.stdout);
self.stderr = Some(output.stderr); self.stderr = Some(output.stderr);
return Ok(false); return Ok(false);
} }
self.stderr = None;
if let State::Pending(context) = self.exercise.state()? { if let State::Pending(context) = self.exercise.state()? {
let mut message = format!( let mut message = format!(
" "
@ -100,6 +106,7 @@ You can keep working on this exercise or jump into the next one by removing the
)?; )?;
} }
self.stdout = Some(output.stdout);
self.message = Some(message); self.message = Some(message);
return Ok(false); return Ok(false);
} }
@ -107,14 +114,41 @@ You can keep working on this exercise or jump into the next one by removing the
Ok(true) Ok(true)
} }
pub fn run_exercise_with_ind(&mut self, exercise_ind: usize) -> Result<bool> { pub fn try_recv_event(&mut self) -> Result<()> {
self.exercise = self let Ok(events) = self.rx.recv_timeout(Duration::from_millis(100)) else {
.exercises return Ok(());
.get(exercise_ind) };
.context("Invalid exercise index")?;
self.current_exercise_ind = exercise_ind;
self.run_exercise() if let Some(current_exercise_ind) = events?
.iter()
.filter_map(|event| {
if event.kind != DebouncedEventKind::Any
|| !event.path.extension().is_some_and(|ext| ext == "rs")
{
return None;
}
self.exercises
.iter()
.position(|exercise| event.path.ends_with(&exercise.path))
})
.min()
{
self.current_exercise_ind = current_exercise_ind;
} else {
return Ok(());
};
while self.current_exercise_ind < self.exercises.len() {
self.exercise = &self.exercises[self.current_exercise_ind];
if !self.run_exercise()? {
break;
}
self.current_exercise_ind += 1;
}
Ok(())
} }
pub fn show_prompt(&mut self) -> io::Result<()> { pub fn show_prompt(&mut self) -> io::Result<()> {
@ -122,7 +156,7 @@ You can keep working on this exercise or jump into the next one by removing the
self.writer.flush() self.writer.flush()
} }
pub fn render(&mut self) -> Result<()> { pub fn render(&mut self) -> io::Result<()> {
self.writer.execute(Clear(ClearType::All))?; self.writer.execute(Clear(ClearType::All))?;
if let Some(stdout) = &self.stdout { if let Some(stdout) = &self.stdout {
@ -137,18 +171,7 @@ You can keep working on this exercise or jump into the next one by removing the
self.writer.write_all(message.as_bytes())?; self.writer.write_all(message.as_bytes())?;
} }
self.writer.write_all(b"\n")?; self.show_prompt()
let line_width = size()?.0;
let progress_bar = progress_bar(
self.current_exercise_ind as u16,
self.exercises.len() as u16,
line_width,
)?;
self.writer.write_all(progress_bar.as_bytes())?;
self.show_prompt()?;
Ok(())
} }
pub fn show_hint(&mut self) -> io::Result<()> { pub fn show_hint(&mut self) -> io::Result<()> {