2024-08-17 10:34:43 -04:00
|
|
|
use anyhow::{Context, Result};
|
2024-07-25 10:26:48 -04:00
|
|
|
use ratatui::{
|
|
|
|
backend::CrosstermBackend,
|
|
|
|
crossterm::{
|
2024-08-17 10:34:43 -04:00
|
|
|
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind},
|
2024-07-25 10:26:48 -04:00
|
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
2024-08-17 10:34:43 -04:00
|
|
|
QueueableCommand,
|
2024-07-25 10:26:48 -04:00
|
|
|
},
|
|
|
|
Terminal,
|
2024-04-06 21:03:37 -04:00
|
|
|
};
|
2024-08-17 10:49:07 -04:00
|
|
|
use std::io::{self, StdoutLock, Write};
|
2024-04-06 21:03:37 -04:00
|
|
|
|
2024-04-10 20:51:02 -04:00
|
|
|
use crate::app_state::AppState;
|
2024-04-07 10:33:00 -04:00
|
|
|
|
2024-04-07 20:41:48 -04:00
|
|
|
use self::state::{Filter, UiState};
|
2024-04-06 21:03:37 -04:00
|
|
|
|
2024-04-18 05:31:08 -04:00
|
|
|
mod state;
|
|
|
|
|
2024-08-17 10:49:07 -04:00
|
|
|
fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()> {
|
|
|
|
let mut terminal = Terminal::new(CrosstermBackend::new(stdout))?;
|
2024-04-06 21:38:18 -04:00
|
|
|
terminal.clear()?;
|
|
|
|
|
2024-04-10 20:51:02 -04:00
|
|
|
let mut ui_state = UiState::new(app_state);
|
2024-04-06 21:38:18 -04:00
|
|
|
|
|
|
|
'outer: loop {
|
2024-08-08 20:17:01 -04:00
|
|
|
terminal.try_draw(|frame| ui_state.draw(frame).map_err(io::Error::other))?;
|
2024-04-06 21:03:37 -04:00
|
|
|
|
2024-04-06 21:38:18 -04:00
|
|
|
let key = loop {
|
2024-08-17 10:34:43 -04:00
|
|
|
match event::read().context("Failed to read terminal event")? {
|
2024-04-12 13:16:52 -04:00
|
|
|
Event::Key(key) => match key.kind {
|
|
|
|
KeyEventKind::Press | KeyEventKind::Repeat => break key,
|
|
|
|
KeyEventKind::Release => (),
|
|
|
|
},
|
2024-04-06 21:38:18 -04:00
|
|
|
// Redraw
|
|
|
|
Event::Resize(_, _) => continue 'outer,
|
|
|
|
// Ignore
|
|
|
|
Event::FocusGained | Event::FocusLost | Event::Mouse(_) | Event::Paste(_) => (),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-04-07 19:33:11 -04:00
|
|
|
ui_state.message.clear();
|
|
|
|
|
2024-04-06 21:38:18 -04:00
|
|
|
match key.code {
|
2024-04-28 17:22:11 -04:00
|
|
|
KeyCode::Char('q') => break,
|
2024-04-07 10:33:00 -04: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 20:41:48 -04:00
|
|
|
KeyCode::Char('d') => {
|
|
|
|
let message = if ui_state.filter == Filter::Done {
|
|
|
|
ui_state.filter = Filter::None;
|
|
|
|
"Disabled filter DONE"
|
|
|
|
} else {
|
|
|
|
ui_state.filter = Filter::Done;
|
|
|
|
"Enabled filter DONE │ Press d again to disable the filter"
|
|
|
|
};
|
|
|
|
|
2024-04-10 20:51:02 -04:00
|
|
|
ui_state = ui_state.with_updated_rows();
|
2024-04-07 20:41:48 -04:00
|
|
|
ui_state.message.push_str(message);
|
|
|
|
}
|
|
|
|
KeyCode::Char('p') => {
|
|
|
|
let message = if ui_state.filter == Filter::Pending {
|
|
|
|
ui_state.filter = Filter::None;
|
|
|
|
"Disabled filter PENDING"
|
|
|
|
} else {
|
|
|
|
ui_state.filter = Filter::Pending;
|
|
|
|
"Enabled filter PENDING │ Press p again to disable the filter"
|
|
|
|
};
|
|
|
|
|
2024-04-10 20:51:02 -04:00
|
|
|
ui_state = ui_state.with_updated_rows();
|
2024-04-07 20:41:48 -04:00
|
|
|
ui_state.message.push_str(message);
|
|
|
|
}
|
2024-04-07 16:43:59 -04:00
|
|
|
KeyCode::Char('r') => {
|
2024-04-13 19:15:43 -04:00
|
|
|
ui_state = ui_state.with_reset_selected()?;
|
2024-04-07 16:43:59 -04:00
|
|
|
}
|
2024-04-06 22:59:22 -04:00
|
|
|
KeyCode::Char('c') => {
|
2024-04-10 20:51:02 -04:00
|
|
|
ui_state.selected_to_current_exercise()?;
|
2024-08-19 17:29:17 -04:00
|
|
|
break;
|
2024-04-06 22:59:22 -04:00
|
|
|
}
|
2024-04-06 21:38:18 -04:00
|
|
|
_ => (),
|
2024-04-06 21:03:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-17 10:49:07 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn list(app_state: &mut AppState) -> Result<()> {
|
|
|
|
let mut stdout = io::stdout().lock();
|
|
|
|
stdout
|
|
|
|
.queue(EnterAlternateScreen)?
|
|
|
|
.queue(EnableMouseCapture)?
|
|
|
|
.flush()?;
|
|
|
|
enable_raw_mode()?;
|
|
|
|
|
|
|
|
let res = handle_list(app_state, &mut stdout);
|
|
|
|
|
|
|
|
// Restore the terminal even if we got an error.
|
2024-08-17 10:34:43 -04:00
|
|
|
stdout
|
|
|
|
.queue(LeaveAlternateScreen)?
|
|
|
|
.queue(DisableMouseCapture)?
|
|
|
|
.flush()?;
|
2024-04-06 21:03:37 -04:00
|
|
|
disable_raw_mode()?;
|
|
|
|
|
2024-08-17 10:49:07 -04:00
|
|
|
res
|
2024-04-06 21:03:37 -04:00
|
|
|
}
|