mirror of
https://github.com/notohh/rustlings.git
synced 2024-11-22 05:52:23 -05:00
Add progress bar to list
This commit is contained in:
parent
ee7d976283
commit
850c1d0234
4 changed files with 85 additions and 15 deletions
|
@ -24,7 +24,7 @@ pub fn list(state_file: &mut StateFile, exercises: &[Exercise]) -> Result<()> {
|
||||||
let mut ui_state = UiState::new(state_file, exercises);
|
let mut ui_state = UiState::new(state_file, exercises);
|
||||||
|
|
||||||
'outer: loop {
|
'outer: loop {
|
||||||
terminal.draw(|frame| ui_state.draw(frame))?;
|
terminal.draw(|frame| ui_state.draw(frame).unwrap())?;
|
||||||
|
|
||||||
let key = loop {
|
let key = loop {
|
||||||
match event::read()? {
|
match event::read()? {
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
|
use anyhow::Result;
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
layout::{Constraint, Rect},
|
layout::{Constraint, Rect},
|
||||||
style::{Style, Stylize},
|
style::{Style, Stylize},
|
||||||
text::Span,
|
text::Span,
|
||||||
widgets::{Block, Borders, HighlightSpacing, Row, Table, TableState},
|
widgets::{Block, Borders, HighlightSpacing, Paragraph, Row, Table, TableState},
|
||||||
Frame,
|
Frame,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{exercise::Exercise, state_file::StateFile};
|
use crate::{exercise::Exercise, progress_bar::progress_bar, state_file::StateFile};
|
||||||
|
|
||||||
#[derive(Copy, Clone, PartialEq, Eq)]
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||||
pub enum Filter {
|
pub enum Filter {
|
||||||
|
@ -20,6 +21,7 @@ pub struct UiState<'a> {
|
||||||
pub message: String,
|
pub message: String,
|
||||||
pub filter: Filter,
|
pub filter: Filter,
|
||||||
exercises: &'a [Exercise],
|
exercises: &'a [Exercise],
|
||||||
|
progress: u16,
|
||||||
selected: usize,
|
selected: usize,
|
||||||
table_state: TableState,
|
table_state: TableState,
|
||||||
last_ind: usize,
|
last_ind: usize,
|
||||||
|
@ -28,17 +30,29 @@ pub struct UiState<'a> {
|
||||||
impl<'a> UiState<'a> {
|
impl<'a> UiState<'a> {
|
||||||
pub fn with_updated_rows(mut self, state_file: &StateFile) -> Self {
|
pub fn with_updated_rows(mut self, state_file: &StateFile) -> Self {
|
||||||
let mut rows_counter: usize = 0;
|
let mut rows_counter: usize = 0;
|
||||||
|
let mut progress: u16 = 0;
|
||||||
let rows = self
|
let rows = self
|
||||||
.exercises
|
.exercises
|
||||||
.iter()
|
.iter()
|
||||||
.zip(state_file.progress().iter().copied())
|
.zip(state_file.progress().iter().copied())
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.filter_map(|(ind, (exercise, done))| {
|
.filter_map(|(ind, (exercise, done))| {
|
||||||
match (self.filter, done) {
|
let exercise_state = if done {
|
||||||
(Filter::Done, false) | (Filter::Pending, true) => return None,
|
progress += 1;
|
||||||
_ => (),
|
|
||||||
|
if self.filter == Filter::Pending {
|
||||||
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
"DONE".green()
|
||||||
|
} else {
|
||||||
|
if self.filter == Filter::Done {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
"PENDING".yellow()
|
||||||
|
};
|
||||||
|
|
||||||
rows_counter += 1;
|
rows_counter += 1;
|
||||||
|
|
||||||
let next = if ind == state_file.next_exercise_ind() {
|
let next = if ind == state_file.next_exercise_ind() {
|
||||||
|
@ -47,12 +61,6 @@ impl<'a> UiState<'a> {
|
||||||
Span::default()
|
Span::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let exercise_state = if done {
|
|
||||||
"DONE".green()
|
|
||||||
} else {
|
|
||||||
"PENDING".yellow()
|
|
||||||
};
|
|
||||||
|
|
||||||
Some(Row::new([
|
Some(Row::new([
|
||||||
next,
|
next,
|
||||||
exercise_state,
|
exercise_state,
|
||||||
|
@ -66,6 +74,8 @@ impl<'a> UiState<'a> {
|
||||||
self.last_ind = rows_counter.saturating_sub(1);
|
self.last_ind = rows_counter.saturating_sub(1);
|
||||||
self.select(self.selected.min(self.last_ind));
|
self.select(self.selected.min(self.last_ind));
|
||||||
|
|
||||||
|
self.progress = progress;
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,6 +114,7 @@ impl<'a> UiState<'a> {
|
||||||
message: String::with_capacity(128),
|
message: String::with_capacity(128),
|
||||||
filter: Filter::None,
|
filter: Filter::None,
|
||||||
exercises,
|
exercises,
|
||||||
|
progress: 0,
|
||||||
selected,
|
selected,
|
||||||
table_state,
|
table_state,
|
||||||
last_ind: 0,
|
last_ind: 0,
|
||||||
|
@ -140,7 +151,7 @@ impl<'a> UiState<'a> {
|
||||||
self.select(self.last_ind);
|
self.select(self.last_ind);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(&mut self, frame: &mut Frame) {
|
pub fn draw(&mut self, frame: &mut Frame) -> Result<()> {
|
||||||
let area = frame.size();
|
let area = frame.size();
|
||||||
|
|
||||||
frame.render_stateful_widget(
|
frame.render_stateful_widget(
|
||||||
|
@ -149,11 +160,26 @@ impl<'a> UiState<'a> {
|
||||||
x: 0,
|
x: 0,
|
||||||
y: 0,
|
y: 0,
|
||||||
width: area.width,
|
width: area.width,
|
||||||
height: area.height - 1,
|
height: area.height - 3,
|
||||||
},
|
},
|
||||||
&mut self.table_state,
|
&mut self.table_state,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
frame.render_widget(
|
||||||
|
Paragraph::new(Span::raw(progress_bar(
|
||||||
|
self.progress,
|
||||||
|
self.exercises.len() as u16,
|
||||||
|
area.width,
|
||||||
|
)?))
|
||||||
|
.block(Block::default().borders(Borders::BOTTOM)),
|
||||||
|
Rect {
|
||||||
|
x: 0,
|
||||||
|
y: area.height - 3,
|
||||||
|
width: area.width,
|
||||||
|
height: 2,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
let message = if self.message.is_empty() {
|
let message = if self.message.is_empty() {
|
||||||
// Help footer.
|
// Help footer.
|
||||||
Span::raw(
|
Span::raw(
|
||||||
|
@ -171,5 +197,7 @@ impl<'a> UiState<'a> {
|
||||||
height: 1,
|
height: 1,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ mod embedded;
|
||||||
mod exercise;
|
mod exercise;
|
||||||
mod init;
|
mod init;
|
||||||
mod list;
|
mod list;
|
||||||
|
mod progress_bar;
|
||||||
mod run;
|
mod run;
|
||||||
mod state_file;
|
mod state_file;
|
||||||
mod verify;
|
mod verify;
|
||||||
|
|
41
src/progress_bar.rs
Normal file
41
src/progress_bar.rs
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
use anyhow::{bail, Result};
|
||||||
|
use std::fmt::Write;
|
||||||
|
|
||||||
|
pub fn progress_bar(progress: u16, total: u16, line_width: u16) -> Result<String> {
|
||||||
|
if progress > total {
|
||||||
|
bail!("The progress of the progress bar is higher than the maximum");
|
||||||
|
}
|
||||||
|
|
||||||
|
// "Progress: [".len() == 11
|
||||||
|
// "] xxx/xxx".len() == 9
|
||||||
|
// 11 + 9 = 20
|
||||||
|
let wrapper_width = 20;
|
||||||
|
|
||||||
|
// If the line width is too low for a progress bar, just show the ratio.
|
||||||
|
if line_width < wrapper_width + 4 {
|
||||||
|
return Ok(format!("Progress: {progress}/{total}"));
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut line = String::with_capacity(usize::from(line_width));
|
||||||
|
line.push_str("Progress: [");
|
||||||
|
|
||||||
|
let remaining_width = line_width.saturating_sub(wrapper_width);
|
||||||
|
let filled = (remaining_width * progress) / total;
|
||||||
|
|
||||||
|
for _ in 0..filled {
|
||||||
|
line.push('=');
|
||||||
|
}
|
||||||
|
|
||||||
|
if filled < remaining_width {
|
||||||
|
line.push('>');
|
||||||
|
}
|
||||||
|
|
||||||
|
for _ in 0..(remaining_width - filled).saturating_sub(1) {
|
||||||
|
line.push(' ');
|
||||||
|
}
|
||||||
|
|
||||||
|
line.write_fmt(format_args!("] {progress:>3}/{total:<3}"))
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
Ok(line)
|
||||||
|
}
|
Loading…
Reference in a new issue