From 3c935cc0728f25dab169d2a05dfa153242946c4a Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 9 Oct 2022 17:38:26 +0200 Subject: Abort with C-c --- src/gui/mod.rs | 18 +++++++++++------- src/gui/question.rs | 12 ++++++++++-- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/gui/mod.rs b/src/gui/mod.rs index 92cd943..3599df8 100644 --- a/src/gui/mod.rs +++ b/src/gui/mod.rs @@ -51,13 +51,17 @@ pub fn start(conn: &Connection, term: &mut Term, events: &Events, deck_name: &st match db::pick_random_ready(conn) { Some(card) => { - let difficulty = question::ask(term, events, &title, &card)?; - answers += 1; - db::update( - conn, - &card.question, - &space_repetition::update(card.state, difficulty), - )?; + match question::ask(term, events, &title, &card)? { + question::Response::Aborted => break, + question::Response::Answered { difficulty } => { + answers += 1; + db::update( + conn, + &card.question, + &space_repetition::update(card.state, difficulty), + )?; + } + } } None => { let message = match db::next_ready(conn) { diff --git a/src/gui/question.rs b/src/gui/question.rs index 27759f8..8f9ee19 100644 --- a/src/gui/question.rs +++ b/src/gui/question.rs @@ -25,12 +25,17 @@ enum Answer { Difficulty { difficulty: Difficulty }, } +pub enum Response { + Aborted, + Answered { difficulty: Difficulty }, +} + pub fn ask( terminal: &mut Terminal, events: &Events, title: &str, card: &Card, -) -> Result { +) -> Result { let mut state = State { input: String::new(), answer: Answer::Write, @@ -155,6 +160,9 @@ pub fn ask( state.input = format!("{}{}", words.join(" "), if words.len() > 0 {" " } else {""}); } } + Key::Ctrl('c') => { + return Ok(Response::Aborted); + } _ => {} }, Answer::Difficulty { @@ -171,7 +179,7 @@ pub fn ask( state.answer = Answer::Difficulty { difficulty: *d } } } - Key::Char('\n') => return Ok(selected), + Key::Char('\n') => return Ok(Response::Answered { difficulty: selected }), _ => {} }, } -- cgit v1.2.3