aboutsummaryrefslogtreecommitdiff
path: root/src/Chelleport/KeySequence.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Chelleport/KeySequence.hs')
-rw-r--r--src/Chelleport/KeySequence.hs37
1 files changed, 21 insertions, 16 deletions
diff --git a/src/Chelleport/KeySequence.hs b/src/Chelleport/KeySequence.hs
index 1046a98..1bc50a5 100644
--- a/src/Chelleport/KeySequence.hs
+++ b/src/Chelleport/KeySequence.hs
@@ -2,20 +2,25 @@ module Chelleport.KeySequence where
import Data.List (isPrefixOf, nub)
import qualified Data.Map as Map
-import Data.Maybe (isJust)
import qualified SDL
+type Cell = [Char]
+
+type KeySequence = [Char]
+
+type KeyGrid = [[Cell]]
+
-- padded :: Int -> a -> [a] -> [a]
-- padded 0 _ ls = ls
-- padded n x ls
-- | length ls > n = ls
-- | otherwise = padded (n - 1) x (ls ++ [x])
-safeHead :: a -> [a] -> a
-safeHead def [] = def
-safeHead _ (x : _) = x
+-- safeHead :: a -> [a] -> a
+-- safeHead def [] = def
+-- safeHead _ (x : _) = x
-nextChars :: [Char] -> [[[Char]]] -> Maybe [Char]
+nextChars :: KeySequence -> KeyGrid -> Maybe [Char]
nextChars keys cells =
case matches of
[] -> Nothing
@@ -24,23 +29,23 @@ nextChars keys cells =
matches = concatMap (filter (isPrefixOf keys)) cells
result = concatMap (take 1 . drop (length keys)) matches
-findMatchPosition :: [Char] -> [[[Char]]] -> Maybe (Int, Int)
+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
+
+findMatchPosition :: KeySequence -> KeyGrid -> Maybe (Int, Int)
findMatchPosition keys = findWithIndex findMatch 0
where
- findMatch row =
- fst <$> findWithIndex (\c -> if c == keys then Just () else Nothing) 0 row
-
- 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
+ findMatch =
+ fmap fst . findWithIndex (\c -> if c == keys then Just () else Nothing) 0
isValidKey :: SDL.Keycode -> Bool
isValidKey key = Map.member key keycodeMapping
-generateKeyCells :: (Int, Int) -> [Char] -> [[[Char]]]
+generateKeyCells :: (Int, Int) -> KeySequence -> KeyGrid
generateKeyCells (rows, columns) hintKeys =
(\row -> getCellSeq row <$> [1 .. columns]) <$> [1 .. rows]
where