rustlings/src/list.rs

113 lines
4 KiB
Rust
Raw Normal View History

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-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
};
use std::io::{self, StdoutLock, Write};
2024-04-06 21:03:37 -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;
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') => {
2024-08-24 11:17:56 -04:00
if list_state.filter() == Filter::Done {
2024-08-23 18:14:12 -04:00
list_state.set_filter(Filter::None);
2024-08-24 11:17:56 -04:00
list_state.message.push_str("Disabled filter DONE");
2024-08-23 18:14:12 -04:00
} else {
list_state.set_filter(Filter::Done);
2024-08-24 11:17:56 -04:00
list_state.message.push_str(
"Enabled filter DONE │ Press d again to disable the filter",
);
}
2024-08-23 18:14:12 -04:00
}
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') => {
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,
}
2024-04-06 21:38:18 -04:00
}
2024-08-24 11:17:56 -04:00
Event::Mouse(event) => match event.kind {
MouseEventKind::ScrollDown => list_state.select_next(),
MouseEventKind::ScrollUp => list_state.select_previous(),
_ => continue,
},
Event::Resize(width, height) => {
list_state.set_term_size(width, height);
2024-04-07 20:41:48 -04:00
}
2024-08-23 18:14:12 -04:00
// Ignore
2024-08-24 11:17:56 -04:00
Event::FocusGained | Event::FocusLost => continue,
2024-04-06 21:03:37 -04:00
}
2024-08-24 11:17:56 -04:00
2024-08-24 13:18:13 -04:00
list_state.draw(stdout)?;
2024-04-06 21:03:37 -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)?;
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()?;
res
2024-04-06 21:03:37 -04:00
}