<commit_before>module Events.ChannelSelect where

import Prelude ()
import Prelude.Compat

import Data.Monoid ((<>))
import qualified Data.Text as T
import qualified Data.HashMap.Strict as HM
import qualified Graphics.Vty as Vty
import Lens.Micro.Platform

import Types
import State

onEventChannelSelect :: Vty.Event -> MH ()
onEventChannelSelect e | Just kb <- lookupKeybinding e channelSelectKeybindings =
    kbAction kb
onEventChannelSelect (Vty.EvKey Vty.KBS []) = do
    csChannelSelectString %= (\s -> if T.null s then s else T.init s)
    updateChannelSelectMatches
onEventChannelSelect (Vty.EvKey (Vty.KChar c) []) | c /= '\t' = do
    csChannelSelectString %= (flip T.snoc c)
    updateChannelSelectMatches
onEventChannelSelect _ = return ()

channelSelectKeybindings :: [Keybinding]
channelSelectKeybindings =
    [ KB "Select matching channel"
         (Vty.EvKey Vty.KEnter []) $ do
             -- If there is only one channel selection match, switch to
             -- it
             st <- use id
             let allMatches = (HM.elems $ st^.csChannelSelectChannelMatches) <>
                              (HM.elems $ st^.csChannelSelectUserMatches)
             case allMatches of
                 [single] -> do
                     csMode .= Main
                     changeChannel (channelNameFromMatch single)
                 _ -> return ()

    , KB "Cancel channel selection"
         (Vty.EvKey Vty.KEsc []) $ do
           csMode .= Main
    ]<commit_msg>Channel selection: C-c also cancels<commit_after>module Events.ChannelSelect where

import Prelude ()
import Prelude.Compat

import Data.Monoid ((<>))
import qualified Data.Text as T
import qualified Data.HashMap.Strict as HM
import qualified Graphics.Vty as Vty
import Lens.Micro.Platform

import Types
import State

onEventChannelSelect :: Vty.Event -> MH ()
onEventChannelSelect e | Just kb <- lookupKeybinding e channelSelectKeybindings =
    kbAction kb
onEventChannelSelect (Vty.EvKey Vty.KBS []) = do
    csChannelSelectString %= (\s -> if T.null s then s else T.init s)
    updateChannelSelectMatches
onEventChannelSelect (Vty.EvKey (Vty.KChar c) []) | c /= '\t' = do
    csChannelSelectString %= (flip T.snoc c)
    updateChannelSelectMatches
onEventChannelSelect _ = return ()

channelSelectKeybindings :: [Keybinding]
channelSelectKeybindings =
    [ KB "Select matching channel"
         (Vty.EvKey Vty.KEnter []) $ do
             -- If there is only one channel selection match, switch to
             -- it
             st <- use id
             let allMatches = (HM.elems $ st^.csChannelSelectChannelMatches) <>
                              (HM.elems $ st^.csChannelSelectUserMatches)
             case allMatches of
                 [single] -> do
                     csMode .= Main
                     changeChannel (channelNameFromMatch single)
                 _ -> return ()

    , KB "Cancel channel selection"
         (Vty.EvKey Vty.KEsc []) $ do
           csMode .= Main

    , KB "Cancel channel selection"
         (Vty.EvKey (Vty.KChar 'c') [Vty.MCtrl]) $ do
           csMode .= Main
    ]
{'fraction_non_alphanumeric': 0.09114331723027375, 'fraction_numerical': 0.0, 'mean_word_length': 2.3433799784714746, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1753401', 'n_tokens_mistral': 925, 'n_tokens_neox': 882, 'n_words': 257}