2020-08-06 01:18:44 +02:00
|
|
|
{-# LANGUAGE RecordWildCards #-}
|
|
|
|
{-# LANGUAGE DeriveAnyClass #-}
|
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
2020-08-05 22:52:10 +02:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
module Keyboard where
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
import Utils
|
2020-08-12 10:46:36 +02:00
|
|
|
import Data.Coerce
|
2020-08-06 01:18:44 +02:00
|
|
|
import Data.Hashable (Hashable)
|
|
|
|
import GHC.Generics (Generic)
|
|
|
|
|
2020-08-05 22:52:10 +02:00
|
|
|
import qualified Data.List as List
|
2020-08-06 01:18:44 +02:00
|
|
|
import qualified Data.HashMap.Strict as HM
|
2020-08-05 22:52:10 +02:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
newtype Keyboard = Keyboard [[Char]]
|
2020-08-06 00:20:18 +02:00
|
|
|
deriving (Eq)
|
2020-08-05 22:52:10 +02:00
|
|
|
|
|
|
|
instance Show Keyboard where
|
|
|
|
show (Keyboard xxs) =
|
|
|
|
xxs |> fmap printRow |> List.intercalate "\n"
|
|
|
|
where
|
|
|
|
printRow :: [Char] -> String
|
|
|
|
printRow xs =
|
|
|
|
xs |> fmap (\x -> '[':x:']':"") |> List.intercalate ""
|
|
|
|
|
2020-08-06 01:18:44 +02:00
|
|
|
data Coord = Coord
|
|
|
|
{ row :: Int
|
|
|
|
, col :: Int
|
|
|
|
} deriving (Eq, Show, Generic)
|
|
|
|
|
|
|
|
instance Hashable Coord
|
|
|
|
|
|
|
|
-- | List of characters to their QWERTY coordinatees.
|
|
|
|
coords :: [(Char, Coord)]
|
2020-08-12 10:46:36 +02:00
|
|
|
coords =
|
|
|
|
qwerty
|
|
|
|
|> coerce
|
|
|
|
|> fmap (zip [0..])
|
|
|
|
|> zip [0..]
|
|
|
|
|> fmap (\(row, xs) -> xs |> fmap (\(col, char) -> (char, Coord row col)))
|
|
|
|
|> mconcat
|
2020-08-06 01:18:44 +02:00
|
|
|
|
|
|
|
-- | Mapping of characters to their coordinates on a QWERTY keyboard with the
|
|
|
|
-- top-left corner as 0,0.
|
|
|
|
charToCoord :: HM.HashMap Char Coord
|
|
|
|
charToCoord = HM.fromList coords
|
|
|
|
|
|
|
|
coordToChar :: Keyboard -> Coord -> Maybe Char
|
|
|
|
coordToChar (Keyboard xxs) Coord{..} =
|
|
|
|
Just $ xxs !! row !! col
|
|
|
|
|
2020-08-05 22:52:10 +02:00
|
|
|
qwerty :: Keyboard
|
|
|
|
qwerty = Keyboard [ ['1','2','3','4','5','6','7','8','9','0']
|
|
|
|
, ['Q','W','E','R','T','Y','U','I','O','P']
|
|
|
|
, ['A','S','D','F','G','H','J','K','L',';']
|
|
|
|
, ['Z','X','C','V','B','N','M',',','.','/']
|
|
|
|
]
|