mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-27 00:00:03 +03:00
Show the welcome message
This commit is contained in:
parent
3da860927d
commit
8aef915ee7
|
@ -9,7 +9,7 @@ use std::{
|
||||||
io::{Read, StdoutLock, Write},
|
io::{Read, StdoutLock, Write},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{exercise::Exercise, info_file::InfoFile, FENISH_LINE};
|
use crate::{exercise::Exercise, info_file::ExerciseInfo, FENISH_LINE};
|
||||||
|
|
||||||
const STATE_FILE_NAME: &str = ".rustlings-state.txt";
|
const STATE_FILE_NAME: &str = ".rustlings-state.txt";
|
||||||
const BAD_INDEX_ERR: &str = "The current exercise index is higher than the number of exercises";
|
const BAD_INDEX_ERR: &str = "The current exercise index is higher than the number of exercises";
|
||||||
|
@ -20,31 +20,39 @@ pub enum ExercisesProgress {
|
||||||
Pending,
|
Pending,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum StateFileStatus {
|
||||||
|
Read,
|
||||||
|
NotRead,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
current_exercise_ind: usize,
|
current_exercise_ind: usize,
|
||||||
exercises: Vec<Exercise>,
|
exercises: Vec<Exercise>,
|
||||||
n_done: u16,
|
n_done: u16,
|
||||||
welcome_message: String,
|
|
||||||
final_message: String,
|
final_message: String,
|
||||||
file_buf: Vec<u8>,
|
file_buf: Vec<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppState {
|
impl AppState {
|
||||||
fn update_from_file(&mut self) {
|
fn update_from_file(&mut self) -> StateFileStatus {
|
||||||
self.file_buf.clear();
|
self.file_buf.clear();
|
||||||
self.n_done = 0;
|
self.n_done = 0;
|
||||||
|
|
||||||
if File::open(STATE_FILE_NAME)
|
if File::open(STATE_FILE_NAME)
|
||||||
.and_then(|mut file| file.read_to_end(&mut self.file_buf))
|
.and_then(|mut file| file.read_to_end(&mut self.file_buf))
|
||||||
.is_ok()
|
.is_err()
|
||||||
{
|
{
|
||||||
|
return StateFileStatus::NotRead;
|
||||||
|
}
|
||||||
|
|
||||||
|
// See `Self::write` for more information about the file format.
|
||||||
let mut lines = self.file_buf.split(|c| *c == b'\n');
|
let mut lines = self.file_buf.split(|c| *c == b'\n');
|
||||||
let Some(current_exercise_name) = lines.next() else {
|
let Some(current_exercise_name) = lines.next() else {
|
||||||
return;
|
return StateFileStatus::NotRead;
|
||||||
};
|
};
|
||||||
|
|
||||||
if lines.next().is_none() {
|
if current_exercise_name.is_empty() || lines.next().is_none() {
|
||||||
return;
|
return StateFileStatus::NotRead;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut done_exercises = hashbrown::HashSet::with_capacity(self.exercises.len());
|
let mut done_exercises = hashbrown::HashSet::with_capacity(self.exercises.len());
|
||||||
|
@ -66,12 +74,15 @@ impl AppState {
|
||||||
self.current_exercise_ind = ind;
|
self.current_exercise_ind = ind;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
StateFileStatus::Read
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(info_file: InfoFile) -> Self {
|
pub fn new(
|
||||||
let exercises = info_file
|
exercise_infos: Vec<ExerciseInfo>,
|
||||||
.exercises
|
final_message: String,
|
||||||
|
) -> (Self, StateFileStatus) {
|
||||||
|
let exercises = exercise_infos
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|mut exercise_info| {
|
.map(|mut exercise_info| {
|
||||||
// Leaking to be able to borrow in the watch mode `Table`.
|
// Leaking to be able to borrow in the watch mode `Table`.
|
||||||
|
@ -98,14 +109,13 @@ impl AppState {
|
||||||
current_exercise_ind: 0,
|
current_exercise_ind: 0,
|
||||||
exercises,
|
exercises,
|
||||||
n_done: 0,
|
n_done: 0,
|
||||||
welcome_message: info_file.welcome_message.unwrap_or_default(),
|
final_message,
|
||||||
final_message: info_file.final_message.unwrap_or_default(),
|
|
||||||
file_buf: Vec::with_capacity(2048),
|
file_buf: Vec::with_capacity(2048),
|
||||||
};
|
};
|
||||||
|
|
||||||
slf.update_from_file();
|
let state_file_status = slf.update_from_file();
|
||||||
|
|
||||||
slf
|
(slf, state_file_status)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
@ -231,7 +241,8 @@ impl AppState {
|
||||||
|
|
||||||
// Write the state file.
|
// Write the state file.
|
||||||
// The file's format is very simple:
|
// The file's format is very simple:
|
||||||
// - The first line is the name of the current exercise.
|
// - The first line is the name of the current exercise. It must end with `\n` even if there
|
||||||
|
// are no done exercises.
|
||||||
// - The second line is an empty line.
|
// - The second line is an empty line.
|
||||||
// - All remaining lines are the names of done exercises.
|
// - All remaining lines are the names of done exercises.
|
||||||
fn write(&mut self) -> Result<()> {
|
fn write(&mut self) -> Result<()> {
|
||||||
|
@ -239,12 +250,12 @@ impl AppState {
|
||||||
|
|
||||||
self.file_buf
|
self.file_buf
|
||||||
.extend_from_slice(self.current_exercise().name.as_bytes());
|
.extend_from_slice(self.current_exercise().name.as_bytes());
|
||||||
self.file_buf.extend_from_slice(b"\n\n");
|
self.file_buf.push(b'\n');
|
||||||
|
|
||||||
for exercise in &self.exercises {
|
for exercise in &self.exercises {
|
||||||
if exercise.done {
|
if exercise.done {
|
||||||
self.file_buf.extend_from_slice(exercise.name.as_bytes());
|
|
||||||
self.file_buf.push(b'\n');
|
self.file_buf.push(b'\n');
|
||||||
|
self.file_buf.extend_from_slice(exercise.name.as_bytes());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
32
src/main.rs
32
src/main.rs
|
@ -1,6 +1,15 @@
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
|
use app_state::StateFileStatus;
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
use std::{path::Path, process::exit};
|
use crossterm::{
|
||||||
|
terminal::{Clear, ClearType},
|
||||||
|
ExecutableCommand,
|
||||||
|
};
|
||||||
|
use std::{
|
||||||
|
io::{self, BufRead, Write},
|
||||||
|
path::Path,
|
||||||
|
process::exit,
|
||||||
|
};
|
||||||
|
|
||||||
mod app_state;
|
mod app_state;
|
||||||
mod embedded;
|
mod embedded;
|
||||||
|
@ -67,7 +76,26 @@ fn main() -> Result<()> {
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut app_state = AppState::new(info_file);
|
let (mut app_state, state_file_status) = AppState::new(
|
||||||
|
info_file.exercises,
|
||||||
|
info_file.final_message.unwrap_or_default(),
|
||||||
|
);
|
||||||
|
|
||||||
|
if let Some(welcome_message) = info_file.welcome_message {
|
||||||
|
match state_file_status {
|
||||||
|
StateFileStatus::NotRead => {
|
||||||
|
let mut stdout = io::stdout().lock();
|
||||||
|
stdout.execute(Clear(ClearType::All))?;
|
||||||
|
|
||||||
|
let welcome_message = welcome_message.trim();
|
||||||
|
write!(stdout, "{welcome_message}\n\nPress ENTER to continue ")?;
|
||||||
|
stdout.flush()?;
|
||||||
|
|
||||||
|
io::stdin().lock().read_until(b'\n', &mut Vec::new())?;
|
||||||
|
}
|
||||||
|
StateFileStatus::Read => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
match args.command {
|
match args.command {
|
||||||
None => {
|
None => {
|
||||||
|
|
Loading…
Reference in a new issue