diff --git a/src/app_state.rs b/src/app_state.rs index ad63c6de..5722e607 100644 --- a/src/app_state.rs +++ b/src/app_state.rs @@ -52,8 +52,8 @@ pub enum CheckProgress { pub struct AppState { current_exercise_ind: usize, exercises: Vec, - // 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. @@ -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] diff --git a/src/list/state.rs b/src/list/state.rs index 55ccb4c9..58aa4961 100644 --- a/src/list/state.rs +++ b/src/list/state.rs @@ -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)?; diff --git a/src/term.rs b/src/term.rs index 65e4c511..8cab5005 100644 --- a/src/term.rs +++ b/src/term.rs @@ -193,8 +193,8 @@ 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<()> { const PREFIX: &[u8] = b"Progress: ["; @@ -215,10 +215,9 @@ 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 u16 - let filled = (width as u32 * progress as u32) / total as u32; - let filled = filled as u16; + // 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))?; for _ in 0..filled { diff --git a/src/watch/state.rs b/src/watch/state.rs index 44cbd439..f93ea0cf 100644 --- a/src/watch/state.rs +++ b/src/watch/state.rs @@ -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, )?;