2022-08-03 05:24:35 +03:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
module Main where
|
|
|
|
|
|
|
|
import Lens.Micro ((^.))
|
|
|
|
import Lens.Micro.TH (makeLenses)
|
|
|
|
import Lens.Micro.Mtl ((<~), (.=), (%=), use)
|
|
|
|
import Control.Monad (void)
|
2022-08-03 05:40:23 +03:00
|
|
|
import qualified Data.Text.IO as Text
|
2022-08-03 05:24:35 +03:00
|
|
|
#if !(MIN_VERSION_base(4,11,0))
|
|
|
|
import Data.Monoid ((<>))
|
|
|
|
#endif
|
|
|
|
import qualified Graphics.Vty as V
|
|
|
|
|
|
|
|
import qualified Brick.Types as T
|
|
|
|
import Brick.Types (Widget)
|
|
|
|
import qualified Brick.Keybindings as K
|
|
|
|
import Brick.AttrMap
|
|
|
|
import qualified Brick.Main as M
|
|
|
|
import qualified Brick.Widgets.Border as B
|
|
|
|
import qualified Brick.Widgets.Center as C
|
|
|
|
import Brick.Widgets.Core
|
|
|
|
|
2022-08-03 05:42:57 +03:00
|
|
|
-- | The abstract key events for the application.
|
2022-08-03 05:24:35 +03:00
|
|
|
data KeyEvent = QuitEvent
|
|
|
|
| IncrementEvent
|
|
|
|
| DecrementEvent
|
|
|
|
deriving (Ord, Eq, Show)
|
|
|
|
|
2022-08-03 05:42:57 +03:00
|
|
|
-- | The mapping of key events to their configuration field names.
|
2022-08-03 05:24:35 +03:00
|
|
|
allKeyEvents :: K.KeyEvents KeyEvent
|
|
|
|
allKeyEvents =
|
|
|
|
K.keyEvents [ ("quit", QuitEvent)
|
|
|
|
, ("increment", IncrementEvent)
|
|
|
|
, ("decrement", DecrementEvent)
|
|
|
|
]
|
|
|
|
|
2022-08-03 05:42:57 +03:00
|
|
|
-- | Default key bindings for each abstract key event.
|
2022-08-03 05:24:35 +03:00
|
|
|
defaultBindings :: [(KeyEvent, [K.Binding])]
|
|
|
|
defaultBindings =
|
|
|
|
[ (QuitEvent, [K.ctrl 'q', K.bind V.KEsc])
|
|
|
|
, (IncrementEvent, [K.bind '+'])
|
|
|
|
, (DecrementEvent, [K.bind '-'])
|
|
|
|
]
|
|
|
|
|
|
|
|
data St =
|
|
|
|
St { _keyConfig :: K.KeyConfig KeyEvent
|
2022-08-03 05:42:57 +03:00
|
|
|
-- ^ The key config to use.
|
2022-08-03 05:24:35 +03:00
|
|
|
, _lastKey :: Maybe (V.Key, [V.Modifier])
|
2022-08-03 05:42:57 +03:00
|
|
|
-- ^ The last key that was pressed.
|
2022-08-03 05:24:35 +03:00
|
|
|
, _lastKeyHandled :: Bool
|
2022-08-03 05:42:57 +03:00
|
|
|
-- ^ Whether the last key was handled by a handler.
|
2022-08-03 05:24:35 +03:00
|
|
|
, _counter :: Int
|
2022-08-03 05:42:57 +03:00
|
|
|
-- ^ The counter value to manipulate in the handlers.
|
2022-08-03 05:24:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
makeLenses ''St
|
|
|
|
|
2022-08-03 05:52:14 +03:00
|
|
|
-- | Key event handlers for our application.
|
2022-08-03 05:24:35 +03:00
|
|
|
handlers :: [K.KeyEventHandler KeyEvent (T.EventM () St)]
|
|
|
|
handlers =
|
2022-08-03 05:51:05 +03:00
|
|
|
-- The first three handlers are triggered by keys mapped to events,
|
|
|
|
-- thus decoupling the configured key bindings from these handlers.
|
2022-08-03 05:24:35 +03:00
|
|
|
[ K.onEvent QuitEvent "Quit the program"
|
|
|
|
M.halt
|
|
|
|
, K.onEvent IncrementEvent "Increment the counter" $
|
|
|
|
counter %= succ
|
|
|
|
, K.onEvent DecrementEvent "Decrement the counter" $
|
|
|
|
counter %= subtract 1
|
2022-08-03 05:50:08 +03:00
|
|
|
|
|
|
|
-- This handler is always triggered by a specific key and thus
|
|
|
|
-- cannot be rebound to another key.
|
2022-08-03 05:48:47 +03:00
|
|
|
, K.onKey (K.bind '\t') "Increment the counter by 10" $
|
|
|
|
counter %= (+ 10)
|
2022-08-03 05:24:35 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
appEvent :: T.BrickEvent () e -> T.EventM () St ()
|
|
|
|
appEvent (T.VtyEvent (V.EvKey k mods)) = do
|
2022-08-03 05:42:57 +03:00
|
|
|
-- Dispatch the key to the event handler to which the key is mapped,
|
2022-08-03 05:44:05 +03:00
|
|
|
-- if any. Also record in lastKeyHandled whether the dispatcher
|
|
|
|
-- found a matching handler.
|
2022-08-03 05:24:35 +03:00
|
|
|
kc <- use keyConfig
|
|
|
|
let d = K.keyDispatcher kc handlers
|
|
|
|
lastKey .= Just (k, mods)
|
|
|
|
lastKeyHandled <~ K.handleKey d k mods
|
|
|
|
appEvent _ =
|
|
|
|
return ()
|
|
|
|
|
|
|
|
drawUi :: St -> [Widget ()]
|
|
|
|
drawUi st = [body]
|
|
|
|
where
|
|
|
|
binding = do
|
|
|
|
(k, mods) <- st^.lastKey
|
|
|
|
return $ K.Binding { K.kbKey = k
|
|
|
|
, K.kbMods = mods
|
|
|
|
}
|
2022-08-03 05:40:23 +03:00
|
|
|
keybindingHelp = K.keybindingHelpWidget (st^.keyConfig) handlers
|
2022-08-03 05:24:35 +03:00
|
|
|
status = hLimit 40 $
|
|
|
|
padRight Max $
|
|
|
|
vBox [ txt $ "Last key: " <> K.ppMaybeBinding binding
|
|
|
|
, str $ "Last key handled: " <> show (st^.lastKeyHandled)
|
|
|
|
, str $ "Counter: " <> show (st^.counter)
|
|
|
|
]
|
|
|
|
body = C.center $
|
|
|
|
(padRight (Pad 7) $ B.borderWithLabel (txt "Status") status) <+>
|
2022-08-03 05:40:23 +03:00
|
|
|
B.borderWithLabel (txt "Keybinding Help") keybindingHelp
|
2022-08-03 05:24:35 +03:00
|
|
|
|
|
|
|
app :: M.App St e ()
|
|
|
|
app =
|
|
|
|
M.App { M.appDraw = drawUi
|
|
|
|
, M.appStartEvent = return ()
|
|
|
|
, M.appHandleEvent = appEvent
|
|
|
|
, M.appAttrMap = const $ attrMap V.defAttr []
|
|
|
|
, M.appChooseCursor = M.showFirstCursor
|
|
|
|
}
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
2022-08-03 05:42:57 +03:00
|
|
|
-- Create a key config that has no customized bindings overriding
|
|
|
|
-- the default ones.
|
2022-08-03 05:24:35 +03:00
|
|
|
let kc = K.newKeyConfig allKeyEvents defaultBindings []
|
2022-08-03 05:42:57 +03:00
|
|
|
|
2022-08-03 05:24:35 +03:00
|
|
|
void $ M.defaultMain app $ St { _keyConfig = kc
|
|
|
|
, _lastKey = Nothing
|
|
|
|
, _lastKeyHandled = False
|
|
|
|
, _counter = 0
|
|
|
|
}
|
2022-08-03 05:40:23 +03:00
|
|
|
|
2022-08-03 05:44:44 +03:00
|
|
|
-- Now demonstrate how the library's generated key binding help text
|
|
|
|
-- looks in plain text and Markdown formats.
|
2022-08-03 05:40:23 +03:00
|
|
|
let sections = [("Main", handlers)]
|
|
|
|
|
|
|
|
putStrLn "Generated plain text help:"
|
|
|
|
Text.putStrLn $ K.keybindingTextTable kc sections
|
|
|
|
|
|
|
|
putStrLn "Generated Markdown help:"
|
|
|
|
Text.putStrLn $ K.keybindingMarkdownTable kc sections
|