mirror of
https://github.com/rust-lang/rustlings
synced 2024-11-10 13:10:40 +00:00
Add "Next" column
This commit is contained in:
parent
7f5a18fa34
commit
e640b4a1ff
20
src/list.rs
20
src/list.rs
@ -8,7 +8,7 @@ use ratatui::{
|
||||
backend::CrosstermBackend,
|
||||
layout::{Constraint, Rect},
|
||||
style::{Style, Stylize},
|
||||
text::{Line, Span},
|
||||
text::Span,
|
||||
widgets::{Block, Borders, HighlightSpacing, Row, Table, TableState},
|
||||
Terminal,
|
||||
};
|
||||
@ -17,7 +17,7 @@ use std::io;
|
||||
use crate::{exercise::Exercise, state::State};
|
||||
|
||||
fn table<'a>(state: &State, exercises: &'a [Exercise]) -> Table<'a> {
|
||||
let header = Row::new(["State", "Name", "Path"]);
|
||||
let header = Row::new(["Next", "State", "Name", "Path"]);
|
||||
|
||||
let max_name_len = exercises
|
||||
.iter()
|
||||
@ -26,6 +26,7 @@ fn table<'a>(state: &State, exercises: &'a [Exercise]) -> Table<'a> {
|
||||
.unwrap_or(4) as u16;
|
||||
|
||||
let widths = [
|
||||
Constraint::Length(4),
|
||||
Constraint::Length(7),
|
||||
Constraint::Length(max_name_len),
|
||||
Constraint::Fill(1),
|
||||
@ -34,14 +35,23 @@ fn table<'a>(state: &State, exercises: &'a [Exercise]) -> Table<'a> {
|
||||
let rows = exercises
|
||||
.iter()
|
||||
.zip(&state.progress)
|
||||
.map(|(exercise, done)| {
|
||||
let state = if *done {
|
||||
.enumerate()
|
||||
.map(|(ind, (exercise, done))| {
|
||||
let exercise_state = if *done {
|
||||
"DONE".green()
|
||||
} else {
|
||||
"PENDING".yellow()
|
||||
};
|
||||
|
||||
let next = if ind == state.next_exercise_ind {
|
||||
">>>>".bold().red()
|
||||
} else {
|
||||
Span::default()
|
||||
};
|
||||
|
||||
Row::new([
|
||||
state,
|
||||
next,
|
||||
exercise_state,
|
||||
Span::raw(&exercise.name),
|
||||
Span::raw(exercise.path.to_string_lossy()),
|
||||
])
|
||||
|
@ -6,6 +6,7 @@ use crate::exercise::Exercise;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct State {
|
||||
pub next_exercise_ind: usize,
|
||||
pub progress: Vec<bool>,
|
||||
}
|
||||
|
||||
@ -15,7 +16,7 @@ impl State {
|
||||
|
||||
let slf: Self = serde_json::de::from_slice(&file_content).ok()?;
|
||||
|
||||
if slf.progress.len() != exercises.len() {
|
||||
if slf.progress.len() != exercises.len() || slf.next_exercise_ind >= exercises.len() {
|
||||
return None;
|
||||
}
|
||||
|
||||
@ -24,6 +25,7 @@ impl State {
|
||||
|
||||
pub fn read_or_default(exercises: &[Exercise]) -> Self {
|
||||
Self::read(exercises).unwrap_or_else(|| Self {
|
||||
next_exercise_ind: 0,
|
||||
progress: vec![false; exercises.len()],
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user