Add "Next" column

This commit is contained in:
mo8it 2024-04-07 04:36:27 +02:00
parent 7f5a18fa34
commit e640b4a1ff
2 changed files with 18 additions and 6 deletions

View file

@ -8,7 +8,7 @@ use ratatui::{
backend::CrosstermBackend, backend::CrosstermBackend,
layout::{Constraint, Rect}, layout::{Constraint, Rect},
style::{Style, Stylize}, style::{Style, Stylize},
text::{Line, Span}, text::Span,
widgets::{Block, Borders, HighlightSpacing, Row, Table, TableState}, widgets::{Block, Borders, HighlightSpacing, Row, Table, TableState},
Terminal, Terminal,
}; };
@ -17,7 +17,7 @@ use std::io;
use crate::{exercise::Exercise, state::State}; use crate::{exercise::Exercise, state::State};
fn table<'a>(state: &State, exercises: &'a [Exercise]) -> Table<'a> { 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 let max_name_len = exercises
.iter() .iter()
@ -26,6 +26,7 @@ fn table<'a>(state: &State, exercises: &'a [Exercise]) -> Table<'a> {
.unwrap_or(4) as u16; .unwrap_or(4) as u16;
let widths = [ let widths = [
Constraint::Length(4),
Constraint::Length(7), Constraint::Length(7),
Constraint::Length(max_name_len), Constraint::Length(max_name_len),
Constraint::Fill(1), Constraint::Fill(1),
@ -34,14 +35,23 @@ fn table<'a>(state: &State, exercises: &'a [Exercise]) -> Table<'a> {
let rows = exercises let rows = exercises
.iter() .iter()
.zip(&state.progress) .zip(&state.progress)
.map(|(exercise, done)| { .enumerate()
let state = if *done { .map(|(ind, (exercise, done))| {
let exercise_state = if *done {
"DONE".green() "DONE".green()
} else { } else {
"PENDING".yellow() "PENDING".yellow()
}; };
let next = if ind == state.next_exercise_ind {
">>>>".bold().red()
} else {
Span::default()
};
Row::new([ Row::new([
state, next,
exercise_state,
Span::raw(&exercise.name), Span::raw(&exercise.name),
Span::raw(exercise.path.to_string_lossy()), Span::raw(exercise.path.to_string_lossy()),
]) ])

View file

@ -6,6 +6,7 @@ use crate::exercise::Exercise;
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct State { pub struct State {
pub next_exercise_ind: usize,
pub progress: Vec<bool>, pub progress: Vec<bool>,
} }
@ -15,7 +16,7 @@ impl State {
let slf: Self = serde_json::de::from_slice(&file_content).ok()?; 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; return None;
} }
@ -24,6 +25,7 @@ impl State {
pub fn read_or_default(exercises: &[Exercise]) -> Self { pub fn read_or_default(exercises: &[Exercise]) -> Self {
Self::read(exercises).unwrap_or_else(|| Self { Self::read(exercises).unwrap_or_else(|| Self {
next_exercise_ind: 0,
progress: vec![false; exercises.len()], progress: vec![false; exercises.len()],
}) })
} }