rustlings/src/list/state.rs

238 lines
6.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,
};
use crate::{app_state::AppState, exercise::Exercise, 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,
}
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,
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
}
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;
let rows = self
.app_state
.exercises()
2024-04-07 20:05:29 +03:00
.iter()
.zip(self.app_state.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 {
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
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,
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
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
}
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"]);
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),
];
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));
let selected = app_state.current_exercise_ind();
2024-04-08 02:49:38 +03:00
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
2024-04-11 15:35:30 +03:00
let filter = Filter::None;
let n_rows = app_state.exercises().len();
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,
app_state,
2024-04-08 02:49:38 +03:00
table_state,
2024-04-11 15:35:30 +03:00
n_rows,
};
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
}
#[inline]
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
}
#[inline]
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-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(
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,
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
}
2024-04-11 15:35:30 +03:00
pub fn reset_selected(&mut self) -> Result<Option<&'static Exercise>> {
let Some(selected) = self.table_state.selected() else {
return Ok(None);
};
self.app_state.set_pending(selected)?;
// TODO: Take care of filters!
2024-04-11 15:35:30 +03:00
let exercise = &self.app_state.exercises()[selected];
exercise.reset()?;
2024-04-11 15:35:30 +03:00
Ok(Some(exercise))
}
#[inline]
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(());
};
// TODO: Take care of filters!
2024-04-11 15:35:30 +03:00
self.app_state.set_current_exercise_ind(selected)
}
2024-04-07 20:05:29 +03:00
}