aboutsummaryrefslogtreecommitdiff
path: root/src/Chelleport/KeySequence.hs
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2024-12-15 15:39:02 +0530
committerAkshay Nair <phenax5@gmail.com>2024-12-15 15:45:37 +0530
commit1d07e554284593cdca804404d1d9f68a473ee986 (patch)
tree6f90288426699384cdb85cfdca4a036c3da1e51c /src/Chelleport/KeySequence.hs
parent0c6b8c83e8673b394914e1f824dfb887b762b0ee (diff)
downloadchelleport-1d07e554284593cdca804404d1d9f68a473ee986.tar.gz
chelleport-1d07e554284593cdca804404d1d9f68a473ee986.zip
Refactor a bunch of stuff
Diffstat (limited to 'src/Chelleport/KeySequence.hs')
-rw-r--r--src/Chelleport/KeySequence.hs29
1 files changed, 12 insertions, 17 deletions
diff --git a/src/Chelleport/KeySequence.hs b/src/Chelleport/KeySequence.hs
index 52e3a54..f112b57 100644
--- a/src/Chelleport/KeySequence.hs
+++ b/src/Chelleport/KeySequence.hs
@@ -1,34 +1,29 @@
module Chelleport.KeySequence where
import Chelleport.Types (KeyGrid, KeySequence)
-import Data.List (isPrefixOf, nub)
+import Chelleport.Utils (findWithIndex, uniq)
+import Control.Monad (guard)
+import Data.List (isPrefixOf)
import qualified Data.Map as Map
import qualified SDL
nextChars :: KeySequence -> KeyGrid -> Maybe [Char]
-nextChars keys cells =
+nextChars keySequence cells =
case matches of
[] -> Nothing
- _ -> Just $ nub result
+ _ -> Just nextCharactersInSequence
where
- matches = concatMap (filter (isPrefixOf keys)) cells
- result = concatMap (take 1 . drop (length keys)) matches
-
-findWithIndex :: (x -> Maybe r) -> Int -> [x] -> Maybe (Int, r)
-findWithIndex _pred _index [] = Nothing
-findWithIndex predicate index (x : ls) =
- case predicate x of
- Just item -> Just (index, item)
- Nothing -> findWithIndex predicate (index + 1) ls
+ matches = concatMap (filter $ isPrefixOf keySequence) cells
+ nextCharactersInSequence = uniq $ concatMap (take 1 . drop (length keySequence)) matches
findMatchPosition :: KeySequence -> KeyGrid -> Maybe (Int, Int)
-findMatchPosition keys = findWithIndex findMatch 0
+findMatchPosition keySequence = findWithIndex searchRows 0
where
- findMatch =
- fmap fst . findWithIndex (\c -> if c == keys then Just () else Nothing) 0
+ searchRows = fmap fst . findWithIndex searchInRow 0
+ searchInRow = guard . (== keySequence)
isValidKey :: SDL.Keycode -> Bool
-isValidKey key = Map.member key keycodeMapping
+isValidKey = (`Map.member` keycodeMapping)
generateKeyCells :: (Int, Int) -> KeySequence -> KeyGrid
generateKeyCells (rows, columns) hintKeys =
@@ -48,7 +43,7 @@ generateKeyCells (rows, columns) hintKeys =
| otherwise = 'J'
toKeyChar :: SDL.Keycode -> Maybe Char
-toKeyChar key = Map.lookup key keycodeMapping
+toKeyChar = (`Map.lookup` keycodeMapping)
eventToKeycode :: SDL.KeyboardEventData -> SDL.Keycode
eventToKeycode = SDL.keysymKeycode . SDL.keyboardEventKeysym