module Brick.Keybindings.KeyEvents
( KeyEvents
, keyEvents
, keyEventsList
, lookupKeyEvent
, keyEventName
)
where
import qualified Data.Bimap as B
import qualified Data.Text as T
data KeyEvents k = KeyEvents (B.Bimap T.Text k)
deriving (KeyEvents k -> KeyEvents k -> Bool
forall k. Eq k => KeyEvents k -> KeyEvents k -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: KeyEvents k -> KeyEvents k -> Bool
$c/= :: forall k. Eq k => KeyEvents k -> KeyEvents k -> Bool
== :: KeyEvents k -> KeyEvents k -> Bool
$c== :: forall k. Eq k => KeyEvents k -> KeyEvents k -> Bool
Eq, Int -> KeyEvents k -> ShowS
forall k. Show k => Int -> KeyEvents k -> ShowS
forall k. Show k => [KeyEvents k] -> ShowS
forall k. Show k => KeyEvents k -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [KeyEvents k] -> ShowS
$cshowList :: forall k. Show k => [KeyEvents k] -> ShowS
show :: KeyEvents k -> String
$cshow :: forall k. Show k => KeyEvents k -> String
showsPrec :: Int -> KeyEvents k -> ShowS
$cshowsPrec :: forall k. Show k => Int -> KeyEvents k -> ShowS
Show)
keyEvents :: (Ord k) => [(T.Text, k)] -> KeyEvents k
keyEvents :: forall k. Ord k => [(Text, k)] -> KeyEvents k
keyEvents [(Text, k)]
pairs =
let m :: Bimap Text k
m = forall a b. (Ord a, Ord b) => [(a, b)] -> Bimap a b
B.fromList [(Text -> Text
T.strip forall a b. (a -> b) -> a -> b
$ Text -> Text
T.toLower Text
n, k
e) | (Text
n, k
e) <- [(Text, k)]
pairs]
in if forall a b. Bimap a b -> Int
B.size Bimap Text k
m forall a. Eq a => a -> a -> Bool
/= forall (t :: * -> *) a. Foldable t => t a -> Int
length [(Text, k)]
pairs
then forall a. HasCallStack => String -> a
error String
"keyEvents: input list contains duplicates by name or by event value"
else forall k. Bimap Text k -> KeyEvents k
KeyEvents forall a b. (a -> b) -> a -> b
$ forall a b. (Ord a, Ord b) => [(a, b)] -> Bimap a b
B.fromList [(Text, k)]
pairs
keyEventsList :: KeyEvents k -> [(T.Text, k)]
keyEventsList :: forall k. KeyEvents k -> [(Text, k)]
keyEventsList (KeyEvents Bimap Text k
m) = forall a b. Bimap a b -> [(a, b)]
B.toList Bimap Text k
m
lookupKeyEvent :: (Ord k) => KeyEvents k -> T.Text -> Maybe k
lookupKeyEvent :: forall k. Ord k => KeyEvents k -> Text -> Maybe k
lookupKeyEvent (KeyEvents Bimap Text k
m) Text
name = forall a b (m :: * -> *).
(Ord a, Ord b, MonadThrow m) =>
a -> Bimap a b -> m b
B.lookup (Text -> Text
T.strip forall a b. (a -> b) -> a -> b
$ Text -> Text
T.toLower Text
name) Bimap Text k
m
keyEventName :: (Ord k) => KeyEvents k -> k -> Maybe T.Text
keyEventName :: forall k. Ord k => KeyEvents k -> k -> Maybe Text
keyEventName (KeyEvents Bimap Text k
m) k
e = forall a b (m :: * -> *).
(Ord a, Ord b, MonadThrow m) =>
b -> Bimap a b -> m a
B.lookupR k
e Bimap Text k
m