mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-26 00:00:03 +03:00
Compare commits
4 commits
dd52e9cd72
...
cba4a6f9c8
Author | SHA1 | Date | |
---|---|---|---|
cba4a6f9c8 | |||
5556d42b46 | |||
7d2bc1c7a4 | |||
c209c874a9 |
|
@ -1,5 +1,6 @@
|
||||||
use anyhow::{bail, Context, Error, Result};
|
use anyhow::{bail, Context, Error, Result};
|
||||||
use std::{
|
use std::{
|
||||||
|
env,
|
||||||
fs::{self, File},
|
fs::{self, File},
|
||||||
io::{Read, StdoutLock, Write},
|
io::{Read, StdoutLock, Write},
|
||||||
path::Path,
|
path::Path,
|
||||||
|
@ -44,6 +45,8 @@ pub struct AppState {
|
||||||
file_buf: Vec<u8>,
|
file_buf: Vec<u8>,
|
||||||
official_exercises: bool,
|
official_exercises: bool,
|
||||||
cmd_runner: CmdRunner,
|
cmd_runner: CmdRunner,
|
||||||
|
// Running in VS Code.
|
||||||
|
vs_code: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppState {
|
impl AppState {
|
||||||
|
@ -131,6 +134,7 @@ impl AppState {
|
||||||
file_buf: Vec::with_capacity(2048),
|
file_buf: Vec::with_capacity(2048),
|
||||||
official_exercises: !Path::new("info.toml").exists(),
|
official_exercises: !Path::new("info.toml").exists(),
|
||||||
cmd_runner,
|
cmd_runner,
|
||||||
|
vs_code: env::var_os("TERM_PROGRAM").is_some_and(|v| v == "vscode"),
|
||||||
};
|
};
|
||||||
|
|
||||||
let state_file_status = slf.update_from_file();
|
let state_file_status = slf.update_from_file();
|
||||||
|
@ -163,6 +167,11 @@ impl AppState {
|
||||||
&self.cmd_runner
|
&self.cmd_runner
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn vs_code(&self) -> bool {
|
||||||
|
self.vs_code
|
||||||
|
}
|
||||||
|
|
||||||
// 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 a comment.
|
// - The first line is a comment.
|
||||||
|
@ -321,14 +330,10 @@ impl AppState {
|
||||||
.write_solution_to_disk(self.current_exercise_ind, current_exercise.name)
|
.write_solution_to_disk(self.current_exercise_ind, current_exercise.name)
|
||||||
.map(Some)
|
.map(Some)
|
||||||
} else {
|
} else {
|
||||||
let solution_path = if let Some(dir) = current_exercise.dir {
|
let sol_path = current_exercise.sol_path();
|
||||||
format!("solutions/{dir}/{}.rs", current_exercise.name)
|
|
||||||
} else {
|
|
||||||
format!("solutions/{}.rs", current_exercise.name)
|
|
||||||
};
|
|
||||||
|
|
||||||
if Path::new(&solution_path).exists() {
|
if Path::new(&sol_path).exists() {
|
||||||
return Ok(Some(solution_path));
|
return Ok(Some(sol_path));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(None)
|
Ok(None)
|
||||||
|
@ -461,6 +466,7 @@ mod tests {
|
||||||
file_buf: Vec::new(),
|
file_buf: Vec::new(),
|
||||||
official_exercises: true,
|
official_exercises: true,
|
||||||
cmd_runner: CmdRunner::build().unwrap(),
|
cmd_runner: CmdRunner::build().unwrap(),
|
||||||
|
vs_code: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut assert = |done: [bool; 3], expected: [Option<usize>; 3]| {
|
let mut assert = |done: [bool; 3], expected: [Option<usize>; 3]| {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use crate::info_file::ExerciseInfo;
|
use crate::{exercise::RunnableExercise, info_file::ExerciseInfo};
|
||||||
|
|
||||||
/// Initial capacity of the bins buffer.
|
/// Initial capacity of the bins buffer.
|
||||||
pub const BINS_BUFFER_CAPACITY: usize = 1 << 14;
|
pub const BINS_BUFFER_CAPACITY: usize = 1 << 14;
|
||||||
|
|
|
@ -17,6 +17,8 @@ use crate::{
|
||||||
CURRENT_FORMAT_VERSION,
|
CURRENT_FORMAT_VERSION,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const MAX_EXERCISE_NAME_LEN: usize = 32;
|
||||||
|
|
||||||
// Find a char that isn't allowed in the exercise's `name` or `dir`.
|
// Find a char that isn't allowed in the exercise's `name` or `dir`.
|
||||||
fn forbidden_char(input: &str) -> Option<char> {
|
fn forbidden_char(input: &str) -> Option<char> {
|
||||||
input.chars().find(|c| !c.is_alphanumeric() && *c != '_')
|
input.chars().find(|c| !c.is_alphanumeric() && *c != '_')
|
||||||
|
@ -59,6 +61,9 @@ fn check_info_file_exercises(info_file: &InfoFile) -> Result<HashSet<PathBuf>> {
|
||||||
if name.is_empty() {
|
if name.is_empty() {
|
||||||
bail!("Found an empty exercise name in `info.toml`");
|
bail!("Found an empty exercise name in `info.toml`");
|
||||||
}
|
}
|
||||||
|
if name.len() > MAX_EXERCISE_NAME_LEN {
|
||||||
|
bail!("The length of the exercise name `{name}` is bigger than the maximum {MAX_EXERCISE_NAME_LEN}");
|
||||||
|
}
|
||||||
if let Some(c) = forbidden_char(name) {
|
if let Some(c) = forbidden_char(name) {
|
||||||
bail!("Char `{c}` in the exercise name `{name}` is not allowed");
|
bail!("Char `{c}` in the exercise name `{name}` is not allowed");
|
||||||
}
|
}
|
||||||
|
|
|
@ -68,6 +68,7 @@ pub struct Exercise {
|
||||||
|
|
||||||
pub trait RunnableExercise {
|
pub trait RunnableExercise {
|
||||||
fn name(&self) -> &str;
|
fn name(&self) -> &str;
|
||||||
|
fn dir(&self) -> Option<&str>;
|
||||||
fn strict_clippy(&self) -> bool;
|
fn strict_clippy(&self) -> bool;
|
||||||
fn test(&self) -> bool;
|
fn test(&self) -> bool;
|
||||||
|
|
||||||
|
@ -145,6 +146,31 @@ pub trait RunnableExercise {
|
||||||
|
|
||||||
self.run::<true>(&bin_name, output, cmd_runner)
|
self.run::<true>(&bin_name, output, cmd_runner)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn sol_path(&self) -> String {
|
||||||
|
let name = self.name();
|
||||||
|
|
||||||
|
let mut path = if let Some(dir) = self.dir() {
|
||||||
|
// 14 = 10 + 1 + 3
|
||||||
|
// solutions/ + / + .rs
|
||||||
|
let mut path = String::with_capacity(14 + dir.len() + name.len());
|
||||||
|
path.push_str("solutions/");
|
||||||
|
path.push_str(dir);
|
||||||
|
path.push('/');
|
||||||
|
path
|
||||||
|
} else {
|
||||||
|
// 13 = 10 + 3
|
||||||
|
// solutions/ + .rs
|
||||||
|
let mut path = String::with_capacity(13 + name.len());
|
||||||
|
path.push_str("solutions/");
|
||||||
|
path
|
||||||
|
};
|
||||||
|
|
||||||
|
path.push_str(name);
|
||||||
|
path.push_str(".rs");
|
||||||
|
|
||||||
|
path
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RunnableExercise for Exercise {
|
impl RunnableExercise for Exercise {
|
||||||
|
@ -153,6 +179,11 @@ impl RunnableExercise for Exercise {
|
||||||
self.name
|
self.name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn dir(&self) -> Option<&str> {
|
||||||
|
self.dir
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn strict_clippy(&self) -> bool {
|
fn strict_clippy(&self) -> bool {
|
||||||
self.strict_clippy
|
self.strict_clippy
|
||||||
|
|
|
@ -52,30 +52,6 @@ impl ExerciseInfo {
|
||||||
|
|
||||||
path
|
path
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Path to the solution file starting with the `solutions/` directory.
|
|
||||||
pub fn sol_path(&self) -> String {
|
|
||||||
let mut path = if let Some(dir) = &self.dir {
|
|
||||||
// 14 = 10 + 1 + 3
|
|
||||||
// solutions/ + / + .rs
|
|
||||||
let mut path = String::with_capacity(14 + dir.len() + self.name.len());
|
|
||||||
path.push_str("solutions/");
|
|
||||||
path.push_str(dir);
|
|
||||||
path.push('/');
|
|
||||||
path
|
|
||||||
} else {
|
|
||||||
// 13 = 10 + 3
|
|
||||||
// solutions/ + .rs
|
|
||||||
let mut path = String::with_capacity(13 + self.name.len());
|
|
||||||
path.push_str("solutions/");
|
|
||||||
path
|
|
||||||
};
|
|
||||||
|
|
||||||
path.push_str(&self.name);
|
|
||||||
path.push_str(".rs");
|
|
||||||
|
|
||||||
path
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RunnableExercise for ExerciseInfo {
|
impl RunnableExercise for ExerciseInfo {
|
||||||
|
@ -84,6 +60,11 @@ impl RunnableExercise for ExerciseInfo {
|
||||||
&self.name
|
&self.name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn dir(&self) -> Option<&str> {
|
||||||
|
self.dir.as_deref()
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn strict_clippy(&self) -> bool {
|
fn strict_clippy(&self) -> bool {
|
||||||
self.strict_clippy
|
self.strict_clippy
|
||||||
|
|
|
@ -13,8 +13,8 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
cargo_toml::updated_cargo_toml, embedded::EMBEDDED_FILES, info_file::InfoFile,
|
cargo_toml::updated_cargo_toml, embedded::EMBEDDED_FILES, exercise::RunnableExercise,
|
||||||
term::press_enter_prompt,
|
info_file::InfoFile, term::press_enter_prompt,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
|
|
@ -14,13 +14,11 @@ use crate::{
|
||||||
app_state::AppState,
|
app_state::AppState,
|
||||||
exercise::Exercise,
|
exercise::Exercise,
|
||||||
term::{progress_bar, terminal_file_link, CountedWrite, MaxLenWriter},
|
term::{progress_bar, terminal_file_link, CountedWrite, MaxLenWriter},
|
||||||
MAX_EXERCISE_NAME_LEN,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::scroll_state::ScrollState;
|
use super::scroll_state::ScrollState;
|
||||||
|
|
||||||
// +1 for column padding.
|
const COL_SPACING: usize = 2;
|
||||||
const SPACE: &[u8] = &[b' '; MAX_EXERCISE_NAME_LEN + 1];
|
|
||||||
|
|
||||||
fn next_ln(stdout: &mut StdoutLock) -> io::Result<()> {
|
fn next_ln(stdout: &mut StdoutLock) -> io::Result<()> {
|
||||||
stdout
|
stdout
|
||||||
|
@ -41,7 +39,7 @@ pub struct ListState<'a> {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
app_state: &'a mut AppState,
|
app_state: &'a mut AppState,
|
||||||
scroll_state: ScrollState,
|
scroll_state: ScrollState,
|
||||||
name_col_width: usize,
|
name_col_padding: Vec<u8>,
|
||||||
filter: Filter,
|
filter: Filter,
|
||||||
term_width: u16,
|
term_width: u16,
|
||||||
term_height: u16,
|
term_height: u16,
|
||||||
|
@ -61,6 +59,7 @@ impl<'a> ListState<'a> {
|
||||||
.map(|exercise| exercise.name.len())
|
.map(|exercise| exercise.name.len())
|
||||||
.max()
|
.max()
|
||||||
.map_or(name_col_title_len, |max| max.max(name_col_title_len));
|
.map_or(name_col_title_len, |max| max.max(name_col_title_len));
|
||||||
|
let name_col_padding = vec![b' '; name_col_width + COL_SPACING];
|
||||||
|
|
||||||
let filter = Filter::None;
|
let filter = Filter::None;
|
||||||
let n_rows_with_filter = app_state.exercises().len();
|
let n_rows_with_filter = app_state.exercises().len();
|
||||||
|
@ -73,7 +72,7 @@ impl<'a> ListState<'a> {
|
||||||
message: String::with_capacity(128),
|
message: String::with_capacity(128),
|
||||||
app_state,
|
app_state,
|
||||||
scroll_state,
|
scroll_state,
|
||||||
name_col_width,
|
name_col_padding,
|
||||||
filter,
|
filter,
|
||||||
// Set by `set_term_size`
|
// Set by `set_term_size`
|
||||||
term_width: 0,
|
term_width: 0,
|
||||||
|
@ -162,9 +161,15 @@ impl<'a> ListState<'a> {
|
||||||
writer.stdout.queue(SetForegroundColor(Color::Reset))?;
|
writer.stdout.queue(SetForegroundColor(Color::Reset))?;
|
||||||
|
|
||||||
writer.write_str(exercise.name)?;
|
writer.write_str(exercise.name)?;
|
||||||
writer.write_ascii(&SPACE[..self.name_col_width + 2 - exercise.name.len()])?;
|
writer.write_ascii(&self.name_col_padding[exercise.name.len()..])?;
|
||||||
|
|
||||||
terminal_file_link(&mut writer, exercise.path, Color::Blue)?;
|
// The list links aren't shown correctly in VS Code on Windows.
|
||||||
|
// But VS Code shows its own links anyway.
|
||||||
|
if self.app_state.vs_code() {
|
||||||
|
writer.write_str(exercise.path)?;
|
||||||
|
} else {
|
||||||
|
terminal_file_link(&mut writer, exercise.path, Color::Blue)?;
|
||||||
|
}
|
||||||
|
|
||||||
next_ln(stdout)?;
|
next_ln(stdout)?;
|
||||||
stdout.queue(ResetColor)?;
|
stdout.queue(ResetColor)?;
|
||||||
|
@ -184,7 +189,7 @@ impl<'a> ListState<'a> {
|
||||||
// Header
|
// Header
|
||||||
let mut writer = MaxLenWriter::new(stdout, self.term_width as usize);
|
let mut writer = MaxLenWriter::new(stdout, self.term_width as usize);
|
||||||
writer.write_ascii(b" Current State Name")?;
|
writer.write_ascii(b" Current State Name")?;
|
||||||
writer.write_ascii(&SPACE[..self.name_col_width - 2])?;
|
writer.write_ascii(&self.name_col_padding[2..])?;
|
||||||
writer.write_ascii(b"Path")?;
|
writer.write_ascii(b"Path")?;
|
||||||
next_ln(stdout)?;
|
next_ln(stdout)?;
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,6 @@ mod term;
|
||||||
mod watch;
|
mod watch;
|
||||||
|
|
||||||
const CURRENT_FORMAT_VERSION: u8 = 1;
|
const CURRENT_FORMAT_VERSION: u8 = 1;
|
||||||
const MAX_EXERCISE_NAME_LEN: usize = 32;
|
|
||||||
|
|
||||||
/// Rustlings is a collection of small exercises to get you used to writing and reading Rust code
|
/// Rustlings is a collection of small exercises to get you used to writing and reading Rust code
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
|
|
12
src/term.rs
12
src/term.rs
|
@ -1,6 +1,5 @@
|
||||||
use std::{
|
use std::{
|
||||||
cell::Cell,
|
fmt, fs,
|
||||||
env, fmt, fs,
|
|
||||||
io::{self, BufRead, StdoutLock, Write},
|
io::{self, BufRead, StdoutLock, Write},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -11,10 +10,6 @@ use crossterm::{
|
||||||
Command, QueueableCommand,
|
Command, QueueableCommand,
|
||||||
};
|
};
|
||||||
|
|
||||||
thread_local! {
|
|
||||||
static VS_CODE: Cell<bool> = Cell::new(env::var_os("TERM_PROGRAM").is_some_and(|v| v == "vscode"));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct MaxLenWriter<'a, 'b> {
|
pub struct MaxLenWriter<'a, 'b> {
|
||||||
pub stdout: &'a mut StdoutLock<'b>,
|
pub stdout: &'a mut StdoutLock<'b>,
|
||||||
len: usize,
|
len: usize,
|
||||||
|
@ -161,11 +156,6 @@ pub fn terminal_file_link<'a>(
|
||||||
path: &str,
|
path: &str,
|
||||||
color: Color,
|
color: Color,
|
||||||
) -> io::Result<()> {
|
) -> io::Result<()> {
|
||||||
// VS Code shows its own links. This also avoids some issues, especially on Windows.
|
|
||||||
if VS_CODE.get() {
|
|
||||||
return writer.write_str(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
let canonical_path = fs::canonicalize(path).ok();
|
let canonical_path = fs::canonicalize(path).ok();
|
||||||
|
|
||||||
let Some(canonical_path) = canonical_path.as_deref().and_then(|p| p.to_str()) else {
|
let Some(canonical_path) = canonical_path.as_deref().and_then(|p| p.to_str()) else {
|
||||||
|
|
Loading…
Reference in a new issue