2024-08-17 10:34:43 -04:00
|
|
|
use anyhow::{Context, Result};
|
2024-08-23 18:14:12 -04:00
|
|
|
use crossterm::{
|
|
|
|
cursor,
|
|
|
|
event::{
|
|
|
|
self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind, MouseEventKind,
|
2024-07-25 10:26:48 -04:00
|
|
|
},
|
2024-08-23 18:14:12 -04:00
|
|
|
terminal::{
|
|
|
|
disable_raw_mode, enable_raw_mode, DisableLineWrap, EnableLineWrap, EnterAlternateScreen,
|
|
|
|
LeaveAlternateScreen,
|
|
|
|
},
|
|
|
|
QueueableCommand,
|
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-08-23 18:14:12 -04:00
|
|
|
use self::state::{Filter, ListState};
|
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<()> {
|
2024-08-23 18:14:12 -04:00
|
|
|
let mut list_state = ListState::new(app_state, stdout)?;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
match event::read().context("Failed to read terminal event")? {
|
|
|
|
Event::Key(key) => {
|
|
|
|
match key.kind {
|
|
|
|
KeyEventKind::Release => continue,
|
|
|
|
KeyEventKind::Press | KeyEventKind::Repeat => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
list_state.message.clear();
|
|
|
|
|
|
|
|
match key.code {
|
|
|
|
KeyCode::Char('q') => return Ok(()),
|
|
|
|
KeyCode::Down | KeyCode::Char('j') => list_state.select_next(),
|
|
|
|
KeyCode::Up | KeyCode::Char('k') => list_state.select_previous(),
|
|
|
|
KeyCode::Home | KeyCode::Char('g') => list_state.select_first(),
|
|
|
|
KeyCode::End | KeyCode::Char('G') => list_state.select_last(),
|
|
|
|
KeyCode::Char('d') => {
|
|
|
|
let message = if list_state.filter() == Filter::Done {
|
|
|
|
list_state.set_filter(Filter::None);
|
|
|
|
"Disabled filter DONE"
|
|
|
|
} else {
|
|
|
|
list_state.set_filter(Filter::Done);
|
|
|
|
"Enabled filter DONE │ Press d again to disable the filter"
|
|
|
|
};
|
|
|
|
|
|
|
|
list_state.message.push_str(message);
|
|
|
|
}
|
|
|
|
KeyCode::Char('p') => {
|
|
|
|
let message = if list_state.filter() == Filter::Pending {
|
|
|
|
list_state.set_filter(Filter::None);
|
|
|
|
"Disabled filter PENDING"
|
|
|
|
} else {
|
|
|
|
list_state.set_filter(Filter::Pending);
|
|
|
|
"Enabled filter PENDING │ Press p again to disable the filter"
|
|
|
|
};
|
|
|
|
|
|
|
|
list_state.message.push_str(message);
|
|
|
|
}
|
|
|
|
KeyCode::Char('r') => {
|
|
|
|
list_state.reset_selected()?;
|
|
|
|
}
|
|
|
|
KeyCode::Char('c') => {
|
2024-08-23 18:23:45 -04:00
|
|
|
if list_state.selected_to_current_exercise()? {
|
|
|
|
return Ok(());
|
|
|
|
}
|
2024-08-23 18:14:12 -04:00
|
|
|
}
|
|
|
|
// Redraw to remove the message.
|
|
|
|
KeyCode::Esc => (),
|
|
|
|
_ => continue,
|
|
|
|
}
|
|
|
|
|
|
|
|
list_state.redraw(stdout)?;
|
2024-04-06 21:38:18 -04:00
|
|
|
}
|
2024-08-23 18:14:12 -04:00
|
|
|
Event::Mouse(event) => {
|
|
|
|
match event.kind {
|
|
|
|
MouseEventKind::ScrollDown => list_state.select_next(),
|
|
|
|
MouseEventKind::ScrollUp => list_state.select_previous(),
|
|
|
|
_ => continue,
|
|
|
|
}
|
|
|
|
|
|
|
|
list_state.redraw(stdout)?;
|
2024-04-07 20:41:48 -04:00
|
|
|
}
|
2024-08-23 18:14:12 -04:00
|
|
|
// Redraw
|
|
|
|
Event::Resize(_, _) => list_state.redraw(stdout)?,
|
|
|
|
// Ignore
|
|
|
|
Event::FocusGained | Event::FocusLost => (),
|
2024-04-06 21:03:37 -04:00
|
|
|
}
|
|
|
|
}
|
2024-08-17 10:49:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn list(app_state: &mut AppState) -> Result<()> {
|
|
|
|
let mut stdout = io::stdout().lock();
|
|
|
|
stdout
|
|
|
|
.queue(EnterAlternateScreen)?
|
2024-08-23 18:14:12 -04:00
|
|
|
.queue(cursor::Hide)?
|
|
|
|
.queue(DisableLineWrap)?
|
|
|
|
.queue(EnableMouseCapture)?;
|
2024-08-17 10:49:07 -04:00
|
|
|
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)?
|
2024-08-23 18:14:12 -04:00
|
|
|
.queue(cursor::Show)?
|
|
|
|
.queue(EnableLineWrap)?
|
2024-08-17 10:34:43 -04:00
|
|
|
.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
|
|
|
}
|