From 3c72d1a2c80b3694487bb35307b838d912ab081b Mon Sep 17 00:00:00 2001 From: T Date: Tue, 11 Oct 2022 23:33:36 +0200 Subject: [PATCH 1/3] Handle Ctrl-D in REPL and trigger toggling Quit Modal --- src/Swarm/TUI/Controller.hs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Swarm/TUI/Controller.hs b/src/Swarm/TUI/Controller.hs index 46d8c0494..1a8c92fd4 100644 --- a/src/Swarm/TUI/Controller.hs +++ b/src/Swarm/TUI/Controller.hs @@ -747,6 +747,15 @@ handleREPLEvent = \case CmdPrompt {} -> continueWithoutRedraw SearchPrompt _ _ -> uiState %= resetWithREPLForm (mkReplForm $ mkCmdPrompt "") + ControlKey 'd' -> do + formSt <- use $ uiState . uiReplForm . to formState + case formSt of + CmdPrompt t _ -> if t == T.empty + then toggleModal QuitModal + else continueWithoutRedraw + SearchPrompt t _ -> if t == T.empty + then toggleModal QuitModal + else continueWithoutRedraw ev -> do replForm <- use $ uiState . uiReplForm f' <- nestEventM' replForm (handleFormEvent ev) From b2153efe434cc50879c97c14ab27c8e2ba7ea45a Mon Sep 17 00:00:00 2001 From: T Date: Wed, 12 Oct 2022 18:28:51 +0200 Subject: [PATCH 2/3] Refactor checking if the prompt text is empty --- src/Swarm/TUI/Controller.hs | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/src/Swarm/TUI/Controller.hs b/src/Swarm/TUI/Controller.hs index 1a8c92fd4..a9c8e49ec 100644 --- a/src/Swarm/TUI/Controller.hs +++ b/src/Swarm/TUI/Controller.hs @@ -236,11 +236,11 @@ handleMainEvent ev = do Key V.KEsc | isJust (s ^. uiState . uiError) -> uiState . uiError .= Nothing | Just m <- s ^. uiState . uiModal -> do - safeAutoUnpause - uiState . uiModal .= Nothing - -- message modal is not autopaused, so update notifications when leaving it - when (m ^. modalType == MessagesModal) $ do - gameState . lastSeenMessageTime .= s ^. gameState . ticks + safeAutoUnpause + uiState . uiModal .= Nothing + -- message modal is not autopaused, so update notifications when leaving it + when (m ^. modalType == MessagesModal) $ do + gameState . lastSeenMessageTime .= s ^. gameState . ticks FKey 1 -> toggleModal HelpModal FKey 2 -> toggleModal RobotsModal FKey 3 | not (null (s ^. gameState . availableRecipes . notificationsContent)) -> do @@ -720,8 +720,8 @@ handleREPLEvent = \case Just found | T.null t -> uiState %= resetWithREPLForm (mkReplForm $ mkCmdPrompt "") | otherwise -> do - uiState %= resetWithREPLForm (mkReplForm $ mkCmdPrompt found) - modify validateREPLForm + uiState %= resetWithREPLForm (mkReplForm $ mkCmdPrompt found) + modify validateREPLForm else continueWithoutRedraw Key V.KUp -> modify $ adjReplHistIndex Older Key V.KDown -> modify $ adjReplHistIndex Newer @@ -748,14 +748,10 @@ handleREPLEvent = \case SearchPrompt _ _ -> uiState %= resetWithREPLForm (mkReplForm $ mkCmdPrompt "") ControlKey 'd' -> do - formSt <- use $ uiState . uiReplForm . to formState - case formSt of - CmdPrompt t _ -> if t == T.empty - then toggleModal QuitModal - else continueWithoutRedraw - SearchPrompt t _ -> if t == T.empty - then toggleModal QuitModal - else continueWithoutRedraw + text <- use $ uiState . uiReplForm . to formState . promptTextL + if text == T.empty + then toggleModal QuitModal + else continueWithoutRedraw ev -> do replForm <- use $ uiState . uiReplForm f' <- nestEventM' replForm (handleFormEvent ev) @@ -776,9 +772,9 @@ tabComplete s (CmdPrompt t mms) | (m : ms) <- mms = CmdPrompt (replaceLast m t) (ms ++ [m]) | T.null lastWord = CmdPrompt t [] | otherwise = case matches of - [] -> CmdPrompt t [] - [m] -> CmdPrompt (completeWith m) [] - (m : ms) -> CmdPrompt (completeWith m) (ms ++ [m]) + [] -> CmdPrompt t [] + [m] -> CmdPrompt (completeWith m) [] + (m : ms) -> CmdPrompt (completeWith m) (ms ++ [m]) where completeWith m = T.append t (T.drop (T.length lastWord) m) lastWord = T.takeWhileEnd isIdentChar t @@ -799,7 +795,8 @@ validateREPLForm s = theType = case result of Right (Just (ProcessedTerm _ (Module ty _) _ _)) -> Just ty _ -> Nothing - in s & uiState . uiReplForm %~ validate result + in s + & uiState . uiReplForm %~ validate result & uiState . uiReplType .~ theType SearchPrompt _ _ -> s where From b52e9202e28a995204325751501af825b6973c97 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Wed, 12 Oct 2022 16:45:53 +0000 Subject: [PATCH 3/3] Restyled by fourmolu --- src/Swarm/TUI/Controller.hs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Swarm/TUI/Controller.hs b/src/Swarm/TUI/Controller.hs index a9c8e49ec..14b5a2721 100644 --- a/src/Swarm/TUI/Controller.hs +++ b/src/Swarm/TUI/Controller.hs @@ -236,11 +236,11 @@ handleMainEvent ev = do Key V.KEsc | isJust (s ^. uiState . uiError) -> uiState . uiError .= Nothing | Just m <- s ^. uiState . uiModal -> do - safeAutoUnpause - uiState . uiModal .= Nothing - -- message modal is not autopaused, so update notifications when leaving it - when (m ^. modalType == MessagesModal) $ do - gameState . lastSeenMessageTime .= s ^. gameState . ticks + safeAutoUnpause + uiState . uiModal .= Nothing + -- message modal is not autopaused, so update notifications when leaving it + when (m ^. modalType == MessagesModal) $ do + gameState . lastSeenMessageTime .= s ^. gameState . ticks FKey 1 -> toggleModal HelpModal FKey 2 -> toggleModal RobotsModal FKey 3 | not (null (s ^. gameState . availableRecipes . notificationsContent)) -> do @@ -720,8 +720,8 @@ handleREPLEvent = \case Just found | T.null t -> uiState %= resetWithREPLForm (mkReplForm $ mkCmdPrompt "") | otherwise -> do - uiState %= resetWithREPLForm (mkReplForm $ mkCmdPrompt found) - modify validateREPLForm + uiState %= resetWithREPLForm (mkReplForm $ mkCmdPrompt found) + modify validateREPLForm else continueWithoutRedraw Key V.KUp -> modify $ adjReplHistIndex Older Key V.KDown -> modify $ adjReplHistIndex Newer @@ -772,9 +772,9 @@ tabComplete s (CmdPrompt t mms) | (m : ms) <- mms = CmdPrompt (replaceLast m t) (ms ++ [m]) | T.null lastWord = CmdPrompt t [] | otherwise = case matches of - [] -> CmdPrompt t [] - [m] -> CmdPrompt (completeWith m) [] - (m : ms) -> CmdPrompt (completeWith m) (ms ++ [m]) + [] -> CmdPrompt t [] + [m] -> CmdPrompt (completeWith m) [] + (m : ms) -> CmdPrompt (completeWith m) (ms ++ [m]) where completeWith m = T.append t (T.drop (T.length lastWord) m) lastWord = T.takeWhileEnd isIdentChar t