mirror of
https://github.com/notohh/rustlings.git
synced 2024-11-24 22:47:32 -05:00
Implement going to the next exercise
This commit is contained in:
parent
98c5088a39
commit
a534de0312
3 changed files with 31 additions and 5 deletions
11
src/watch.rs
11
src/watch.rs
|
@ -26,9 +26,9 @@ use self::{
|
||||||
enum WatchEvent {
|
enum WatchEvent {
|
||||||
Input(InputEvent),
|
Input(InputEvent),
|
||||||
FileChange { exercise_ind: usize },
|
FileChange { exercise_ind: usize },
|
||||||
|
TerminalResize,
|
||||||
NotifyErr(notify::Error),
|
NotifyErr(notify::Error),
|
||||||
TerminalEventErr(io::Error),
|
TerminalEventErr(io::Error),
|
||||||
TerminalResize,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returned by the watch mode to indicate what to do afterwards.
|
/// Returned by the watch mode to indicate what to do afterwards.
|
||||||
|
@ -60,15 +60,15 @@ pub fn watch(app_state: &mut AppState) -> Result<WatchExit> {
|
||||||
|
|
||||||
while let Ok(event) = rx.recv() {
|
while let Ok(event) = rx.recv() {
|
||||||
match event {
|
match event {
|
||||||
|
WatchEvent::Input(InputEvent::Next) => {
|
||||||
|
watch_state.next_exercise()?;
|
||||||
|
}
|
||||||
WatchEvent::Input(InputEvent::Hint) => {
|
WatchEvent::Input(InputEvent::Hint) => {
|
||||||
watch_state.show_hint()?;
|
watch_state.show_hint()?;
|
||||||
}
|
}
|
||||||
WatchEvent::Input(InputEvent::List) => {
|
WatchEvent::Input(InputEvent::List) => {
|
||||||
return Ok(WatchExit::List);
|
return Ok(WatchExit::List);
|
||||||
}
|
}
|
||||||
WatchEvent::TerminalResize => {
|
|
||||||
watch_state.render()?;
|
|
||||||
}
|
|
||||||
WatchEvent::Input(InputEvent::Quit) => break,
|
WatchEvent::Input(InputEvent::Quit) => break,
|
||||||
WatchEvent::Input(InputEvent::Unrecognized(cmd)) => {
|
WatchEvent::Input(InputEvent::Unrecognized(cmd)) => {
|
||||||
watch_state.handle_invalid_cmd(&cmd)?;
|
watch_state.handle_invalid_cmd(&cmd)?;
|
||||||
|
@ -76,6 +76,9 @@ pub fn watch(app_state: &mut AppState) -> Result<WatchExit> {
|
||||||
WatchEvent::FileChange { exercise_ind } => {
|
WatchEvent::FileChange { exercise_ind } => {
|
||||||
watch_state.run_exercise_with_ind(exercise_ind)?;
|
watch_state.run_exercise_with_ind(exercise_ind)?;
|
||||||
}
|
}
|
||||||
|
WatchEvent::TerminalResize => {
|
||||||
|
watch_state.render()?;
|
||||||
|
}
|
||||||
WatchEvent::NotifyErr(e) => {
|
WatchEvent::NotifyErr(e) => {
|
||||||
return Err(Error::from(e).context("Exercise file watcher failed"))
|
return Err(Error::from(e).context("Exercise file watcher failed"))
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,10 @@ use crossterm::{
|
||||||
};
|
};
|
||||||
use std::io::{self, StdoutLock, Write};
|
use std::io::{self, StdoutLock, Write};
|
||||||
|
|
||||||
use crate::{app_state::AppState, progress_bar::progress_bar};
|
use crate::{
|
||||||
|
app_state::{AppState, ExercisesProgress},
|
||||||
|
progress_bar::progress_bar,
|
||||||
|
};
|
||||||
|
|
||||||
pub struct WatchState<'a> {
|
pub struct WatchState<'a> {
|
||||||
writer: StdoutLock<'a>,
|
writer: StdoutLock<'a>,
|
||||||
|
@ -58,9 +61,27 @@ impl<'a> WatchState<'a> {
|
||||||
self.run_current_exercise()
|
self.run_current_exercise()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn next_exercise(&mut self) -> Result<()> {
|
||||||
|
if !self.show_done {
|
||||||
|
self.writer
|
||||||
|
.write_all(b"The current exercise isn't done yet\n")?;
|
||||||
|
self.show_prompt()?;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
match self.app_state.done_current_exercise()? {
|
||||||
|
ExercisesProgress::AllDone => todo!(),
|
||||||
|
ExercisesProgress::Pending => self.run_current_exercise(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn show_prompt(&mut self) -> io::Result<()> {
|
fn show_prompt(&mut self) -> io::Result<()> {
|
||||||
self.writer.write_all(b"\n")?;
|
self.writer.write_all(b"\n")?;
|
||||||
|
|
||||||
|
if self.show_done {
|
||||||
|
self.writer.write_fmt(format_args!("{}ext/", 'n'.bold()))?;
|
||||||
|
}
|
||||||
|
|
||||||
if !self.show_hint {
|
if !self.show_hint {
|
||||||
self.writer.write_fmt(format_args!("{}int/", 'h'.bold()))?;
|
self.writer.write_fmt(format_args!("{}int/", 'h'.bold()))?;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ use std::sync::mpsc::Sender;
|
||||||
use super::WatchEvent;
|
use super::WatchEvent;
|
||||||
|
|
||||||
pub enum InputEvent {
|
pub enum InputEvent {
|
||||||
|
Next,
|
||||||
Hint,
|
Hint,
|
||||||
List,
|
List,
|
||||||
Quit,
|
Quit,
|
||||||
|
@ -38,6 +39,7 @@ pub fn terminal_event_handler(tx: Sender<WatchEvent>) {
|
||||||
match key.code {
|
match key.code {
|
||||||
KeyCode::Enter => {
|
KeyCode::Enter => {
|
||||||
let input_event = match input.trim() {
|
let input_event = match input.trim() {
|
||||||
|
"n" | "next" => InputEvent::Next,
|
||||||
"h" | "hint" => InputEvent::Hint,
|
"h" | "hint" => InputEvent::Hint,
|
||||||
"l" | "list" => break InputEvent::List,
|
"l" | "list" => break InputEvent::List,
|
||||||
"q" | "quit" => break InputEvent::Quit,
|
"q" | "quit" => break InputEvent::Quit,
|
||||||
|
|
Loading…
Reference in a new issue