2024-04-11 15:58:56 +03:00
|
|
|
use anyhow::{Context, Result};
|
2024-04-07 20:05:29 +03:00
|
|
|
use ratatui::{
|
|
|
|
layout::{Constraint, Rect},
|
|
|
|
style::{Style, Stylize},
|
2024-08-17 17:54:44 +03:00
|
|
|
text::{Span, Text},
|
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-08-17 17:54:44 +03:00
|
|
|
use std::{fmt::Write, mem};
|
2024-04-07 20:05:29 +03:00
|
|
|
|
2024-04-14 02:15:43 +03:00
|
|
|
use crate::{app_state::AppState, progress_bar::progress_bar_ratatui};
|
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-11 03:51:02 +03:00
|
|
|
pub struct UiState<'a> {
|
2024-04-09 22:16:27 +03:00
|
|
|
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-11 03:51:02 +03:00
|
|
|
app_state: &'a mut AppState,
|
2024-04-07 20:05:29 +03:00
|
|
|
table_state: TableState,
|
2024-04-11 15:35:30 +03:00
|
|
|
n_rows: usize,
|
2024-04-07 20:05:29 +03:00
|
|
|
}
|
|
|
|
|
2024-04-11 03:51:02 +03:00
|
|
|
impl<'a> UiState<'a> {
|
|
|
|
pub fn with_updated_rows(mut self) -> Self {
|
|
|
|
let current_exercise_ind = self.app_state.current_exercise_ind();
|
|
|
|
|
2024-04-11 15:35:30 +03:00
|
|
|
self.n_rows = 0;
|
2024-04-08 04:16:38 +03:00
|
|
|
let rows = self
|
2024-04-11 03:51:02 +03:00
|
|
|
.app_state
|
|
|
|
.exercises()
|
2024-04-07 20:05:29 +03:00
|
|
|
.iter()
|
|
|
|
.enumerate()
|
2024-04-14 02:15:43 +03:00
|
|
|
.filter_map(|(ind, exercise)| {
|
|
|
|
let exercise_state = if exercise.done {
|
2024-04-09 20:37:39 +03:00
|
|
|
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
|
|
|
|
2024-04-11 15:35:30 +03:00
|
|
|
self.n_rows += 1;
|
2024-04-08 04:08:05 +03:00
|
|
|
|
2024-04-11 03:51:02 +03:00
|
|
|
let next = if ind == current_exercise_ind {
|
2024-04-07 20:05:29 +03:00
|
|
|
">>>>".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,
|
2024-04-14 02:15:43 +03:00
|
|
|
Span::raw(exercise.name),
|
2024-04-14 03:41:19 +03:00
|
|
|
Span::raw(exercise.path),
|
2024-04-08 03:41:48 +03:00
|
|
|
]))
|
2024-04-08 04:16:38 +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
|
|
|
|
2024-04-11 15:35:30 +03:00
|
|
|
if self.n_rows == 0 {
|
|
|
|
self.table_state.select(None);
|
|
|
|
} else {
|
|
|
|
self.table_state.select(Some(
|
|
|
|
self.table_state
|
|
|
|
.selected()
|
|
|
|
.map_or(0, |selected| selected.min(self.n_rows - 1)),
|
|
|
|
));
|
|
|
|
}
|
2024-04-08 04:08:05 +03:00
|
|
|
|
2024-04-08 03:41:48 +03:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-04-11 03:51:02 +03:00
|
|
|
pub fn new(app_state: &'a mut AppState) -> Self {
|
2024-04-07 20:05:29 +03:00
|
|
|
let header = Row::new(["Next", "State", "Name", "Path"]);
|
|
|
|
|
2024-04-11 03:51:02 +03:00
|
|
|
let max_name_len = app_state
|
|
|
|
.exercises()
|
2024-04-07 20:05:29 +03:00
|
|
|
.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),
|
|
|
|
];
|
|
|
|
|
2024-04-08 04:16:38 +03:00
|
|
|
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-11 03:51:02 +03:00
|
|
|
let selected = app_state.current_exercise_ind();
|
2024-04-08 02:49:38 +03:00
|
|
|
let table_state = TableState::default()
|
2024-04-08 03:46:35 +03:00
|
|
|
.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
|
|
|
|
2024-04-11 15:35:30 +03:00
|
|
|
let filter = Filter::None;
|
|
|
|
let n_rows = app_state.exercises().len();
|
|
|
|
|
2024-04-08 04:16:38 +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),
|
2024-04-11 15:35:30 +03:00
|
|
|
filter,
|
2024-04-11 03:51:02 +03:00
|
|
|
app_state,
|
2024-04-08 02:49:38 +03:00
|
|
|
table_state,
|
2024-04-11 15:35:30 +03:00
|
|
|
n_rows,
|
2024-04-08 04:16:38 +03:00
|
|
|
};
|
|
|
|
|
2024-04-11 03:51:02 +03:00
|
|
|
slf.with_updated_rows()
|
2024-04-07 20:05:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn select_next(&mut self) {
|
2024-04-11 15:35:30 +03:00
|
|
|
if self.n_rows > 0 {
|
|
|
|
let next = self
|
|
|
|
.table_state
|
|
|
|
.selected()
|
|
|
|
.map_or(0, |selected| (selected + 1).min(self.n_rows - 1));
|
|
|
|
self.table_state.select(Some(next));
|
|
|
|
}
|
2024-04-07 20:05:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn select_previous(&mut self) {
|
2024-04-11 15:35:30 +03:00
|
|
|
if self.n_rows > 0 {
|
|
|
|
let previous = self
|
|
|
|
.table_state
|
|
|
|
.selected()
|
|
|
|
.map_or(0, |selected| selected.saturating_sub(1));
|
|
|
|
self.table_state.select(Some(previous));
|
|
|
|
}
|
2024-04-07 20:05:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn select_first(&mut self) {
|
2024-04-11 15:35:30 +03:00
|
|
|
if self.n_rows > 0 {
|
|
|
|
self.table_state.select(Some(0));
|
|
|
|
}
|
2024-04-07 20:05:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn select_last(&mut self) {
|
2024-04-11 15:35:30 +03:00
|
|
|
if self.n_rows > 0 {
|
|
|
|
self.table_state.select(Some(self.n_rows - 1));
|
|
|
|
}
|
2024-04-07 20:05:29 +03:00
|
|
|
}
|
|
|
|
|
2024-04-09 20:37:39 +03:00
|
|
|
pub fn draw(&mut self, frame: &mut Frame) -> Result<()> {
|
2024-08-09 03:17:01 +03:00
|
|
|
let area = frame.area();
|
2024-08-17 17:54:44 +03:00
|
|
|
let narrow = area.width < 95;
|
|
|
|
let narrow_u16 = u16::from(narrow);
|
|
|
|
let table_height = area.height - 3 - narrow_u16;
|
2024-04-07 20:05:29 +03:00
|
|
|
|
|
|
|
frame.render_stateful_widget(
|
|
|
|
&self.table,
|
|
|
|
Rect {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: area.width,
|
2024-08-17 17:54:44 +03:00
|
|
|
height: table_height,
|
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-11 03:51:02 +03:00
|
|
|
self.app_state.n_done(),
|
|
|
|
self.app_state.exercises().len() as u16,
|
2024-04-09 20:37:39 +03:00
|
|
|
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,
|
2024-08-17 17:54:44 +03:00
|
|
|
y: table_height,
|
2024-04-09 20:37:39 +03:00
|
|
|
width: area.width,
|
|
|
|
height: 2,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2024-04-08 02:33:11 +03:00
|
|
|
let message = if self.message.is_empty() {
|
|
|
|
// Help footer.
|
2024-08-17 17:54:44 +03:00
|
|
|
let mut text = Text::default();
|
2024-04-29 00:21:13 +03:00
|
|
|
let mut spans = Vec::with_capacity(4);
|
|
|
|
spans.push(Span::raw(
|
2024-08-17 17:54:44 +03:00
|
|
|
"↓/j ↑/k home/g end/G │ <c>ontinue at │ <r>eset exercise │",
|
2024-04-29 00:21:13 +03:00
|
|
|
));
|
2024-08-17 17:54:44 +03:00
|
|
|
|
|
|
|
if narrow {
|
|
|
|
text.push_line(mem::take(&mut spans));
|
|
|
|
spans.push(Span::raw("filter "));
|
|
|
|
} else {
|
|
|
|
spans.push(Span::raw(" filter "));
|
|
|
|
}
|
|
|
|
|
2024-04-29 00:21:13 +03:00
|
|
|
match self.filter {
|
|
|
|
Filter::Done => {
|
|
|
|
spans.push("<d>one".underlined().magenta());
|
|
|
|
spans.push(Span::raw("/<p>ending"));
|
|
|
|
}
|
|
|
|
Filter::Pending => {
|
|
|
|
spans.push(Span::raw("<d>one/"));
|
|
|
|
spans.push("<p>ending".underlined().magenta());
|
|
|
|
}
|
|
|
|
Filter::None => spans.push(Span::raw("<d>one/<p>ending")),
|
|
|
|
}
|
2024-08-17 17:54:44 +03:00
|
|
|
|
2024-08-17 16:53:34 +03:00
|
|
|
spans.push(Span::raw(" │ <q>uit list"));
|
2024-08-17 17:54:44 +03:00
|
|
|
text.push_line(spans);
|
|
|
|
text
|
2024-04-08 02:33:11 +03:00
|
|
|
} else {
|
2024-08-17 17:54:44 +03:00
|
|
|
Text::from(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,
|
2024-08-17 17:54:44 +03:00
|
|
|
y: table_height + 2,
|
2024-04-07 20:05:29 +03:00
|
|
|
width: area.width,
|
2024-08-17 17:54:44 +03:00
|
|
|
height: 1 + narrow_u16,
|
2024-04-07 20:05:29 +03:00
|
|
|
},
|
|
|
|
);
|
2024-04-09 20:37:39 +03:00
|
|
|
|
|
|
|
Ok(())
|
2024-04-07 20:05:29 +03:00
|
|
|
}
|
2024-04-11 03:51:02 +03:00
|
|
|
|
2024-04-14 02:15:43 +03:00
|
|
|
pub fn with_reset_selected(mut self) -> Result<Self> {
|
2024-04-11 15:35:30 +03:00
|
|
|
let Some(selected) = self.table_state.selected() else {
|
2024-04-14 02:15:43 +03:00
|
|
|
return Ok(self);
|
2024-04-11 15:35:30 +03:00
|
|
|
};
|
|
|
|
|
2024-04-18 13:41:17 +03:00
|
|
|
let ind = self
|
2024-04-11 15:58:56 +03:00
|
|
|
.app_state
|
|
|
|
.exercises()
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
2024-04-14 02:15:43 +03:00
|
|
|
.filter_map(|(ind, exercise)| match self.filter {
|
2024-04-18 13:41:17 +03:00
|
|
|
Filter::Done => exercise.done.then_some(ind),
|
|
|
|
Filter::Pending => (!exercise.done).then_some(ind),
|
|
|
|
Filter::None => Some(ind),
|
2024-04-11 15:58:56 +03:00
|
|
|
})
|
|
|
|
.nth(selected)
|
|
|
|
.context("Invalid selection index")?;
|
|
|
|
|
2024-04-18 13:41:17 +03:00
|
|
|
let exercise_path = self.app_state.reset_exercise_by_ind(ind)?;
|
2024-04-25 03:03:26 +03:00
|
|
|
write!(self.message, "The exercise {exercise_path} has been reset")?;
|
2024-04-11 03:51:02 +03:00
|
|
|
|
2024-04-14 02:15:43 +03:00
|
|
|
Ok(self.with_updated_rows())
|
2024-04-11 03:51:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn selected_to_current_exercise(&mut self) -> Result<()> {
|
2024-04-11 15:35:30 +03:00
|
|
|
let Some(selected) = self.table_state.selected() else {
|
|
|
|
return Ok(());
|
|
|
|
};
|
|
|
|
|
2024-04-11 15:58:56 +03:00
|
|
|
let ind = self
|
|
|
|
.app_state
|
2024-04-14 02:15:43 +03:00
|
|
|
.exercises()
|
2024-04-11 15:58:56 +03:00
|
|
|
.iter()
|
|
|
|
.enumerate()
|
2024-04-14 02:15:43 +03:00
|
|
|
.filter_map(|(ind, exercise)| match self.filter {
|
|
|
|
Filter::Done => exercise.done.then_some(ind),
|
|
|
|
Filter::Pending => (!exercise.done).then_some(ind),
|
2024-04-11 15:58:56 +03:00
|
|
|
Filter::None => Some(ind),
|
|
|
|
})
|
|
|
|
.nth(selected)
|
|
|
|
.context("Invalid selection index")?;
|
|
|
|
|
|
|
|
self.app_state.set_current_exercise_ind(ind)
|
2024-04-11 03:51:02 +03:00
|
|
|
}
|
2024-04-07 20:05:29 +03:00
|
|
|
}
|