From ffa37b16841a550f5c8fb1fa4be8bbbf9c1270f7 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Mon, 19 Dec 2022 13:05:54 +0000 Subject: [PATCH] terminal: use Ctrl-L to start/continue live message, as Alt-Enter is not always supported (#1607) * terminal: use Ctrl-L to start/continue live message, as Alt-Enter is not always supported * refactor --- src/Simplex/Chat/Terminal/Input.hs | 33 ++++++++++++++++++------------ 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/Simplex/Chat/Terminal/Input.hs b/src/Simplex/Chat/Terminal/Input.hs index f4470a73d1..e5b5317620 100644 --- a/src/Simplex/Chat/Terminal/Input.hs +++ b/src/Simplex/Chat/Terminal/Input.hs @@ -123,15 +123,22 @@ receiveFromTTY cc@ChatController {inputQ, activeTo} ct@ChatTerminal {termSize, t forever $ getKey >>= liftIO . processKey >> withTermLock ct (updateInput ct) where processKey :: (Key, Modifiers) -> IO () - processKey = \case - (EnterKey, ms) -> - when (ms == mempty || ms == altKey) $ - atomically (readTVar termState >>= submitInput ms) - >>= mapM_ (uncurry endLiveMessage) - key -> atomically $ do - ac <- readTVar activeTo - live <- isJust <$> readTVar liveMessageState - modifyTVar termState $ updateTermState ac live (width termSize) key + processKey key = case key of + (EnterKey, ms) + | ms == mempty -> submit False + | ms == altKey -> submit True + | otherwise -> pure () + (CharKey c, ms) + | (c == 'l' || c == 'L') && ms == ctrlKey -> submit True + | otherwise -> update key + _ -> update key + submit live = + atomically (readTVar termState >>= submitInput live) + >>= mapM_ (uncurry endLiveMessage) + update key = atomically $ do + ac <- readTVar activeTo + live <- isJust <$> readTVar liveMessageState + modifyTVar termState $ updateTermState ac live (width termSize) key endLiveMessage :: String -> LiveMessage -> IO () endLiveMessage sentMsg lm = do @@ -143,13 +150,13 @@ receiveFromTTY cc@ChatController {inputQ, activeTo} ct@ChatTerminal {termSize, t where kill sel = deRefWeak (sel lm) >>= mapM_ killThread - submitInput :: Modifiers -> TerminalState -> STM (Maybe (String, LiveMessage)) - submitInput ms ts = do + submitInput :: Bool -> TerminalState -> STM (Maybe (String, LiveMessage)) + submitInput live ts = do let s = inputString ts lm_ <- readTVar liveMessageState case lm_ of Just LiveMessage {chatName} - | ms == altKey -> do + | live -> do writeTVar termState ts' {previousInput} writeTBQueue inputQ $ "/live " <> chatNameStr chatName | otherwise -> @@ -157,7 +164,7 @@ receiveFromTTY cc@ChatController {inputQ, activeTo} ct@ChatTerminal {termSize, t where previousInput = chatNameStr chatName <> " " <> s _ - | ms == altKey -> when (isSend s) $ do + | live -> when (isSend s) $ do writeTVar termState ts' {previousInput = s} writeTBQueue inputQ $ "/live " <> s | otherwise -> do