rustlings/src/list.rs

109 lines
3.9 KiB
Rust
Raw Normal View History

2024-08-17 15:34:43 +01:00
use anyhow::{Context, Result};
2024-08-23 23:14:12 +01:00
use crossterm::{
cursor,
event::{
self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind, MouseEventKind,
},
2024-08-23 23:14:12 +01:00
terminal::{
disable_raw_mode, enable_raw_mode, DisableLineWrap, EnableLineWrap, EnterAlternateScreen,
LeaveAlternateScreen,
},
QueueableCommand,
2024-04-07 02:03:37 +01:00
};
use std::io::{self, StdoutLock, Write};
2024-04-07 02:03:37 +01:00
use crate::app_state::AppState;
2024-04-07 15:33:00 +01:00
2024-08-23 23:14:12 +01:00
use self::state::{Filter, ListState};
2024-04-07 02:03:37 +01:00
2024-04-18 10:31:08 +01:00
mod state;
fn handle_list(app_state: &mut AppState, stdout: &mut StdoutLock) -> Result<()> {
2024-08-23 23:14:12 +01: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 16:17:56 +01:00
if list_state.filter() == Filter::Done {
2024-08-23 23:14:12 +01:00
list_state.set_filter(Filter::None);
2024-08-24 16:17:56 +01:00
list_state.message.push_str("Disabled filter DONE");
2024-08-23 23:14:12 +01:00
} else {
list_state.set_filter(Filter::Done);
2024-08-24 16:17:56 +01:00
list_state.message.push_str(
"Enabled filter DONE │ Press d again to disable the filter",
);
}
2024-08-23 23:14:12 +01: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);
}
2024-08-25 18:24:12 +01:00
KeyCode::Char('r') => list_state.reset_selected()?,
2024-08-23 23:14:12 +01:00
KeyCode::Char('c') => {
if list_state.selected_to_current_exercise()? {
return Ok(());
}
2024-08-23 23:14:12 +01:00
}
// Redraw to remove the message.
KeyCode::Esc => (),
_ => continue,
}
2024-04-07 02:38:18 +01:00
}
2024-08-24 16:17:56 +01:00
Event::Mouse(event) => match event.kind {
MouseEventKind::ScrollDown => list_state.select_next(),
MouseEventKind::ScrollUp => list_state.select_previous(),
_ => continue,
},
2024-08-25 18:24:12 +01:00
Event::Resize(width, height) => list_state.set_term_size(width, height),
2024-08-23 23:14:12 +01:00
// Ignore
2024-08-24 16:17:56 +01:00
Event::FocusGained | Event::FocusLost => continue,
2024-04-07 02:03:37 +01:00
}
2024-08-24 16:17:56 +01:00
2024-08-24 18:18:13 +01:00
list_state.draw(stdout)?;
2024-04-07 02:03:37 +01:00
}
}
pub fn list(app_state: &mut AppState) -> Result<()> {
let mut stdout = io::stdout().lock();
stdout
.queue(EnterAlternateScreen)?
2024-08-23 23:14:12 +01: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 15:34:43 +01:00
stdout
.queue(LeaveAlternateScreen)?
2024-08-23 23:14:12 +01:00
.queue(cursor::Show)?
.queue(EnableLineWrap)?
2024-08-17 15:34:43 +01:00
.queue(DisableMouseCapture)?
.flush()?;
2024-04-07 02:03:37 +01:00
disable_raw_mode()?;
res
2024-04-07 02:03:37 +01:00
}