module Yi.Frontend.Vty.Conversions
( colorToAttr
, fromVtyEvent
, fromVtyKey
, fromVtyMod
) where
import Data.List (nub, sort)
import qualified Graphics.Vty as Vty (Attr, Color, Event (EvKey), Key (KBS, KBackTab, KBegin, KCenter, KChar, KDel, KDown, KEnd, KEnter, KEsc, KFun, KHome, KIns, KLeft, KMenu, KPageDown, KPageUp, KPause, KPrtScr, KRight, KUp),
Modifier (..), black, blue, brightBlack,
brightBlue, brightCyan, brightGreen,
brightMagenta, brightRed, brightWhite,
brightYellow, cyan, green, magenta, red,
rgbColor, white, yellow)
import qualified Yi.Event (Event (..), Key (..), Modifier (MCtrl, MMeta, MShift))
import qualified Yi.Style (Color (..))
fromVtyEvent :: Vty.Event -> Yi.Event.Event
fromVtyEvent :: Event -> Event
fromVtyEvent (Vty.EvKey Vty.KBackTab mods :: [Modifier]
mods) =
Key -> [Modifier] -> Event
Yi.Event.Event Key
Yi.Event.KTab ([Modifier] -> [Modifier]
forall a. Ord a => [a] -> [a]
sort ([Modifier] -> [Modifier]) -> [Modifier] -> [Modifier]
forall a b. (a -> b) -> a -> b
$ [Modifier] -> [Modifier]
forall a. Eq a => [a] -> [a]
nub ([Modifier] -> [Modifier]) -> [Modifier] -> [Modifier]
forall a b. (a -> b) -> a -> b
$ Modifier
Yi.Event.MShift Modifier -> [Modifier] -> [Modifier]
forall a. a -> [a] -> [a]
: (Modifier -> Modifier) -> [Modifier] -> [Modifier]
forall a b. (a -> b) -> [a] -> [b]
map Modifier -> Modifier
fromVtyMod [Modifier]
mods)
fromVtyEvent (Vty.EvKey k :: Key
k mods :: [Modifier]
mods) =
Key -> [Modifier] -> Event
Yi.Event.Event (Key -> Key
fromVtyKey Key
k) ([Modifier] -> [Modifier]
forall a. Ord a => [a] -> [a]
sort ([Modifier] -> [Modifier]) -> [Modifier] -> [Modifier]
forall a b. (a -> b) -> a -> b
$ (Modifier -> Modifier) -> [Modifier] -> [Modifier]
forall a b. (a -> b) -> [a] -> [b]
map Modifier -> Modifier
fromVtyMod [Modifier]
mods)
fromVtyEvent _ = [Char] -> Event
forall a. HasCallStack => [Char] -> a
error "fromVtyEvent: unsupported event encountered."
fromVtyKey :: Vty.Key -> Yi.Event.Key
fromVtyKey :: Key -> Key
fromVtyKey (Key
Vty.KEsc ) = Key
Yi.Event.KEsc
fromVtyKey (Vty.KFun x :: Int
x ) = Int -> Key
Yi.Event.KFun Int
x
fromVtyKey (Key
Vty.KPrtScr ) = Key
Yi.Event.KPrtScr
fromVtyKey (Key
Vty.KPause ) = Key
Yi.Event.KPause
fromVtyKey (Vty.KChar '\t') = Key
Yi.Event.KTab
fromVtyKey (Vty.KChar c :: Char
c ) = Char -> Key
Yi.Event.KASCII Char
c
fromVtyKey (Key
Vty.KBS ) = Key
Yi.Event.KBS
fromVtyKey (Key
Vty.KIns ) = Key
Yi.Event.KIns
fromVtyKey (Key
Vty.KHome ) = Key
Yi.Event.KHome
fromVtyKey (Key
Vty.KPageUp ) = Key
Yi.Event.KPageUp
fromVtyKey (Key
Vty.KDel ) = Key
Yi.Event.KDel
fromVtyKey (Key
Vty.KEnd ) = Key
Yi.Event.KEnd
fromVtyKey (Key
Vty.KPageDown ) = Key
Yi.Event.KPageDown
fromVtyKey (Key
Vty.KCenter ) = Key
Yi.Event.KNP5
fromVtyKey (Key
Vty.KUp ) = Key
Yi.Event.KUp
fromVtyKey (Key
Vty.KMenu ) = Key
Yi.Event.KMenu
fromVtyKey (Key
Vty.KLeft ) = Key
Yi.Event.KLeft
fromVtyKey (Key
Vty.KDown ) = Key
Yi.Event.KDown
fromVtyKey (Key
Vty.KRight ) = Key
Yi.Event.KRight
fromVtyKey (Key
Vty.KEnter ) = Key
Yi.Event.KEnter
fromVtyKey (Key
Vty.KBackTab ) = [Char] -> Key
forall a. HasCallStack => [Char] -> a
error "This should be handled in fromVtyEvent"
fromVtyKey (Key
Vty.KBegin ) = [Char] -> Key
forall a. HasCallStack => [Char] -> a
error "Yi.Frontend.Vty.Conversions.fromVtyKey: can't handle KBegin"
fromVtyKey _ = [Char] -> Key
forall a. HasCallStack => [Char] -> a
error "Unhandled key in fromVtyKey"
fromVtyMod :: Vty.Modifier -> Yi.Event.Modifier
fromVtyMod :: Modifier -> Modifier
fromVtyMod Vty.MShift = Modifier
Yi.Event.MShift
fromVtyMod Vty.MCtrl = Modifier
Yi.Event.MCtrl
fromVtyMod Vty.MMeta = Modifier
Yi.Event.MMeta
fromVtyMod Vty.MAlt = Modifier
Yi.Event.MMeta
colorToAttr :: (Vty.Color -> Vty.Attr -> Vty.Attr)
-> Yi.Style.Color -> Vty.Attr -> Vty.Attr
colorToAttr :: (Color -> Attr -> Attr) -> Color -> Attr -> Attr
colorToAttr set :: Color -> Attr -> Attr
set c :: Color
c =
case Color
c of
Yi.Style.RGB 0 0 0 -> Color -> Attr -> Attr
set Color
Vty.black
Yi.Style.RGB 128 128 128 -> Color -> Attr -> Attr
set Color
Vty.brightBlack
Yi.Style.RGB 139 0 0 -> Color -> Attr -> Attr
set Color
Vty.red
Yi.Style.RGB 255 0 0 -> Color -> Attr -> Attr
set Color
Vty.brightRed
Yi.Style.RGB 0 100 0 -> Color -> Attr -> Attr
set Color
Vty.green
Yi.Style.RGB 0 128 0 -> Color -> Attr -> Attr
set Color
Vty.brightGreen
Yi.Style.RGB 165 42 42 -> Color -> Attr -> Attr
set Color
Vty.yellow
Yi.Style.RGB 255 255 0 -> Color -> Attr -> Attr
set Color
Vty.brightYellow
Yi.Style.RGB 0 0 139 -> Color -> Attr -> Attr
set Color
Vty.blue
Yi.Style.RGB 0 0 255 -> Color -> Attr -> Attr
set Color
Vty.brightBlue
Yi.Style.RGB 128 0 128 -> Color -> Attr -> Attr
set Color
Vty.magenta
Yi.Style.RGB 255 0 255 -> Color -> Attr -> Attr
set Color
Vty.brightMagenta
Yi.Style.RGB 0 139 139 -> Color -> Attr -> Attr
set Color
Vty.cyan
Yi.Style.RGB 0 255 255 -> Color -> Attr -> Attr
set Color
Vty.brightCyan
Yi.Style.RGB 165 165 165 -> Color -> Attr -> Attr
set Color
Vty.white
Yi.Style.RGB 255 255 255 -> Color -> Attr -> Attr
set Color
Vty.brightWhite
Yi.Style.Default -> Attr -> Attr
forall a. a -> a
id
Yi.Style.RGB r :: Word8
r g :: Word8
g b :: Word8
b -> Color -> Attr -> Attr
set (Word8 -> Word8 -> Word8 -> Color
forall i. Integral i => i -> i -> i -> Color
Vty.rgbColor Word8
r Word8
g Word8
b)