From eec0551772a501c49199b56e96f8b6e41883b154 Mon Sep 17 00:00:00 2001 From: Yvee1 Date: Sun, 27 Oct 2024 19:51:13 +0100 Subject: [PATCH] Show open question answer after double enter The Shift+Tab keybinding was not reliable and there are few good keybindings available. Therefore, we switch to a different approach: show the correct answer(s) when submitting empty answer(s) (which is not correct) or submitting the same answer(s) twice in a row. --- src/States.hs | 4 +++- src/UI/Cards.hs | 40 ++++++++++++++++++++++------------------ src/UI/Info.hs | 2 -- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/States.hs b/src/States.hs index d2b976c..430ac1d 100644 --- a/src/States.hs +++ b/src/States.hs @@ -103,6 +103,7 @@ data CardState = , _entered :: Bool , _correctGaps :: Map Int Bool , _failed :: Bool + , _previousInput :: Map Int String } | ReorderState { _highlighted :: Int @@ -124,7 +125,8 @@ defaultCardState OpenQuestion{perforated=perf} = OpenQuestionState , _number = nGapsInPerforated perf , _entered = False , _correctGaps = M.fromList [(i, False) | i <- [0..nGapsInPerforated perf - 1]] - , _failed = False } + , _failed = False + , _previousInput = M.empty } defaultCardState MultipleAnswer{options=opts} = MultipleAnswerState { _highlighted = 0 , _selected = M.fromList [(i, False) | i <- [0..NE.length opts-1]] diff --git a/src/UI/Cards.hs b/src/UI/Cards.hs index b625231..42d527b 100644 --- a/src/UI/Cards.hs +++ b/src/UI/Cards.hs @@ -45,7 +45,7 @@ drawInfo s = if not (s ^. showControls) then emptyWidget else DefinitionState {} -> ", Enter: flip card / continue" MultipleChoiceState {} -> ", Enter: submit answer / continue" MultipleAnswerState {} -> ", Enter: select / continue, c: submit selection" - OpenQuestionState {} -> ", Left/Right/Tab: navigate gaps, Enter: submit answer / continue, Shift+Tab: show answer" + OpenQuestionState {} -> ", Left/Right/Tab: navigate gaps, Enter: submit answer / continue" ReorderState {} -> ", Enter: grab, c: submit answer" drawCardBox :: Widget Name -> Widget Name @@ -358,15 +358,11 @@ handleEvent (VtyEvent e) = correctlyAnswered = NE.toList (NE.map isOptionCorrect opts) == map snd (M.toAscList kvs) - (OpenQuestionState {_highlighted = i, _number = n, _gapInput = kvs, _correctGaps = cGaps, _failed=fail}, OpenQuestion _ _ perforated) -> + (OpenQuestionState {_highlighted = i, _number = n, _gapInput = kvs, _correctGaps = cGaps, _failed=fail, _previousInput = pkvs}, OpenQuestion _ _ perforated) -> + let frozen = M.foldr (&&) True cGaps + down = scroll s 1 + up = scroll s (-1) in case ev of - V.EvKey (V.KChar '\t') [V.MShift] -> zoom (cs.cardState) $ do - gapInput .= correctAnswers - entered .= True - failed .= True - correctGaps .= M.fromAscList [(i, True) | i <- [0..n-1]] - where correctAnswers = M.fromAscList $ zip [0..] $ map NE.head (sentenceToGaps (perforatedToSentence perforated)) - V.EvKey (V.KChar '\t') [] -> zoom (cs.cardState) $ do if i < n - 1 && not frozen then highlighted += 1 @@ -397,7 +393,7 @@ handleEvent (VtyEvent e) = _ -> return () V.EvKey V.KEnter [] -> case (frozen, fail) of - (False, _) -> zoom cs $ do + (False, _) -> zoom (cs.cardState) $ do let sentence = perforatedToSentence perforated gaps = sentenceToGaps sentence @@ -406,11 +402,23 @@ handleEvent (VtyEvent e) = then elem else (\word possibilites -> map toLower word `elem` NE.map (map toLower) possibilites) - cardState.correctGaps %= M.mapWithKey (\j _ -> M.findWithDefault "" j kvs `wordIsCorrect` (gaps !! j)) - cardState.entered .= True + allEmpty = all null kvs + sameAsPrevious = kvs == pkvs - unlessM (M.foldr (&&) True <$> use (cardState.correctGaps)) $ - cardState.failed .= True + correctGaps %= M.mapWithKey (\j _ -> M.findWithDefault "" j kvs `wordIsCorrect` (gaps !! j)) + entered .= True + + allCorrect <- M.foldr (&&) True <$> use correctGaps + + when (allEmpty && not allCorrect || sameAsPrevious) $ do + let correctAnswers = M.fromAscList $ zip [0..] $ map NE.head (sentenceToGaps (perforatedToSentence perforated)) + correctGaps .= M.fromAscList [(i, True) | i <- [0..n-1]] + gapInput .= correctAnswers + + unless allCorrect $ + failed .= True + + previousInput .= kvs (_, True) -> next (_, False) -> do @@ -424,10 +432,6 @@ handleEvent (VtyEvent e) = _ -> return () - where frozen = M.foldr (&&) True cGaps - down = scroll s 1 - up = scroll s (-1) - (ReorderState {_highlighted = i, _entered = submitted, _grabbed=dragging, _number = n, _order = kvs }, Reorder _ _ elts) -> case ev of V.EvKey V.KUp [] -> up diff --git a/src/UI/Info.hs b/src/UI/Info.hs index c9cee68..0cd4977 100644 --- a/src/UI/Info.hs +++ b/src/UI/Info.hs @@ -67,6 +67,4 @@ info = unlines , "" , " * Use the c key for confirming reorder questions or multiple choice questions with more than 1 possible answer" , "" - , " * Use Shift+Tab to show the answers of a open question" - , "" , " * Use Ctrl+Left and Ctrl+Right to move to previous and next cards without having to answer them; this is disabled in review mode"]