diff options
| -rw-r--r-- | specs/Specs/KeySequenceSpec.hs | 19 | ||||
| -rw-r--r-- | src/Chelleport.hs | 3 | ||||
| -rw-r--r-- | src/Chelleport/KeySequence.hs | 12 |
3 files changed, 27 insertions, 7 deletions
diff --git a/specs/Specs/KeySequenceSpec.hs b/specs/Specs/KeySequenceSpec.hs index 8b820b4..570f1f9 100644 --- a/specs/Specs/KeySequenceSpec.hs +++ b/specs/Specs/KeySequenceSpec.hs @@ -1,6 +1,6 @@ module Specs.KeySequenceSpec where -import Chelleport.KeySequence (nextChars) +import Chelleport.KeySequence (generateKeyCells, nextChars) import Test.Hspec test = do @@ -20,3 +20,20 @@ test = do it "returns nothing" $ do nextChars "FOO" [["XYZ", "ABC"], ["AMK", "BBL", "ABD"]] `shouldBe` Nothing + + describe "#generateKeyCells" $ do + it "generates grid of key sequences" $ do + generateKeyCells (4, 4) "ABCDEF" + `shouldBe` [ ["HKA", "HKB", "LKA", "LKB"], + ["HKC", "HKD", "LKC", "LKD"], + ["HJA", "HJB", "LJA", "LJB"], + ["HJC", "HJD", "LJC", "LJD"] + ] + context "when the the keys set is too short" $ do + it "cycles back to first character" $ do + generateKeyCells (4, 4) "AB" + `shouldBe` [ ["HKA", "HKB", "LKA", "LKB"], + ["HKA", "HKB", "LKA", "LKB"], + ["HJA", "HJB", "LJA", "LJB"], + ["HJA", "HJB", "LJA", "LJB"] + ] diff --git a/src/Chelleport.hs b/src/Chelleport.hs index f34bf27..74c61e7 100644 --- a/src/Chelleport.hs +++ b/src/Chelleport.hs @@ -6,7 +6,6 @@ import Chelleport.KeySequence (eventToKeycode, generateKeyCells, isValidKey, nex import Control.Monad (forM_, unless, void) import Data.IORef (modifyIORef', newIORef, readIORef) import Data.List (isPrefixOf) -import Data.Text (splitOn) import qualified Data.Text as Text import qualified SDL import Unsafe.Coerce (unsafeCoerce) @@ -34,7 +33,7 @@ initialState _ctx = do where rows = 16 columns = 16 - hintKeys = "ABCDEFGIMNOPRSTUVWXYZ" + hintKeys = "ABCDEFGHIJKLMNOPRSTUVWXYZ1234567890" render :: State -> DrawContext -> IO () render state ctx = do diff --git a/src/Chelleport/KeySequence.hs b/src/Chelleport/KeySequence.hs index 1d118cd..88658f1 100644 --- a/src/Chelleport/KeySequence.hs +++ b/src/Chelleport/KeySequence.hs @@ -24,12 +24,16 @@ generateKeyCells :: (Int, Int) -> [Char] -> [[[Char]]] generateKeyCells (rows, columns) hintKeys = (\row -> getCellSeq row <$> [1 .. columns]) <$> [1 .. rows] where - getCellSeq x y = [getPrefix1 x y, getPrefix2 x y, getKey x y] - getKey _row col = hintKeys !! (col `mod` (columns `div` 2)) - getPrefix1 _row col + getCellSeq row col = [getPrefixHoriz row col, getPrefixVert row col, getKey row col] + getKey row col = hintKeys !! index + where + index = (secRow * (columns `div` 2) + secCol) `mod` length hintKeys + secCol = (col - 1) `mod` (columns `div` 2) + secRow = (row - 1) `mod` (rows `div` 2) + getPrefixHoriz _row col | col <= (columns `div` 2) = 'H' | otherwise = 'L' - getPrefix2 row _col + getPrefixVert row _col | row <= (rows `div` 2) = 'K' | otherwise = 'J' |
