aboutsummaryrefslogtreecommitdiff
path: root/src/Chelleport
diff options
context:
space:
mode:
Diffstat (limited to 'src/Chelleport')
-rw-r--r--src/Chelleport/AppState.hs5
-rw-r--r--src/Chelleport/Control.hs9
-rw-r--r--src/Chelleport/KeySequence.hs6
3 files changed, 13 insertions, 7 deletions
diff --git a/src/Chelleport/AppState.hs b/src/Chelleport/AppState.hs
index 72a1afe..8c7bac1 100644
--- a/src/Chelleport/AppState.hs
+++ b/src/Chelleport/AppState.hs
@@ -9,13 +9,12 @@ import Chelleport.Types
import Chelleport.Utils (cIntToInt, clamp, intToCInt, isEmpty, itemAt)
import Control.Monad (replicateM_)
import Data.Char (toLower)
-import Data.List (isInfixOf)
-import Data.Maybe (fromMaybe, isJust)
+import Data.Maybe (isJust)
import qualified Text.Fuzzy as Fuzzy
initialState :: (Monad m) => m (State, Maybe AppAction)
initialState = do
- let cells = fromMaybe (pure undefined) $ generateGrid 0 (rows, columns) hintKeys
+ let cells = either error id $ generateGrid 0 (rows, columns) hintKeys
pure (defaultAppState {stateGrid = cells}, Just $ SetMode defaultHintsMode)
where
rows = 9
diff --git a/src/Chelleport/Control.hs b/src/Chelleport/Control.hs
index a7c475f..b987564 100644
--- a/src/Chelleport/Control.hs
+++ b/src/Chelleport/Control.hs
@@ -1,12 +1,13 @@
module Chelleport.Control where
-import Chelleport.KeySequence (isAlphabetic, isKeycodeDigit)
+import Chelleport.KeySequence (isAlphabetic, isKeycodeDigit, toKeyChar)
import Chelleport.Types
import Chelleport.Utils
import Control.Concurrent (threadDelay)
import Control.Monad (unless)
import Control.Monad.IO.Class (MonadIO (liftIO))
import Control.Monad.Reader (MonadReader (ask))
+import Data.Maybe (fromMaybe)
import qualified Debug.Trace as Debug
import Foreign.C.Types
import qualified Graphics.X11 as X11
@@ -85,6 +86,9 @@ key keycode = (keycode ==) . eventToKeycode
ctrl :: SDL.KeyboardEventData -> Bool
ctrl ev = SDL.keyModifierLeftCtrl (keyModifier ev) || SDL.keyModifierRightCtrl (keyModifier ev)
+alt :: SDL.KeyboardEventData -> Bool
+alt ev = SDL.keyModifierLeftAlt (keyModifier ev) || SDL.keyModifierRightAlt (keyModifier ev)
+
shift :: SDL.KeyboardEventData -> Bool
shift ev = SDL.keyModifierLeftShift (keyModifier ev) || SDL.keyModifierRightShift (keyModifier ev)
@@ -94,6 +98,9 @@ anyDigit = isKeycodeDigit . eventToKeycode
anyAlphabetic :: SDL.KeyboardEventData -> Bool
anyAlphabetic = isAlphabetic . eventToKeycode
+hjkl :: SDL.KeyboardEventData -> Bool
+hjkl = (`elem` ("HJKL" :: String)) . fromMaybe ' ' . toKeyChar . eventToKeycode
+
hjklDirection :: Char -> Direction
hjklDirection = \case
'H' -> DirLeft
diff --git a/src/Chelleport/KeySequence.hs b/src/Chelleport/KeySequence.hs
index 70d56e2..141e71e 100644
--- a/src/Chelleport/KeySequence.hs
+++ b/src/Chelleport/KeySequence.hs
@@ -37,10 +37,10 @@ lcg seed = (multiplier * seed + increment) `mod` modulus
getIndexRounded :: Int -> [a] -> a
getIndexRounded i ls = ls !! (i `mod` length ls)
-generateGrid :: Int -> (Int, Int) -> KeySequence -> Maybe KeyGrid
+generateGrid :: Int -> (Int, Int) -> KeySequence -> Either String KeyGrid
generateGrid seed (rows, columns) hintKeys
- | rows * columns > length hintKeys * length hintKeys = Nothing
- | otherwise = Just $ (\row -> getKeySeq row <$> [0 .. columns - 1]) <$> [0 .. rows - 1]
+ | rows * columns >= length hintKeys * length hintKeys = Left "Row/Column counts too high"
+ | otherwise = Right $ (\row -> getKeySeq row <$> [0 .. columns - 1]) <$> [0 .. rows - 1]
where
allKeySeq = take numPairs . uniq $ generatePairs
numPairs = rows * columns