Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 34 additions & 34 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions src/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ pub enum CheckProgress {
pub struct AppState {
current_exercise_ind: usize,
exercises: Vec<Exercise>,
// Caches the number of done exercises to avoid iterating over all exercises every time.
n_done: u16,
// Cache the number of done exercises to avoid iterating over all exercises every time.
n_done: u32,
final_message: &'static str,
state_file: File,
// Preallocated buffer for reading and writing the state file.
Expand Down Expand Up @@ -191,13 +191,13 @@ impl AppState {
}

#[inline]
pub fn n_done(&self) -> u16 {
pub fn n_done(&self) -> u32 {
self.n_done
}

#[inline]
pub fn n_pending(&self) -> u16 {
self.exercises.len() as u16 - self.n_done
pub fn n_pending(&self) -> u32 {
self.exercises.len() as u32 - self.n_done
}

#[inline]
Expand Down Expand Up @@ -433,7 +433,7 @@ impl AppState {
.is_err()
{
break;
};
}

let success = exercise.run_exercise(None, &slf.cmd_runner);
let progress = match success {
Expand Down
2 changes: 1 addition & 1 deletion src/info_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct ExerciseInfo {
#[serde(default)]
pub skip_check_unsolved: bool,
}
#[inline(always)]
#[inline]
const fn default_true() -> bool {
true
}
Expand Down
31 changes: 22 additions & 9 deletions src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crossterm::{
};
use serde::Deserialize;
use std::{
env::set_current_dir,
env::{current_dir, set_current_dir},
fs::{self, create_dir},
io::{self, Write},
path::Path,
Expand Down Expand Up @@ -169,14 +169,27 @@ pub fn init() -> Result<()> {
fs::write(".vscode/extensions.json", VS_CODE_EXTENSIONS_JSON)
.context("Failed to create the file `rustlings/.vscode/extensions.json`")?;

if init_git {
// Ignore any Git error because Git initialization is not required.
let _ = Command::new("git")
.arg("init")
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status();
if init_git && let Ok(dir) = current_dir() {
let mut dir = dir.as_path();

loop {
if dir.join(".git").exists() || dir.join(".jj").exists() {
break;
}

if let Some(parent) = dir.parent() {
dir = parent;
} else {
// Ignore any Git error because Git initialization is not required.
let _ = Command::new("git")
.arg("init")
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status();
break;
}
}
}

stdout.queue(SetForegroundColor(Color::Green))?;
Expand Down
2 changes: 1 addition & 1 deletion src/list/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ impl<'a> ListState<'a> {
progress_bar(
&mut MaxLenWriter::new(stdout, self.term_width as usize),
self.app_state.n_done(),
self.app_state.exercises().len() as u16,
self.app_state.exercises().len() as u32,
self.term_width,
)?;
next_ln(stdout)?;
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ fn main() -> Result<ExitCode> {
stdout.write_all(b"\n")?;

return Ok(ExitCode::FAILURE);
} else {
app_state.render_final_message(&mut stdout)?;
}

app_state.render_final_message(&mut stdout)?;
}
Some(Subcommands::Reset { name }) => {
app_state.set_current_exercise_by_name(&name)?;
Expand Down
26 changes: 13 additions & 13 deletions src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,19 +193,19 @@ impl Drop for ProgressCounter<'_, '_> {

pub fn progress_bar<'a>(
writer: &mut impl CountedWrite<'a>,
progress: u16,
total: u16,
progress: u32,
total: u32,
term_width: u16,
) -> io::Result<()> {
debug_assert!(total <= 999);
debug_assert!(progress <= total);

const PREFIX: &[u8] = b"Progress: [";
const PREFIX_WIDTH: u16 = PREFIX.len() as u16;
const POSTFIX_WIDTH: u16 = "] xxx/xxx".len() as u16;
const WRAPPER_WIDTH: u16 = PREFIX_WIDTH + POSTFIX_WIDTH;
const MIN_LINE_WIDTH: u16 = WRAPPER_WIDTH + 4;

debug_assert!(total <= 999);
debug_assert!(progress <= total);

if term_width < MIN_LINE_WIDTH {
writer.write_ascii(b"Progress: ")?;
// Integers are in ASCII.
Expand All @@ -215,7 +215,8 @@ pub fn progress_bar<'a>(
let stdout = writer.stdout();
stdout.write_all(PREFIX)?;

let width = term_width - WRAPPER_WIDTH;
// Use u32 to prevent the intermediate multiplication from overflowing
let width = u32::from(term_width - WRAPPER_WIDTH);
let filled = (width * progress) / total;

stdout.queue(SetForegroundColor(Color::Green))?;
Expand All @@ -225,14 +226,13 @@ pub fn progress_bar<'a>(

if filled < width {
stdout.write_all(b">")?;
}

let width_minus_filled = width - filled;
if width_minus_filled > 1 {
let red_part_width = width_minus_filled - 1;
stdout.queue(SetForegroundColor(Color::Red))?;
for _ in 0..red_part_width {
stdout.write_all(b"-")?;
let width_minus_filled = width - filled;
if width_minus_filled > 1 {
stdout.queue(SetForegroundColor(Color::Red))?;
for _ in 1..width_minus_filled {
stdout.write_all(b"-")?;
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/watch/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl<'a> WatchState<'a> {
watch_event_sender,
terminal_event_unpause_receiver,
manual_run,
)
);
})
.context("Failed to spawn a thread to handle terminal events")?;

Expand Down Expand Up @@ -245,7 +245,7 @@ impl<'a> WatchState<'a> {
progress_bar(
stdout,
self.app_state.n_done(),
self.app_state.exercises().len() as u16,
self.app_state.exercises().len() as u32,
self.term_width,
)?;

Expand Down
4 changes: 2 additions & 2 deletions src/watch/terminal_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn terminal_event_handler(
// Pause input until quitting the confirmation prompt.
if unpause_receiver.recv().is_err() {
return;
};
}

continue;
}
Expand All @@ -64,7 +64,7 @@ pub fn terminal_event_handler(
return;
}
}
Ok(Event::FocusGained | Event::FocusLost | Event::Mouse(_)) => continue,
Ok(Event::FocusGained | Event::FocusLost | Event::Mouse(_)) => (),
Err(e) => break WatchEvent::TerminalEventErr(e),
}
};
Expand Down