rustlings/src/list.rs

98 lines
3.3 KiB
Rust
Raw Normal View History

2024-04-07 04:03:37 +03:00
use anyhow::Result;
use crossterm::{
event::{self, Event, KeyCode, KeyEventKind},
2024-04-07 04:03:37 +03:00
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
ExecutableCommand,
};
2024-04-07 20:05:29 +03:00
use ratatui::{backend::CrosstermBackend, Terminal};
2024-04-08 02:33:11 +03:00
use std::{fmt::Write, io};
2024-04-07 04:03:37 +03:00
2024-04-07 20:05:29 +03:00
mod state;
2024-04-07 17:33:00 +03:00
use crate::app_state::AppState;
2024-04-07 17:33:00 +03:00
2024-04-08 03:41:48 +03:00
use self::state::{Filter, UiState};
2024-04-07 04:03:37 +03:00
pub fn list(app_state: &mut AppState) -> Result<()> {
2024-04-07 04:38:18 +03:00
let mut stdout = io::stdout().lock();
stdout.execute(EnterAlternateScreen)?;
enable_raw_mode()?;
let mut terminal = Terminal::new(CrosstermBackend::new(&mut stdout))?;
terminal.clear()?;
let mut ui_state = UiState::new(app_state);
2024-04-07 04:38:18 +03:00
'outer: loop {
2024-04-09 20:37:39 +03:00
terminal.draw(|frame| ui_state.draw(frame).unwrap())?;
2024-04-07 04:03:37 +03:00
2024-04-07 04:38:18 +03:00
let key = loop {
match event::read()? {
Event::Key(key) => match key.kind {
KeyEventKind::Press | KeyEventKind::Repeat => break key,
KeyEventKind::Release => (),
},
2024-04-07 04:38:18 +03:00
// Redraw
Event::Resize(_, _) => continue 'outer,
// Ignore
Event::FocusGained | Event::FocusLost | Event::Mouse(_) | Event::Paste(_) => (),
}
};
2024-04-08 02:33:11 +03:00
ui_state.message.clear();
2024-04-07 04:38:18 +03:00
match key.code {
KeyCode::Char('q') => break,
2024-04-07 17:33:00 +03: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-08 03:41:48 +03: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"
};
ui_state = ui_state.with_updated_rows();
2024-04-08 03:41:48 +03: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"
};
ui_state = ui_state.with_updated_rows();
2024-04-08 03:41:48 +03:00
ui_state.message.push_str(message);
}
2024-04-07 23:43:59 +03:00
KeyCode::Char('r') => {
2024-04-11 15:35:30 +03:00
let Some(exercise) = ui_state.reset_selected()? else {
continue;
};
2024-04-08 02:33:11 +03:00
ui_state = ui_state.with_updated_rows();
2024-04-08 02:33:11 +03:00
ui_state
.message
.write_fmt(format_args!("The exercise {exercise} has been reset!"))?;
2024-04-07 23:43:59 +03:00
}
2024-04-07 05:59:22 +03:00
KeyCode::Char('c') => {
ui_state.selected_to_current_exercise()?;
ui_state = ui_state.with_updated_rows();
2024-04-07 05:59:22 +03:00
}
2024-04-07 04:38:18 +03:00
_ => (),
2024-04-07 04:03:37 +03:00
}
}
drop(terminal);
stdout.execute(LeaveAlternateScreen)?;
disable_raw_mode()?;
Ok(())
}