diff options
Diffstat (limited to 'src/gui/question.rs')
-rw-r--r-- | src/gui/question.rs | 69 |
1 files changed, 34 insertions, 35 deletions
diff --git a/src/gui/question.rs b/src/gui/question.rs index 2aa6e65..73e4a93 100644 --- a/src/gui/question.rs +++ b/src/gui/question.rs @@ -1,11 +1,10 @@ use crate::{ gui::util, model::{difficulty, difficulty::Difficulty, Card}, - util::event::{Event, Events}, util::serialization, }; use anyhow::Result; -use termion::event::Key; +use crossterm::event::{self, Event, KeyCode, KeyModifiers}; use tui::{ backend::Backend, layout::{Alignment, Constraint, Direction, Layout}, @@ -32,7 +31,6 @@ pub enum Response { pub fn ask<B: Backend>( terminal: &mut Terminal<B>, - events: &Events, title: &str, card: &Card, ) -> Result<Response> { @@ -136,10 +134,10 @@ pub fn ask<B: Backend>( } })?; - if let Event::Input(key) = events.next()? { + if let Event::Key(key) = event::read()? { match state.answer { - Answer::Write => match key { - Key::Char('\n') => { + Answer::Write => match key.code { + KeyCode::Char('\n') => { let difficulty = if is_correct(&state.input, &card.responses) { Difficulty::Good } else { @@ -147,57 +145,58 @@ pub fn ask<B: Backend>( }; state.answer = Answer::Difficulty { difficulty } } - Key::Char(c) => { - state.input.push(c); - if is_correct(&state.input, &card.responses) { - state.answer = Answer::Difficulty { - difficulty: Difficulty::Good, + KeyCode::Char(c) => { + if key.modifiers.contains(KeyModifiers::CONTROL) { + if c == 'u' { + state.input.clear(); + } else if c == 'w' { + let mut words = state.input.split_whitespace().collect::<Vec<&str>>(); + if !words.is_empty() { + words.truncate(words.len() - 1); + let joined_words = words.join(" "); + let space = if !words.is_empty() { " " } else { "" }; + state.input = format!("{joined_words}{space}"); + } + } else if c == 'c' { + return Ok(Response::Aborted); + } + } else { + state.input.push(c); + if is_correct(&state.input, &card.responses) { + state.answer = Answer::Difficulty { + difficulty: Difficulty::Good, + } } } } - Key::Backspace => { + KeyCode::Backspace => { state.input.pop(); } - Key::Ctrl('u') => { - state.input.clear(); - } - Key::Ctrl('w') => { - let mut words = state.input.split_whitespace().collect::<Vec<&str>>(); - if !words.is_empty() { - words.truncate(words.len() - 1); - state.input = format!( - "{}{}", - words.join(" "), - if !words.is_empty() { " " } else { "" } - ); - } - } - Key::Ctrl('c') => { - return Ok(Response::Aborted); - } _ => {} }, Answer::Difficulty { difficulty: selected, - } => match key { - Key::Left => { + } => match key.code { + KeyCode::Left => { for d in relative_element(&card.state.difficulties(), &selected, -1).iter() { state.answer = Answer::Difficulty { difficulty: *d } } } - Key::Right => { + KeyCode::Right => { for d in relative_element(&card.state.difficulties(), &selected, 1).iter() { state.answer = Answer::Difficulty { difficulty: *d } } } - Key::Char('\n') => { + KeyCode::Enter => { return Ok(Response::Answered { difficulty: selected, }) } - Key::Ctrl('c') => { - return Ok(Response::Aborted); + KeyCode::Char('c') => { + if key.modifiers.contains(KeyModifiers::CONTROL) { + return Ok(Response::Aborted); + } } _ => {} }, |