2024-04-07 04:03:37 +03:00
|
|
|
use anyhow::Result;
|
|
|
|
use crossterm::{
|
2024-04-07 04:38:18 +03:00
|
|
|
event::{self, Event, KeyCode, KeyEventKind},
|
2024-04-07 04:03:37 +03:00
|
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
|
|
ExecutableCommand,
|
|
|
|
};
|
|
|
|
use ratatui::{
|
|
|
|
backend::CrosstermBackend,
|
2024-04-07 05:19:50 +03:00
|
|
|
layout::{Constraint, Rect},
|
2024-04-07 04:38:18 +03:00
|
|
|
style::{Style, Stylize},
|
2024-04-07 05:36:27 +03:00
|
|
|
text::Span,
|
2024-04-07 05:19:50 +03:00
|
|
|
widgets::{Block, Borders, HighlightSpacing, Row, Table, TableState},
|
2024-04-07 17:33:00 +03:00
|
|
|
Frame, Terminal,
|
2024-04-07 04:03:37 +03:00
|
|
|
};
|
2024-04-07 04:38:18 +03:00
|
|
|
use std::io;
|
2024-04-07 04:03:37 +03:00
|
|
|
|
2024-04-07 20:01:08 +03:00
|
|
|
use crate::{exercise::Exercise, state_file::StateFile};
|
2024-04-07 04:03:37 +03:00
|
|
|
|
2024-04-07 17:33:00 +03:00
|
|
|
struct UiState<'a> {
|
|
|
|
pub table: Table<'a>,
|
|
|
|
selected: usize,
|
|
|
|
table_state: TableState,
|
|
|
|
last_ind: usize,
|
2024-04-07 05:59:22 +03:00
|
|
|
}
|
2024-04-07 04:03:37 +03:00
|
|
|
|
2024-04-07 17:33:00 +03:00
|
|
|
impl<'a> UiState<'a> {
|
|
|
|
pub fn rows<'s, 'i>(
|
2024-04-07 20:01:08 +03:00
|
|
|
state_file: &'s StateFile,
|
2024-04-07 17:33:00 +03:00
|
|
|
exercises: &'a [Exercise],
|
|
|
|
) -> impl Iterator<Item = Row<'a>> + 'i
|
|
|
|
where
|
|
|
|
's: 'i,
|
|
|
|
'a: 'i,
|
|
|
|
{
|
|
|
|
exercises
|
|
|
|
.iter()
|
2024-04-07 20:01:08 +03:00
|
|
|
.zip(state_file.progress())
|
2024-04-07 17:33:00 +03:00
|
|
|
.enumerate()
|
|
|
|
.map(|(ind, (exercise, done))| {
|
2024-04-07 20:01:08 +03:00
|
|
|
let next = if ind == state_file.next_exercise_ind() {
|
2024-04-07 17:33:00 +03:00
|
|
|
">>>>".bold().red()
|
|
|
|
} else {
|
|
|
|
Span::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
let exercise_state = if *done {
|
|
|
|
"DONE".green()
|
|
|
|
} else {
|
|
|
|
"PENDING".yellow()
|
|
|
|
};
|
|
|
|
|
|
|
|
Row::new([
|
|
|
|
next,
|
|
|
|
exercise_state,
|
|
|
|
Span::raw(&exercise.name),
|
|
|
|
Span::raw(exercise.path.to_string_lossy()),
|
|
|
|
])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-04-07 20:01:08 +03:00
|
|
|
pub fn new(state_file: &StateFile, exercises: &'a [Exercise]) -> Self {
|
2024-04-07 17:33:00 +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),
|
|
|
|
];
|
|
|
|
|
2024-04-07 20:01:08 +03:00
|
|
|
let rows = Self::rows(state_file, exercises);
|
2024-04-07 17:33:00 +03:00
|
|
|
|
|
|
|
let table = Table::new(rows, widths)
|
|
|
|
.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 = 0;
|
|
|
|
let table_state = TableState::default().with_selected(Some(selected));
|
|
|
|
let last_ind = exercises.len() - 1;
|
|
|
|
|
|
|
|
Self {
|
|
|
|
table,
|
|
|
|
selected,
|
|
|
|
table_state,
|
|
|
|
last_ind,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn draw(&mut self, frame: &mut Frame) {
|
|
|
|
let area = frame.size();
|
|
|
|
|
|
|
|
frame.render_stateful_widget(
|
|
|
|
&self.table,
|
|
|
|
Rect {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: area.width,
|
|
|
|
height: area.height - 1,
|
|
|
|
},
|
|
|
|
&mut self.table_state,
|
|
|
|
);
|
|
|
|
|
2024-04-07 18:57:20 +03:00
|
|
|
let help_footer =
|
2024-04-07 17:33:00 +03:00
|
|
|
"↓/j ↑/k home/g end/G │ Filter <d>one/<p>ending │ <r>eset │ <c>ontinue at │ <q>uit";
|
|
|
|
frame.render_widget(
|
2024-04-07 18:57:20 +03:00
|
|
|
Span::raw(help_footer),
|
2024-04-07 17:33:00 +03:00
|
|
|
Rect {
|
|
|
|
x: 0,
|
|
|
|
y: area.height - 1,
|
|
|
|
width: area.width,
|
|
|
|
height: 1,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2024-04-07 04:38:18 +03:00
|
|
|
}
|
2024-04-07 04:03:37 +03:00
|
|
|
|
2024-04-07 20:01:08 +03:00
|
|
|
pub fn list(state_file: &mut StateFile, exercises: &[Exercise]) -> Result<()> {
|
2024-04-07 04:38:18 +03:00
|
|
|
let mut stdout = io::stdout().lock();
|
|
|
|
stdout.execute(EnterAlternateScreen)?;
|
|
|
|
enable_raw_mode()?;
|
|
|
|
|
|
|
|
let mut terminal = Terminal::new(CrosstermBackend::new(&mut stdout))?;
|
|
|
|
terminal.clear()?;
|
|
|
|
|
2024-04-07 20:01:08 +03:00
|
|
|
let mut ui_state = UiState::new(state_file, exercises);
|
2024-04-07 04:38:18 +03:00
|
|
|
|
|
|
|
'outer: loop {
|
2024-04-07 17:33:00 +03:00
|
|
|
terminal.draw(|frame| ui_state.draw(frame))?;
|
2024-04-07 04:03:37 +03:00
|
|
|
|
2024-04-07 04:38:18 +03:00
|
|
|
let key = loop {
|
|
|
|
match event::read()? {
|
2024-04-07 04:41:23 +03:00
|
|
|
Event::Key(key) => {
|
|
|
|
if key.kind != KeyEventKind::Press {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break key;
|
|
|
|
}
|
2024-04-07 04:38:18 +03:00
|
|
|
// Redraw
|
|
|
|
Event::Resize(_, _) => continue 'outer,
|
|
|
|
// Ignore
|
|
|
|
Event::FocusGained | Event::FocusLost | Event::Mouse(_) | Event::Paste(_) => (),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match key.code {
|
|
|
|
KeyCode::Char('q') => break,
|
2024-04-07 17:33:00 +03:00
|
|
|
KeyCode::Down | KeyCode::Char('j') => ui_state.select_next(),
|
|
|
|
KeyCode::Up | KeyCode::Char('k') => ui_state.select_previous(),
|
|
|
|
KeyCode::Home | KeyCode::Char('g') => ui_state.select_first(),
|
|
|
|
KeyCode::End | KeyCode::Char('G') => ui_state.select_last(),
|
2024-04-07 05:59:22 +03:00
|
|
|
KeyCode::Char('c') => {
|
2024-04-07 20:01:08 +03:00
|
|
|
state_file.set_next_exercise_ind(ui_state.selected)?;
|
|
|
|
ui_state.table = ui_state.table.rows(UiState::rows(state_file, exercises));
|
2024-04-07 05:59:22 +03:00
|
|
|
}
|
2024-04-07 04:38:18 +03:00
|
|
|
_ => (),
|
2024-04-07 04:03:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
drop(terminal);
|
|
|
|
stdout.execute(LeaveAlternateScreen)?;
|
|
|
|
disable_raw_mode()?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|