mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-01-09 20:03:24 +03:00
Compare commits
10 commits
d0fcd8ae8a
...
c8d217ad50
Author | SHA1 | Date | |
---|---|---|---|
c8d217ad50 | |||
a8ddc07a9a | |||
af85f2036c | |||
ff6c15f9c1 | |||
4110ae21af | |||
b15e0a279b | |||
787bec9875 | |||
f0ce2c1afa | |||
850c1d0234 | |||
ee7d976283 |
|
@ -13,7 +13,7 @@ use crate::{exercise::Exercise, state_file::StateFile};
|
|||
|
||||
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();
|
||||
stdout.execute(EnterAlternateScreen)?;
|
||||
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);
|
||||
|
||||
'outer: loop {
|
||||
terminal.draw(|frame| ui_state.draw(frame))?;
|
||||
terminal.draw(|frame| ui_state.draw(frame).unwrap())?;
|
||||
|
||||
let key = loop {
|
||||
match event::read()? {
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
use anyhow::Result;
|
||||
use ratatui::{
|
||||
layout::{Constraint, Rect},
|
||||
style::{Style, Stylize},
|
||||
text::Span,
|
||||
widgets::{Block, Borders, HighlightSpacing, Row, Table, TableState},
|
||||
widgets::{Block, Borders, HighlightSpacing, Paragraph, Row, Table, TableState},
|
||||
Frame,
|
||||
};
|
||||
|
||||
use crate::{exercise::Exercise, state_file::StateFile};
|
||||
use crate::{exercise::Exercise, progress_bar::progress_bar, state_file::StateFile};
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||
pub enum Filter {
|
||||
|
@ -15,29 +16,42 @@ pub enum Filter {
|
|||
None,
|
||||
}
|
||||
|
||||
pub struct UiState<'a> {
|
||||
pub table: Table<'a>,
|
||||
pub struct UiState {
|
||||
pub table: Table<'static>,
|
||||
pub message: String,
|
||||
pub filter: Filter,
|
||||
exercises: &'a [Exercise],
|
||||
exercises: &'static [Exercise],
|
||||
progress: u16,
|
||||
selected: usize,
|
||||
table_state: TableState,
|
||||
last_ind: usize,
|
||||
}
|
||||
|
||||
impl<'a> UiState<'a> {
|
||||
impl UiState {
|
||||
pub fn with_updated_rows(mut self, state_file: &StateFile) -> Self {
|
||||
let mut rows_counter: usize = 0;
|
||||
let mut progress: u16 = 0;
|
||||
let rows = self
|
||||
.exercises
|
||||
.iter()
|
||||
.zip(state_file.progress().iter().copied())
|
||||
.enumerate()
|
||||
.filter_map(|(ind, (exercise, done))| {
|
||||
match (self.filter, done) {
|
||||
(Filter::Done, false) | (Filter::Pending, true) => return None,
|
||||
_ => (),
|
||||
}
|
||||
let exercise_state = if done {
|
||||
progress += 1;
|
||||
|
||||
if self.filter == Filter::Pending {
|
||||
return None;
|
||||
}
|
||||
|
||||
"DONE".green()
|
||||
} else {
|
||||
if self.filter == Filter::Done {
|
||||
return None;
|
||||
}
|
||||
|
||||
"PENDING".yellow()
|
||||
};
|
||||
|
||||
rows_counter += 1;
|
||||
|
||||
|
@ -47,12 +61,6 @@ impl<'a> UiState<'a> {
|
|||
Span::default()
|
||||
};
|
||||
|
||||
let exercise_state = if done {
|
||||
"DONE".green()
|
||||
} else {
|
||||
"PENDING".yellow()
|
||||
};
|
||||
|
||||
Some(Row::new([
|
||||
next,
|
||||
exercise_state,
|
||||
|
@ -66,10 +74,12 @@ impl<'a> UiState<'a> {
|
|||
self.last_ind = rows_counter.saturating_sub(1);
|
||||
self.select(self.selected.min(self.last_ind));
|
||||
|
||||
self.progress = progress;
|
||||
|
||||
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 max_name_len = exercises
|
||||
|
@ -104,6 +114,7 @@ impl<'a> UiState<'a> {
|
|||
message: String::with_capacity(128),
|
||||
filter: Filter::None,
|
||||
exercises,
|
||||
progress: 0,
|
||||
selected,
|
||||
table_state,
|
||||
last_ind: 0,
|
||||
|
@ -140,7 +151,7 @@ impl<'a> UiState<'a> {
|
|||
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();
|
||||
|
||||
frame.render_stateful_widget(
|
||||
|
@ -149,11 +160,26 @@ impl<'a> UiState<'a> {
|
|||
x: 0,
|
||||
y: 0,
|
||||
width: area.width,
|
||||
height: area.height - 1,
|
||||
height: area.height - 3,
|
||||
},
|
||||
&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() {
|
||||
// Help footer.
|
||||
Span::raw(
|
||||
|
@ -171,5 +197,7 @@ impl<'a> UiState<'a> {
|
|||
height: 1,
|
||||
},
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
24
src/main.rs
24
src/main.rs
|
@ -7,6 +7,7 @@ mod embedded;
|
|||
mod exercise;
|
||||
mod init;
|
||||
mod list;
|
||||
mod progress_bar;
|
||||
mod run;
|
||||
mod state_file;
|
||||
mod verify;
|
||||
|
@ -55,7 +56,7 @@ enum Subcommands {
|
|||
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" {
|
||||
for (ind, exercise) in exercises.iter().enumerate() {
|
||||
if !exercise.looks_done()? {
|
||||
|
@ -84,10 +85,13 @@ Did you already install Rust?
|
|||
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)) {
|
||||
init::init(&exercises).context("Initialization failed")?;
|
||||
init::init(exercises).context("Initialization failed")?;
|
||||
println!(
|
||||
"\nDone initialization!\n
|
||||
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);
|
||||
}
|
||||
|
||||
let mut state_file = StateFile::read_or_default(&exercises);
|
||||
let mut state_file = StateFile::read_or_default(exercises);
|
||||
|
||||
match args.command {
|
||||
None | Some(Subcommands::Watch) => {
|
||||
watch::watch(&state_file, &exercises)?;
|
||||
watch::watch(&state_file, exercises)?;
|
||||
}
|
||||
// `Init` is handled above.
|
||||
Some(Subcommands::Init) => (),
|
||||
Some(Subcommands::List) => {
|
||||
list::list(&mut state_file, &exercises)?;
|
||||
list::list(&mut state_file, exercises)?;
|
||||
}
|
||||
Some(Subcommands::Run { name }) => {
|
||||
let (_, exercise) = find_exercise(&name, &exercises)?;
|
||||
let (_, exercise) = find_exercise(&name, exercises)?;
|
||||
run(exercise).unwrap_or_else(|_| exit(1));
|
||||
}
|
||||
Some(Subcommands::Reset { name }) => {
|
||||
let (ind, exercise) = find_exercise(&name, &exercises)?;
|
||||
let (ind, exercise) = find_exercise(&name, exercises)?;
|
||||
exercise.reset()?;
|
||||
state_file.reset(ind)?;
|
||||
println!("The exercise {exercise} has been reset!");
|
||||
}
|
||||
Some(Subcommands::Hint { name }) => {
|
||||
let (_, exercise) = find_exercise(&name, &exercises)?;
|
||||
let (_, exercise) = find_exercise(&name, exercises)?;
|
||||
println!("{}", exercise.hint);
|
||||
}
|
||||
Some(Subcommands::Verify) => match verify(&exercises, 0)? {
|
||||
Some(Subcommands::Verify) => match verify(exercises, 0)? {
|
||||
VerifyState::AllExercisesDone => println!("All exercises done!"),
|
||||
VerifyState::Failed(exercise) => bail!("Exercise {exercise} failed"),
|
||||
},
|
||||
|
|
41
src/progress_bar.rs
Normal file
41
src/progress_bar.rs
Normal 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)
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
use anyhow::{bail, Result};
|
||||
use crossterm::style::Stylize;
|
||||
use std::io::{stdout, Write};
|
||||
|
||||
use crate::exercise::Exercise;
|
||||
|
@ -21,8 +22,7 @@ pub fn run(exercise: &Exercise) -> Result<()> {
|
|||
bail!("Ran {exercise} with errors");
|
||||
}
|
||||
|
||||
// TODO: Color
|
||||
println!("Successfully ran {exercise}");
|
||||
println!("{}", "✓ Successfully ran {exercise}".green());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -4,9 +4,9 @@ use std::io::{stdout, Write};
|
|||
|
||||
use crate::exercise::{Exercise, Mode, State};
|
||||
|
||||
pub enum VerifyState<'a> {
|
||||
pub enum VerifyState {
|
||||
AllExercisesDone,
|
||||
Failed(&'a Exercise),
|
||||
Failed(&'static Exercise),
|
||||
}
|
||||
|
||||
// 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.
|
||||
// If the Exercise being verified is a test, the verbose boolean
|
||||
// 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() {
|
||||
let exercise = &exercises[current_exercise_ind];
|
||||
|
||||
|
|
154
src/watch.rs
154
src/watch.rs
|
@ -1,9 +1,13 @@
|
|||
use anyhow::Result;
|
||||
use notify_debouncer_mini::{new_debouncer, notify::RecursiveMode};
|
||||
use notify_debouncer_mini::{
|
||||
new_debouncer,
|
||||
notify::{self, RecursiveMode},
|
||||
DebounceEventResult, DebouncedEventKind,
|
||||
};
|
||||
use std::{
|
||||
io::{self, BufRead, Write},
|
||||
path::Path,
|
||||
sync::mpsc::{channel, sync_channel},
|
||||
sync::mpsc::{channel, Sender},
|
||||
thread,
|
||||
time::Duration,
|
||||
};
|
||||
|
@ -14,63 +18,127 @@ use crate::{exercise::Exercise, state_file::StateFile};
|
|||
|
||||
use self::state::WatchState;
|
||||
|
||||
enum Event {
|
||||
enum InputEvent {
|
||||
Hint,
|
||||
Clear,
|
||||
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 mut debouncer = new_debouncer(Duration::from_secs(1), tx)?;
|
||||
let mut debouncer = new_debouncer(
|
||||
Duration::from_secs(1),
|
||||
DebouceEventHandler {
|
||||
tx: tx.clone(),
|
||||
exercises,
|
||||
},
|
||||
)?;
|
||||
debouncer
|
||||
.watcher()
|
||||
.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.render()?;
|
||||
|
||||
let (tx, rx) = sync_channel(0);
|
||||
thread::spawn(move || {
|
||||
let mut stdin = io::stdin().lock();
|
||||
let mut stdin_buf = String::with_capacity(8);
|
||||
thread::spawn(move || input_handler(tx));
|
||||
|
||||
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 {
|
||||
Some(Event::Hint) => {
|
||||
watch_state.show_hint()?;
|
||||
}
|
||||
Some(Event::Clear) => {
|
||||
watch_state.render()?;
|
||||
}
|
||||
Some(Event::Quit) => break,
|
||||
None => {
|
||||
watch_state.handle_invalid_cmd()?;
|
||||
}
|
||||
while let Ok(event) = rx.recv() {
|
||||
match event {
|
||||
WatchEvent::Input(InputEvent::Hint) => {
|
||||
watch_state.show_hint()?;
|
||||
}
|
||||
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()),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,27 +1,24 @@
|
|||
use anyhow::Result;
|
||||
use anyhow::{Context, Result};
|
||||
use crossterm::{
|
||||
style::{Attribute, ContentStyle, Stylize},
|
||||
terminal::{Clear, ClearType},
|
||||
terminal::{size, Clear, ClearType},
|
||||
ExecutableCommand,
|
||||
};
|
||||
use notify_debouncer_mini::{DebounceEventResult, DebouncedEventKind};
|
||||
use std::{
|
||||
fmt::Write as _,
|
||||
io::{self, StdoutLock, Write as _},
|
||||
sync::mpsc::Receiver,
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
exercise::{Exercise, State},
|
||||
progress_bar::progress_bar,
|
||||
state_file::StateFile,
|
||||
};
|
||||
|
||||
pub struct WatchState<'a> {
|
||||
writer: StdoutLock<'a>,
|
||||
rx: Receiver<DebounceEventResult>,
|
||||
exercises: &'a [Exercise],
|
||||
exercise: &'a Exercise,
|
||||
exercises: &'static [Exercise],
|
||||
exercise: &'static Exercise,
|
||||
current_exercise_ind: usize,
|
||||
stdout: Option<Vec<u8>>,
|
||||
stderr: Option<Vec<u8>>,
|
||||
|
@ -30,11 +27,7 @@ pub struct WatchState<'a> {
|
|||
}
|
||||
|
||||
impl<'a> WatchState<'a> {
|
||||
pub fn new(
|
||||
state_file: &StateFile,
|
||||
exercises: &'a [Exercise],
|
||||
rx: Receiver<DebounceEventResult>,
|
||||
) -> Self {
|
||||
pub fn new(state_file: &StateFile, exercises: &'static [Exercise]) -> Self {
|
||||
let current_exercise_ind = state_file.next_exercise_ind();
|
||||
let exercise = &exercises[current_exercise_ind];
|
||||
|
||||
|
@ -50,7 +43,6 @@ impl<'a> WatchState<'a> {
|
|||
|
||||
Self {
|
||||
writer,
|
||||
rx,
|
||||
exercises,
|
||||
exercise,
|
||||
current_exercise_ind,
|
||||
|
@ -68,13 +60,15 @@ impl<'a> WatchState<'a> {
|
|||
|
||||
pub fn run_exercise(&mut self) -> Result<bool> {
|
||||
let output = self.exercise.run()?;
|
||||
self.stdout = Some(output.stdout);
|
||||
|
||||
if !output.status.success() {
|
||||
self.stdout = Some(output.stdout);
|
||||
self.stderr = Some(output.stderr);
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
self.stderr = None;
|
||||
|
||||
if let State::Pending(context) = self.exercise.state()? {
|
||||
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);
|
||||
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)
|
||||
}
|
||||
|
||||
pub fn try_recv_event(&mut self) -> Result<()> {
|
||||
let Ok(events) = self.rx.recv_timeout(Duration::from_millis(100)) else {
|
||||
return Ok(());
|
||||
};
|
||||
pub fn run_exercise_with_ind(&mut self, exercise_ind: usize) -> Result<bool> {
|
||||
self.exercise = self
|
||||
.exercises
|
||||
.get(exercise_ind)
|
||||
.context("Invalid exercise index")?;
|
||||
self.current_exercise_ind = exercise_ind;
|
||||
|
||||
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(())
|
||||
self.run_exercise()
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
pub fn render(&mut self) -> io::Result<()> {
|
||||
pub fn render(&mut self) -> Result<()> {
|
||||
self.writer.execute(Clear(ClearType::All))?;
|
||||
|
||||
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.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<()> {
|
||||
|
|
Loading…
Reference in a new issue