2024-04-06 21:03:37 -04:00
|
|
|
use anyhow::Result;
|
|
|
|
use crossterm::{
|
2024-04-06 21:38:18 -04:00
|
|
|
event::{self, Event, KeyCode, KeyEventKind},
|
2024-04-06 21:03:37 -04:00
|
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
|
|
ExecutableCommand,
|
|
|
|
};
|
|
|
|
use ratatui::{
|
|
|
|
backend::CrosstermBackend,
|
2024-04-06 22:19:50 -04:00
|
|
|
layout::{Constraint, Rect},
|
2024-04-06 21:38:18 -04:00
|
|
|
style::{Style, Stylize},
|
2024-04-06 22:36:27 -04:00
|
|
|
text::Span,
|
2024-04-06 22:19:50 -04:00
|
|
|
widgets::{Block, Borders, HighlightSpacing, Row, Table, TableState},
|
2024-04-06 21:03:37 -04:00
|
|
|
Terminal,
|
|
|
|
};
|
2024-04-06 21:38:18 -04:00
|
|
|
use std::io;
|
2024-04-06 21:03:37 -04:00
|
|
|
|
|
|
|
use crate::{exercise::Exercise, state::State};
|
|
|
|
|
2024-04-07 07:12:40 -04:00
|
|
|
fn rows<'s, 'e, 'i>(
|
|
|
|
state: &'s State,
|
|
|
|
exercises: &'e [Exercise],
|
|
|
|
) -> impl Iterator<Item = Row<'e>> + 'i
|
2024-04-06 22:59:22 -04:00
|
|
|
where
|
2024-04-07 07:12:40 -04:00
|
|
|
's: 'i,
|
|
|
|
'e: 'i,
|
2024-04-06 22:59:22 -04:00
|
|
|
{
|
|
|
|
exercises
|
2024-04-06 21:03:37 -04:00
|
|
|
.iter()
|
2024-04-06 22:59:22 -04:00
|
|
|
.zip(state.progress())
|
2024-04-06 22:36:27 -04:00
|
|
|
.enumerate()
|
|
|
|
.map(|(ind, (exercise, done))| {
|
2024-04-06 22:59:22 -04:00
|
|
|
let next = if ind == state.next_exercise_ind() {
|
2024-04-06 22:36:27 -04:00
|
|
|
">>>>".bold().red()
|
|
|
|
} else {
|
|
|
|
Span::default()
|
|
|
|
};
|
|
|
|
|
2024-04-07 07:12:40 -04:00
|
|
|
let exercise_state = if *done {
|
|
|
|
"DONE".green()
|
|
|
|
} else {
|
|
|
|
"PENDING".yellow()
|
|
|
|
};
|
|
|
|
|
2024-04-06 21:03:37 -04:00
|
|
|
Row::new([
|
2024-04-06 22:36:27 -04:00
|
|
|
next,
|
|
|
|
exercise_state,
|
2024-04-06 21:03:37 -04:00
|
|
|
Span::raw(&exercise.name),
|
|
|
|
Span::raw(exercise.path.to_string_lossy()),
|
|
|
|
])
|
|
|
|
})
|
2024-04-06 22:59:22 -04:00
|
|
|
}
|
2024-04-06 21:03:37 -04:00
|
|
|
|
2024-04-06 22:59:22 -04:00
|
|
|
fn table<'a>(state: &State, exercises: &'a [Exercise]) -> Table<'a> {
|
|
|
|
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),
|
|
|
|
];
|
|
|
|
|
|
|
|
Table::new(rows(state, exercises), widths)
|
2024-04-06 21:03:37 -04:00
|
|
|
.header(header)
|
|
|
|
.column_spacing(2)
|
|
|
|
.highlight_spacing(HighlightSpacing::Always)
|
2024-04-06 21:38:18 -04:00
|
|
|
.highlight_style(Style::new().bg(ratatui::style::Color::Rgb(50, 50, 50)))
|
|
|
|
.highlight_symbol("🦀")
|
2024-04-06 22:19:50 -04:00
|
|
|
.block(Block::default().borders(Borders::BOTTOM))
|
2024-04-06 21:38:18 -04:00
|
|
|
}
|
2024-04-06 21:03:37 -04:00
|
|
|
|
2024-04-06 22:59:22 -04:00
|
|
|
pub fn list(state: &mut State, exercises: &[Exercise]) -> Result<()> {
|
2024-04-06 21:38:18 -04:00
|
|
|
let mut stdout = io::stdout().lock();
|
2024-04-06 21:03:37 -04:00
|
|
|
|
2024-04-06 21:38:18 -04:00
|
|
|
stdout.execute(EnterAlternateScreen)?;
|
|
|
|
enable_raw_mode()?;
|
|
|
|
|
|
|
|
let mut terminal = Terminal::new(CrosstermBackend::new(&mut stdout))?;
|
|
|
|
terminal.clear()?;
|
|
|
|
|
2024-04-06 22:59:22 -04:00
|
|
|
let mut table = table(state, exercises);
|
2024-04-06 21:38:18 -04:00
|
|
|
|
|
|
|
let last_ind = exercises.len() - 1;
|
|
|
|
let mut selected = 0;
|
|
|
|
let mut table_state = TableState::default().with_selected(Some(selected));
|
|
|
|
|
|
|
|
'outer: loop {
|
2024-04-06 21:03:37 -04:00
|
|
|
terminal.draw(|frame| {
|
|
|
|
let area = frame.size();
|
|
|
|
|
2024-04-06 22:19:50 -04:00
|
|
|
frame.render_stateful_widget(
|
|
|
|
&table,
|
|
|
|
Rect {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: area.width,
|
|
|
|
height: area.height - 1,
|
|
|
|
},
|
|
|
|
&mut table_state,
|
|
|
|
);
|
2024-04-06 22:39:03 -04:00
|
|
|
|
|
|
|
// Help footer
|
2024-04-06 22:19:50 -04:00
|
|
|
frame.render_widget(
|
2024-04-06 22:39:03 -04:00
|
|
|
Span::raw("↓/j ↑/k home/g end/G │ Filter <d>one/<p>ending │ <r>eset │ <c>ontinue at │ <q>uit"),
|
2024-04-06 22:19:50 -04:00
|
|
|
Rect {
|
|
|
|
x: 0,
|
|
|
|
y: area.height - 1,
|
|
|
|
width: area.width,
|
|
|
|
height: 1,
|
|
|
|
},
|
|
|
|
);
|
2024-04-06 21:03:37 -04:00
|
|
|
})?;
|
|
|
|
|
2024-04-06 21:38:18 -04:00
|
|
|
let key = loop {
|
|
|
|
match event::read()? {
|
2024-04-06 21:41:23 -04:00
|
|
|
Event::Key(key) => {
|
|
|
|
if key.kind != KeyEventKind::Press {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break key;
|
|
|
|
}
|
2024-04-06 21:38:18 -04:00
|
|
|
// Redraw
|
|
|
|
Event::Resize(_, _) => continue 'outer,
|
|
|
|
// Ignore
|
|
|
|
Event::FocusGained | Event::FocusLost | Event::Mouse(_) | Event::Paste(_) => (),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match key.code {
|
|
|
|
KeyCode::Char('q') => break,
|
|
|
|
KeyCode::Down | KeyCode::Char('j') => {
|
|
|
|
selected = selected.saturating_add(1).min(last_ind);
|
|
|
|
table_state.select(Some(selected));
|
|
|
|
}
|
|
|
|
KeyCode::Up | KeyCode::Char('k') => {
|
|
|
|
selected = selected.saturating_sub(1).max(0);
|
|
|
|
table_state.select(Some(selected));
|
|
|
|
}
|
|
|
|
KeyCode::Home | KeyCode::Char('g') => {
|
|
|
|
selected = 0;
|
|
|
|
table_state.select(Some(selected));
|
|
|
|
}
|
|
|
|
KeyCode::End | KeyCode::Char('G') => {
|
|
|
|
selected = last_ind;
|
|
|
|
table_state.select(Some(selected));
|
2024-04-06 21:03:37 -04:00
|
|
|
}
|
2024-04-06 22:59:22 -04:00
|
|
|
KeyCode::Char('c') => {
|
|
|
|
state.set_next_exercise_ind(selected)?;
|
|
|
|
table = table.rows(rows(state, exercises));
|
|
|
|
}
|
2024-04-06 21:38:18 -04:00
|
|
|
_ => (),
|
2024-04-06 21:03:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
drop(terminal);
|
|
|
|
stdout.execute(LeaveAlternateScreen)?;
|
|
|
|
disable_raw_mode()?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|