Compare commits

..

No commits in common. "cba4a6f9c8f3b76ccfbf8c4c2aab6adda649df64" and "dd52e9cd7239276745c2fbad02a63931327a8e48" have entirely different histories.

9 changed files with 54 additions and 71 deletions

View file

@ -1,6 +1,5 @@
use anyhow::{bail, Context, Error, Result};
use std::{
env,
fs::{self, File},
io::{Read, StdoutLock, Write},
path::Path,
@ -45,8 +44,6 @@ pub struct AppState {
file_buf: Vec<u8>,
official_exercises: bool,
cmd_runner: CmdRunner,
// Running in VS Code.
vs_code: bool,
}
impl AppState {
@ -134,7 +131,6 @@ impl AppState {
file_buf: Vec::with_capacity(2048),
official_exercises: !Path::new("info.toml").exists(),
cmd_runner,
vs_code: env::var_os("TERM_PROGRAM").is_some_and(|v| v == "vscode"),
};
let state_file_status = slf.update_from_file();
@ -167,11 +163,6 @@ impl AppState {
&self.cmd_runner
}
#[inline]
pub fn vs_code(&self) -> bool {
self.vs_code
}
// Write the state file.
// The file's format is very simple:
// - The first line is a comment.
@ -330,10 +321,14 @@ impl AppState {
.write_solution_to_disk(self.current_exercise_ind, current_exercise.name)
.map(Some)
} else {
let sol_path = current_exercise.sol_path();
let solution_path = if let Some(dir) = current_exercise.dir {
format!("solutions/{dir}/{}.rs", current_exercise.name)
} else {
format!("solutions/{}.rs", current_exercise.name)
};
if Path::new(&sol_path).exists() {
return Ok(Some(sol_path));
if Path::new(&solution_path).exists() {
return Ok(Some(solution_path));
}
Ok(None)
@ -466,7 +461,6 @@ mod tests {
file_buf: Vec::new(),
official_exercises: true,
cmd_runner: CmdRunner::build().unwrap(),
vs_code: false,
};
let mut assert = |done: [bool; 3], expected: [Option<usize>; 3]| {

View file

@ -1,7 +1,7 @@
use anyhow::{Context, Result};
use std::path::Path;
use crate::{exercise::RunnableExercise, info_file::ExerciseInfo};
use crate::info_file::ExerciseInfo;
/// Initial capacity of the bins buffer.
pub const BINS_BUFFER_CAPACITY: usize = 1 << 14;

View file

@ -17,8 +17,6 @@ use crate::{
CURRENT_FORMAT_VERSION,
};
const MAX_EXERCISE_NAME_LEN: usize = 32;
// Find a char that isn't allowed in the exercise's `name` or `dir`.
fn forbidden_char(input: &str) -> Option<char> {
input.chars().find(|c| !c.is_alphanumeric() && *c != '_')
@ -61,9 +59,6 @@ fn check_info_file_exercises(info_file: &InfoFile) -> Result<HashSet<PathBuf>> {
if name.is_empty() {
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) {
bail!("Char `{c}` in the exercise name `{name}` is not allowed");
}

View file

@ -68,7 +68,6 @@ pub struct Exercise {
pub trait RunnableExercise {
fn name(&self) -> &str;
fn dir(&self) -> Option<&str>;
fn strict_clippy(&self) -> bool;
fn test(&self) -> bool;
@ -146,31 +145,6 @@ pub trait RunnableExercise {
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 {
@ -179,11 +153,6 @@ impl RunnableExercise for Exercise {
self.name
}
#[inline]
fn dir(&self) -> Option<&str> {
self.dir
}
#[inline]
fn strict_clippy(&self) -> bool {
self.strict_clippy

View file

@ -52,6 +52,30 @@ impl ExerciseInfo {
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 {
@ -60,11 +84,6 @@ impl RunnableExercise for ExerciseInfo {
&self.name
}
#[inline]
fn dir(&self) -> Option<&str> {
self.dir.as_deref()
}
#[inline]
fn strict_clippy(&self) -> bool {
self.strict_clippy

View file

@ -13,8 +13,8 @@ use std::{
};
use crate::{
cargo_toml::updated_cargo_toml, embedded::EMBEDDED_FILES, exercise::RunnableExercise,
info_file::InfoFile, term::press_enter_prompt,
cargo_toml::updated_cargo_toml, embedded::EMBEDDED_FILES, info_file::InfoFile,
term::press_enter_prompt,
};
#[derive(Deserialize)]

View file

@ -14,11 +14,13 @@ use crate::{
app_state::AppState,
exercise::Exercise,
term::{progress_bar, terminal_file_link, CountedWrite, MaxLenWriter},
MAX_EXERCISE_NAME_LEN,
};
use super::scroll_state::ScrollState;
const COL_SPACING: usize = 2;
// +1 for column padding.
const SPACE: &[u8] = &[b' '; MAX_EXERCISE_NAME_LEN + 1];
fn next_ln(stdout: &mut StdoutLock) -> io::Result<()> {
stdout
@ -39,7 +41,7 @@ pub struct ListState<'a> {
pub message: String,
app_state: &'a mut AppState,
scroll_state: ScrollState,
name_col_padding: Vec<u8>,
name_col_width: usize,
filter: Filter,
term_width: u16,
term_height: u16,
@ -59,7 +61,6 @@ impl<'a> ListState<'a> {
.map(|exercise| exercise.name.len())
.max()
.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 n_rows_with_filter = app_state.exercises().len();
@ -72,7 +73,7 @@ impl<'a> ListState<'a> {
message: String::with_capacity(128),
app_state,
scroll_state,
name_col_padding,
name_col_width,
filter,
// Set by `set_term_size`
term_width: 0,
@ -161,15 +162,9 @@ impl<'a> ListState<'a> {
writer.stdout.queue(SetForegroundColor(Color::Reset))?;
writer.write_str(exercise.name)?;
writer.write_ascii(&self.name_col_padding[exercise.name.len()..])?;
writer.write_ascii(&SPACE[..self.name_col_width + 2 - exercise.name.len()])?;
// 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)?;
}
terminal_file_link(&mut writer, exercise.path, Color::Blue)?;
next_ln(stdout)?;
stdout.queue(ResetColor)?;
@ -189,7 +184,7 @@ impl<'a> ListState<'a> {
// Header
let mut writer = MaxLenWriter::new(stdout, self.term_width as usize);
writer.write_ascii(b" Current State Name")?;
writer.write_ascii(&self.name_col_padding[2..])?;
writer.write_ascii(&SPACE[..self.name_col_width - 2])?;
writer.write_ascii(b"Path")?;
next_ln(stdout)?;

View file

@ -25,6 +25,7 @@ mod term;
mod watch;
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
#[derive(Parser)]

View file

@ -1,5 +1,6 @@
use std::{
fmt, fs,
cell::Cell,
env, fmt, fs,
io::{self, BufRead, StdoutLock, Write},
};
@ -10,6 +11,10 @@ use crossterm::{
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 stdout: &'a mut StdoutLock<'b>,
len: usize,
@ -156,6 +161,11 @@ pub fn terminal_file_link<'a>(
path: &str,
color: Color,
) -> 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 Some(canonical_path) = canonical_path.as_deref().and_then(|p| p.to_str()) else {