Compare commits

...

10 commits

Author SHA1 Message Date
mo8it c8d217ad50 Fix showing stdout and stderr 2024-04-09 22:20:12 +02:00
mo8it a8ddc07a9a Add "exercises" to the end of the progress bar 2024-04-09 22:15:41 +02:00
mo8it af85f2036c Print a newline before the progress bar 2024-04-09 22:06:55 +02:00
mo8it ff6c15f9c1 Don't try to join the input thread 2024-04-09 22:04:10 +02:00
mo8it 4110ae21af Handle notify errors 2024-04-09 21:46:55 +02:00
mo8it b15e0a279b Use shrink to fit before leaking the vector 2024-04-09 21:23:02 +02:00
mo8it 787bec9875 Use exercises as leaked 2024-04-09 21:16:27 +02:00
mo8it f0ce2c1afa Improve event handling in the watch mode 2024-04-09 21:07:53 +02:00
mo8it 850c1d0234 Add progress bar to list 2024-04-09 19:37:39 +02:00
mo8it ee7d976283 Use a green color on successful run 2024-04-09 17:15:12 +02:00
8 changed files with 252 additions and 131 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: &[Exercise]) -> Result<()> { pub fn list(state_file: &mut StateFile, exercises: &'static [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: &[Exercise]) -> Result<()> {
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))?; terminal.draw(|frame| ui_state.draw(frame).unwrap())?;
let key = loop { let key = loop {
match event::read()? { match event::read()? {

View file

@ -1,12 +1,13 @@
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, Row, Table, TableState}, widgets::{Block, Borders, HighlightSpacing, Paragraph, Row, Table, TableState},
Frame, Frame,
}; };
use crate::{exercise::Exercise, state_file::StateFile}; use crate::{exercise::Exercise, progress_bar::progress_bar, state_file::StateFile};
#[derive(Copy, Clone, PartialEq, Eq)] #[derive(Copy, Clone, PartialEq, Eq)]
pub enum Filter { pub enum Filter {
@ -15,29 +16,42 @@ pub enum Filter {
None, None,
} }
pub struct UiState<'a> { pub struct UiState {
pub table: Table<'a>, pub table: Table<'static>,
pub message: String, pub message: String,
pub filter: Filter, pub filter: Filter,
exercises: &'a [Exercise], exercises: &'static [Exercise],
progress: u16,
selected: usize, selected: usize,
table_state: TableState, table_state: TableState,
last_ind: usize, last_ind: usize,
} }
impl<'a> UiState<'a> { impl UiState {
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))| {
match (self.filter, done) { let exercise_state = if done {
(Filter::Done, false) | (Filter::Pending, true) => return None, progress += 1;
_ => (),
} 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;
@ -47,12 +61,6 @@ impl<'a> UiState<'a> {
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,
@ -66,10 +74,12 @@ impl<'a> UiState<'a> {
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: &'a [Exercise]) -> Self { pub fn new(state_file: &StateFile, exercises: &'static [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
@ -104,6 +114,7 @@ impl<'a> UiState<'a> {
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,
@ -140,7 +151,7 @@ impl<'a> UiState<'a> {
self.select(self.last_ind); self.select(self.last_ind);
} }
pub fn draw(&mut self, frame: &mut Frame) { pub fn draw(&mut self, frame: &mut Frame) -> Result<()> {
let area = frame.size(); let area = frame.size();
frame.render_stateful_widget( frame.render_stateful_widget(
@ -149,11 +160,26 @@ impl<'a> UiState<'a> {
x: 0, x: 0,
y: 0, y: 0,
width: area.width, width: area.width,
height: area.height - 1, height: area.height - 3,
}, },
&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(
@ -171,5 +197,7 @@ impl<'a> UiState<'a> {
height: 1, height: 1,
}, },
); );
Ok(())
} }
} }

View file

@ -7,6 +7,7 @@ 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;
@ -55,7 +56,7 @@ enum Subcommands {
List, List,
} }
fn find_exercise<'a>(name: &str, exercises: &'a [Exercise]) -> Result<(usize, &'a Exercise)> { fn find_exercise(name: &str, exercises: &'static [Exercise]) -> Result<(usize, &'static 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()? {
@ -84,10 +85,13 @@ Did you already install Rust?
Try running `cargo --version` to diagnose the problem.", Try running `cargo --version` to diagnose the problem.",
)?; )?;
let exercises = InfoFile::parse()?.exercises; let mut info_file = InfoFile::parse()?;
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.
@ -105,32 +109,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"),
}, },

41
src/progress_bar.rs Normal file
View file

