Terminal clock and countdown timer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Binding.hs 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. {-
  2. This file is part of the package clockdown. It is subject to the
  3. license terms in the LICENSE file found in the top-level directory of
  4. this distribution and at git://pmade.com/clockdown/LICENSE. No part of
  5. the clockdown package, including this file, may be copied, modified,
  6. propagated, or distributed except according to the terms contained in
  7. the LICENSE file.
  8. -}
  9. --------------------------------------------------------------------------------
  10. module Clockdown.UI.Term.Binding
  11. ( eventToAction
  12. ) where
  13. --------------------------------------------------------------------------------
  14. -- Library imports:
  15. import qualified Graphics.Vty as V
  16. --------------------------------------------------------------------------------
  17. -- Local imports:
  18. import Clockdown.Core.Action
  19. import Clockdown.Core.Binding
  20. import Clockdown.Core.Config
  21. --------------------------------------------------------------------------------
  22. eventToAction :: V.Event -> Config -> Maybe Action
  23. eventToAction e c = do
  24. key <- convertVtyEvent (fixupKeys e)
  25. processBinding (configKeys c) key
  26. --------------------------------------------------------------------------------
  27. -- | Fix some Vty key events so that they report the correct keys.
  28. fixupKeys :: V.Event -> V.Event
  29. fixupKeys (V.EvKey V.KBackTab []) = V.EvKey (V.KChar '\t') [V.MShift]
  30. fixupKeys e = e
  31. --------------------------------------------------------------------------------
  32. -- | Convert Vty events into Clockdown bindings.
  33. convertVtyEvent :: V.Event -> Maybe ([KeyModifier], KeyCode)
  34. convertVtyEvent (V.EvKey key ms) = (,) (map convertMod ms) <$> convertKey key
  35. convertVtyEvent _ = Nothing
  36. --------------------------------------------------------------------------------
  37. convertMod :: V.Modifier -> KeyModifier
  38. convertMod V.MShift = Shift
  39. convertMod V.MCtrl = Control
  40. convertMod V.MMeta = Meta
  41. convertMod V.MAlt = Meta
  42. --------------------------------------------------------------------------------
  43. convertKey :: V.Key -> Maybe KeyCode
  44. convertKey (V.KChar c) = Just (RawKey c)
  45. convertKey V.KEsc = Just Escape
  46. convertKey V.KBS = Just Backspace
  47. convertKey (V.KFun n) = Just (FKey n)
  48. convertKey _ = Nothing