rustlings/src/list/state.rs

204 lines
5.3 KiB
Rust
Raw Normal View History

2024-04-09 20:37:39 +03:00
use anyhow::Result;
2024-04-07 20:05:29 +03:00
use ratatui::{
layout::{Constraint, Rect},
style::{Style, Stylize},
text::Span,
2024-04-09 20:37:39 +03:00
widgets::{Block, Borders, HighlightSpacing, Paragraph, Row, Table, TableState},
2024-04-07 20:05:29 +03:00
Frame,
};
2024-04-10 01:42:32 +03:00
use crate::{exercise::Exercise, progress_bar::progress_bar_ratatui, state_file::StateFile};
2024-04-07 20:05:29 +03:00
2024-04-08 03:41:48 +03:00
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum Filter {
Done,
Pending,
None,
}
2024-04-09 22:16:27 +03:00
pub struct UiState {
pub table: Table<'static>,
2024-04-08 02:33:11 +03:00
pub message: String,
2024-04-08 03:41:48 +03:00
pub filter: Filter,
2024-04-09 22:16:27 +03:00
exercises: &'static [Exercise],
2024-04-09 20:37:39 +03:00
progress: u16,
2024-04-07 20:05:29 +03:00
selected: usize,
table_state: TableState,
last_ind: usize,
}
2024-04-09 22:16:27 +03:00
impl UiState {
pub fn with_updated_rows(mut self, state_file: &StateFile) -> Self {
let mut rows_counter: usize = 0;
2024-04-09 20:37:39 +03:00
let mut progress: u16 = 0;
let rows = self
.exercises
2024-04-07 20:05:29 +03:00
.iter()
2024-04-08 03:41:48 +03:00
.zip(state_file.progress().iter().copied())
2024-04-07 20:05:29 +03:00
.enumerate()
.filter_map(|(ind, (exercise, done))| {
2024-04-09 20:37:39 +03:00
let exercise_state = if done {
progress += 1;
if self.filter == Filter::Pending {
return None;
}
"DONE".green()
} else {
if self.filter == Filter::Done {
return None;
}
"PENDING".yellow()
};
2024-04-08 03:41:48 +03:00
rows_counter += 1;
2024-04-08 04:08:05 +03:00
2024-04-07 20:05:29 +03:00
let next = if ind == state_file.next_exercise_ind() {
">>>>".bold().red()
} else {
Span::default()
};
2024-04-08 03:41:48 +03:00
Some(Row::new([
2024-04-07 20:05:29 +03:00
next,
exercise_state,
Span::raw(&exercise.name),
Span::raw(exercise.path.to_string_lossy()),
2024-04-08 03:41:48 +03:00
]))
});
2024-04-07 20:05:29 +03:00
2024-04-08 03:41:48 +03:00
self.table = self.table.rows(rows);
2024-04-08 04:08:05 +03:00
self.last_ind = rows_counter.saturating_sub(1);
self.select(self.selected.min(self.last_ind));
2024-04-09 20:37:39 +03:00
self.progress = progress;
2024-04-08 03:41:48 +03:00
self
}
2024-04-09 22:16:27 +03:00
pub fn new(state_file: &StateFile, exercises: &'static [Exercise]) -> Self {
2024-04-07 20:05:29 +03:00
let header = Row::new(["Next", "State", "Name", "Path"]);
let max_name_len = exercises
.iter()
.map(|exercise| exercise.name.len())
.max()
.unwrap_or(4) as u16;
let widths = [
Constraint::Length(4),
Constraint::Length(7),
Constraint::Length(max_name_len),
Constraint::Fill(1),
];
let table = Table::default()
.widths(widths)
2024-04-07 20:05:29 +03:00
.header(header)
.column_spacing(2)
.highlight_spacing(HighlightSpacing::Always)
.highlight_style(Style::new().bg(ratatui::style::Color::Rgb(50, 50, 50)))
.highlight_symbol("🦀")
.block(Block::default().borders(Borders::BOTTOM));
2024-04-08 02:49:38 +03:00
let selected = state_file.next_exercise_ind();
let table_state = TableState::default()
.with_offset(selected.saturating_sub(10))
2024-04-08 02:49:38 +03:00
.with_selected(Some(selected));
2024-04-07 20:05:29 +03:00
let slf = Self {
2024-04-07 20:05:29 +03:00
table,
2024-04-08 03:41:48 +03:00
message: String::with_capacity(128),
filter: Filter::None,
2024-04-08 03:41:48 +03:00
exercises,
2024-04-09 20:37:39 +03:00
progress: 0,
2024-04-07 20:05:29 +03:00
selected,
2024-04-08 02:49:38 +03:00
table_state,
last_ind: 0,
};
slf.with_updated_rows(state_file)
2024-04-07 20:05:29 +03:00
}
#[inline]
pub fn selected(&self) -> usize {
self.selected
}
fn select(&mut self, ind: usize) {
self.selected = ind;
self.table_state.select(Some(ind));
}
pub fn select_next(&mut self) {
self.select(self.selected.saturating_add(1).min(self.last_ind));
}
pub fn select_previous(&mut self) {
self.select(self.selected.saturating_sub(1));
}
#[inline]
pub fn select_first(&mut self) {
self.select(0);
}
#[inline]
pub fn select_last(&mut self) {
self.select(self.last_ind);
}
2024-04-09 20:37:39 +03:00
pub fn draw(&mut self, frame: &mut Frame) -> Result<()> {
2024-04-07 20:05:29 +03:00
let area = frame.size();
frame.render_stateful_widget(
&self.table,
Rect {
x: 0,
y: 0,
width: area.width,
2024-04-09 20:37:39 +03:00
height: area.height - 3,
2024-04-07 20:05:29 +03:00
},
&mut self.table_state,
);
2024-04-09 20:37:39 +03:00
frame.render_widget(
2024-04-10 01:42:32 +03:00
Paragraph::new(progress_bar_ratatui(
2024-04-09 20:37:39 +03:00
self.progress,
self.exercises.len() as u16,
area.width,
2024-04-10 01:42:32 +03:00
)?)
2024-04-09 20:37:39 +03:00
.block(Block::default().borders(Borders::BOTTOM)),
Rect {
x: 0,
y: area.height - 3,
width: area.width,
height: 2,
},
);
2024-04-08 02:33:11 +03:00
let message = if self.message.is_empty() {
// Help footer.
2024-04-08 04:21:13 +03:00
Span::raw(
"↓/j ↑/k home/g end/G │ filter <d>one/<p>ending │ <r>eset │ <c>ontinue at │ <q>uit",
)
2024-04-08 02:33:11 +03:00
} else {
2024-04-10 15:31:08 +03:00
self.message.as_str().light_blue()
2024-04-08 02:33:11 +03:00
};
2024-04-07 20:05:29 +03:00
frame.render_widget(
2024-04-08 04:21:13 +03:00
message,
2024-04-07 20:05:29 +03:00
Rect {
x: 0,
y: area.height - 1,
width: area.width,
height: 1,
},
);
2024-04-09 20:37:39 +03:00
Ok(())
2024-04-07 20:05:29 +03:00
}
}