@ -0,0 +1,41 @@
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,4 +1,5 @@
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;
@ -21,8 +22,7 @@ pub fn run(exercise: &Exercise) -> Result<()> {
bail!("Ran {exercise} with errors"); bail!("Ran {exercise} with errors");
} }
// TODO: Color println!("{}", "✓ Successfully ran {exercise}".green());
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<'a> { pub enum VerifyState {
AllExercisesDone, AllExercisesDone,
Failed(&'a Exercise), Failed(&'static Exercise),
} }
// Verify that the provided container of Exercise objects // Verify that the provided container of Exercise objects
@ -14,7 +14,10 @@ pub enum VerifyState<'a> {
// 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(exercises: &[Exercise], mut current_exercise_ind: usize) -> Result<VerifyState<'_>> { pub fn verify(
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,9 +1,13 @@
use anyhow::Result; use anyhow::Result;
use notify_debouncer_mini::{new_debouncer, notify::RecursiveMode}; use notify_debouncer_mini::{
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, sync_channel}, sync::mpsc::{channel, Sender},
thread, thread,
time::Duration, time::Duration,
}; };
@ -14,63 +18,127 @@ use crate::{exercise::Exercise, state_file::StateFile};
use self::state::WatchState; use self::state::WatchState;
enum Event { enum InputEvent {
Hint, Hint,
Clear, Clear,
Quit, Quit,
Unrecognized,
} }
pub fn watch(state_file: &StateFile, exercises: &[Exercise]) -> Result<()> { enum WatchEvent {
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(Duration::from_secs(1), tx)?; let mut debouncer = new_debouncer(
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, rx); let mut watch_state = WatchState::new(state_file, exercises);
// TODO: bool
watch_state.run_exercise()?; watch_state.run_exercise()?;
watch_state.render()?; watch_state.render()?;
let (tx, rx) = sync_channel(0); thread::spawn(move || input_handler(tx));
thread::spawn(move || {
let mut stdin = io::stdin().lock();
let mut stdin_buf = String::with_capacity(8);
loop { while let Ok(event) = rx.recv() {
stdin.read_line(&mut stdin_buf).unwrap(); match event {
WatchEvent::Input(InputEvent::Hint) => {
let event = match stdin_buf.trim() { watch_state.show_hint()?;
"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 {
Some(Event::Hint) => {
watch_state.show_hint()?;
}
Some(Event::Clear) => {
watch_state.render()?;
}
Some(Event::Quit) => break,
None => {
watch_state.handle_invalid_cmd()?;
}
} }
WatchEvent::Input(InputEvent::Clear) | WatchEvent::TerminalResize => {
watch_state.render()?;
}
WatchEvent::Input(InputEvent::Quit) => break,
WatchEvent::Input(InputEvent::Unrecognized) => {
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,27 +1,24 @@
use anyhow::Result; use anyhow::{Context, Result};
use crossterm::{ use crossterm::{
style::{Attribute, ContentStyle, Stylize}, style::{Attribute, ContentStyle, Stylize},
terminal::{Clear, ClearType}, terminal::{size, 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>,
rx: Receiver<DebounceEventResult>, exercises: &'static [Exercise],
exercises: &'a [Exercise], exercise: &'static 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>>,
@ -30,11 +27,7 @@ pub struct WatchState<'a> {
} }
impl<'a> WatchState<'a> { impl<'a> WatchState<'a> {
pub fn new( pub fn new(state_file: &StateFile, exercises: &'static [Exercise]) -> Self {
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];
@ -50,7 +43,6 @@ impl<'a> WatchState<'a> {
Self { Self {
writer, writer,
rx,
exercises, exercises,
exercise, exercise,
current_exercise_ind, current_exercise_ind,
@ -68,13 +60,15 @@ 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!(
" "
@ -106,7 +100,6 @@ 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);
} }
@ -114,41 +107,14 @@ You can keep working on this exercise or jump into the next one by removing the
Ok(true) Ok(true)
} }
pub fn try_recv_event(&mut self) -> Result<()> { pub fn run_exercise_with_ind(&mut self, exercise_ind: usize) -> Result<bool> {
let Ok(events) = self.rx.recv_timeout(Duration::from_millis(100)) else { self.exercise = self
return Ok(()); .exercises
}; .get(exercise_ind)
.context("Invalid exercise index")?;
self.current_exercise_ind = exercise_ind;
if let Some(current_exercise_ind) = events? self.run_exercise()
.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<()> {
@ -156,7 +122,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) -> io::Result<()> { pub fn render(&mut self) -> 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 {
@ -171,7 +137,18 @@ 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.show_prompt() self.writer.write_all(b"\n")?;
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<()> {