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)
|
2022-08-19 00:59:00 +03:00
|
|
|
import Control.Monad (void, forM_, when)
|
|
|
|
import qualified Data.Set as S
|
|
|
|
import Data.Maybe (fromJust)
|
2022-08-05 05:07:47 +03:00
|
|
|
import qualified Data.Text as Text
|
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
|
2022-08-05 05:07:47 +03:00
|
|
|
import System.Environment (getArgs)
|
|
|
|
import System.Exit (exitFailure)
|
2022-08-03 05:24:35 +03:00
|
|
|
|
|
|
|
import qualified Brick.Types as T
|
|
|
|
import Brick.Types (Widget)
|
|
|
|
import qualified Brick.Keybindings as K
|
|
|
|
import Brick.AttrMap
|
2022-08-09 00:08:47 +03:00
|
|
|
import Brick.Util (fg)
|
2022-08-03 05:24:35 +03:00
|
|
|
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 =
|
2022-08-03 18:19:43 +03:00
|
|
|
K.keyEvents [ ("quit", QuitEvent)
|
2022-08-03 05:24:35 +03:00
|
|
|
, ("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 =
|
2022-08-03 18:19:43 +03:00
|
|
|
[ (QuitEvent, [K.ctrl 'q', K.bind V.KEsc])
|
2022-08-03 05:24:35 +03:00
|
|
|
, (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-09 00:21:19 +03:00
|
|
|
, _customBindingsPath :: Maybe FilePath
|
2022-08-05 05:13:13 +03:00
|
|
|
-- ^ Set if the application found custom keybindings in the
|
2022-08-05 05:07:47 +03:00
|
|
|
-- specified file.
|
2022-08-19 04:49:43 +03:00
|
|
|
, _dispatcher :: K.KeyDispatcher KeyEvent (T.EventM () St)
|
|
|
|
-- ^ The key dispatcher we'll use to dispatch input events.
|
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 06:13:22 +03:00
|
|
|
-- The first three handlers are triggered by keys mapped to abstract
|
|
|
|
-- 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
|
|
|
|
2022-08-03 06:13:01 +03:00
|
|
|
-- These handlers are always triggered by specific keys and thus
|
|
|
|
-- cannot be rebound.
|
2022-08-03 05:48:47 +03:00
|
|
|
, K.onKey (K.bind '\t') "Increment the counter by 10" $
|
|
|
|
counter %= (+ 10)
|
2022-08-03 06:13:01 +03:00
|
|
|
, K.onKey (K.bind V.KBackTab) "Decrement the counter by 10" $
|
|
|
|
counter %= subtract 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-19 04:49:43 +03:00
|
|
|
d <- use dispatcher
|
2022-08-03 05:24:35 +03:00
|
|
|
lastKey .= Just (k, mods)
|
|
|
|
lastKeyHandled <~ K.handleKey d k mods
|
|
|
|
appEvent _ =
|
|
|
|
return ()
|
|
|
|
|
|
|
|
drawUi :: St -> [Widget ()]
|
|
|
|
drawUi st = [body]
|
|
|
|
where
|
2022-08-03 06:10:33 +03:00
|
|
|
binding = uncurry K.binding <$> st^.lastKey
|
2022-08-05 05:21:38 +03:00
|
|
|
|
|
|
|
-- Generate key binding help using the library so we can embed
|
|
|
|
-- it in the UI.
|
|
|
|
keybindingHelp = B.borderWithLabel (txt "Active Keybindings") $
|
|
|
|
K.keybindingHelpWidget (st^.keyConfig) handlers
|
|
|
|
|
2022-08-09 00:19:50 +03:00
|
|
|
lastKeyDisplay = withDefAttr lastKeyAttr $
|
|
|
|
txt $ maybe "(none)" K.ppBinding binding
|
|
|
|
|
2022-08-05 05:21:38 +03:00
|
|
|
-- Show the status of the last pressed key, whether we handled
|
|
|
|
-- it, and other bits of the application state.
|
|
|
|
status = B.borderWithLabel (txt "Status") $
|
|
|
|
hLimit 40 $
|
2022-08-03 05:24:35 +03:00
|
|
|
padRight Max $
|
2022-08-09 00:19:50 +03:00
|
|
|
vBox [ txt "Last key: " <+> lastKeyDisplay
|
2022-08-03 05:24:35 +03:00
|
|
|
, str $ "Last key handled: " <> show (st^.lastKeyHandled)
|
|
|
|
, str $ "Counter: " <> show (st^.counter)
|
|
|
|
]
|
2022-08-05 05:21:38 +03:00
|
|
|
|
|
|
|
-- Show info about whether the application is currently using
|
|
|
|
-- custom bindings loaded from an INI file.
|
2022-08-05 05:07:47 +03:00
|
|
|
customBindingInfo =
|
|
|
|
B.borderWithLabel (txt "Custom Bindings") $
|
2022-08-09 00:21:19 +03:00
|
|
|
case st^.customBindingsPath of
|
2022-08-05 05:07:47 +03:00
|
|
|
Nothing ->
|
|
|
|
hLimit 40 $
|
2022-08-09 00:20:39 +03:00
|
|
|
txtWrap $ "No custom bindings loaded. Create an INI file with a " <>
|
2022-08-05 05:07:47 +03:00
|
|
|
(Text.pack $ show sectionName) <>
|
2022-08-05 05:10:31 +03:00
|
|
|
" section or use 'programs/custom_keys.ini'. " <>
|
2022-08-09 00:20:39 +03:00
|
|
|
"Pass its path to this program on the command line."
|
2022-08-05 05:07:47 +03:00
|
|
|
Just f -> str "Loaded custom bindings from:" <=> str (show f)
|
2022-08-05 05:21:38 +03:00
|
|
|
|
2022-08-03 05:24:35 +03:00
|
|
|
body = C.center $
|
2022-08-05 05:21:38 +03:00
|
|
|
(padRight (Pad 2) $ status <=> customBindingInfo) <+>
|
|
|
|
keybindingHelp
|
2022-08-03 05:24:35 +03:00
|
|
|
|
2022-08-09 00:15:09 +03:00
|
|
|
lastKeyAttr :: AttrName
|
|
|
|
lastKeyAttr = attrName "lastKey"
|
|
|
|
|
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
|
2022-08-09 00:08:47 +03:00
|
|
|
, M.appAttrMap = const $ attrMap V.defAttr [ (K.eventNameAttr, fg V.magenta)
|
|
|
|
, (K.eventDescriptionAttr, fg V.cyan)
|
|
|
|
, (K.keybindingAttr, fg V.yellow)
|
2022-08-09 00:15:09 +03:00
|
|
|
, (lastKeyAttr, fg V.white `V.withStyle` V.bold)
|
2022-08-09 00:08:47 +03:00
|
|
|
]
|
2022-08-03 05:24:35 +03:00
|
|
|
, M.appChooseCursor = M.showFirstCursor
|
|
|
|
}
|
|
|
|
|
2022-08-05 05:07:47 +03:00
|
|
|
sectionName :: Text.Text
|
|
|
|
sectionName = "keybindings"
|
|
|
|
|
2022-08-03 05:24:35 +03:00
|
|
|
main :: IO ()
|
|
|
|
main = do
|
2022-08-05 05:07:47 +03:00
|
|
|
args <- getArgs
|
|
|
|
|
2022-08-05 05:16:15 +03:00
|
|
|
-- If the command line specified the path to an INI file with custom
|
|
|
|
-- bindings, attempt to load it.
|
2022-08-05 05:07:47 +03:00
|
|
|
(customBindings, customFile) <- case args of
|
|
|
|
[iniFilePath] -> do
|
|
|
|
result <- K.keybindingsFromFile allKeyEvents sectionName iniFilePath
|
|
|
|
case result of
|
2022-08-05 05:16:15 +03:00
|
|
|
-- A section was found and had zero more bindings.
|
2022-08-05 05:07:47 +03:00
|
|
|
Right (Just bindings) ->
|
|
|
|
return (bindings, Just iniFilePath)
|
2022-08-05 05:16:15 +03:00
|
|
|
|
|
|
|
-- No section was found at all.
|
2022-08-05 05:07:47 +03:00
|
|
|
Right Nothing -> do
|
|
|
|
putStrLn $ "Error: found no section " <> show sectionName <> " in " <> show iniFilePath
|
|
|
|
exitFailure
|
2022-08-05 05:16:15 +03:00
|
|
|
|
|
|
|
-- There was some problem parsing the file as an INI
|
|
|
|
-- file.
|
2022-08-05 05:07:47 +03:00
|
|
|
Left e -> do
|
|
|
|
putStrLn $ "Error reading keybindings file " <> show iniFilePath <> ": " <> e
|
|
|
|
exitFailure
|
2022-08-05 05:16:15 +03:00
|
|
|
|
2022-08-05 05:07:47 +03:00
|
|
|
_ -> return ([], Nothing)
|
|
|
|
|
2022-08-05 05:16:15 +03:00
|
|
|
-- Create a key config that includes the default bindings as well as
|
|
|
|
-- the custom bindings we loaded from the INI file, if any.
|
2022-08-05 05:07:47 +03:00
|
|
|
let kc = K.newKeyConfig allKeyEvents defaultBindings customBindings
|
2022-08-03 05:42:57 +03:00
|
|
|
|
2022-08-19 00:59:00 +03:00
|
|
|
-- Before starting the application, check on whether any events have
|
|
|
|
-- colliding bindings. Exit if so.
|
2022-08-19 02:21:43 +03:00
|
|
|
--
|
|
|
|
-- Note that in a Real Application, we would more than likely
|
|
|
|
-- want to check for collisions among specific sets of
|
|
|
|
-- events. For example, if 'Esc' was bound to both 'quit' and
|
|
|
|
-- 'close-dialog-box', we might not care about such a collision
|
|
|
|
-- if the application only ever handled the 'close-dialog-box'
|
|
|
|
-- event in a separate mode and only ever handled 'quit' at the
|
|
|
|
-- top-level of the event handler. But if we had two events such as
|
|
|
|
-- 'dialog-box-okay' and 'dialog-box-cancel' that were intended to
|
|
|
|
-- be handled in the same mode, we might want to check that those
|
|
|
|
-- two events did not have the same binding.
|
2022-08-19 04:22:50 +03:00
|
|
|
forM_ (K.keyEventMappings kc) $ \(b, evs) -> do
|
2022-08-19 00:59:00 +03:00
|
|
|
when (S.size evs > 1) $ do
|
|
|
|
Text.putStrLn $ "Error: key '" <> K.ppBinding b <> "' is bound to multiple events:"
|
|
|
|
forM_ evs $ \e ->
|
|
|
|
Text.putStrLn $ " " <> Text.pack (show e) <> " (" <> fromJust (K.keyEventName allKeyEvents e) <> ")"
|
|
|
|
exitFailure
|
|
|
|
|
2022-08-19 04:49:43 +03:00
|
|
|
-- Now build a key dispatcher for our event handlers. If this fails
|
|
|
|
-- due to key collision detection, we'll print out info about the
|
|
|
|
-- collisions.
|
|
|
|
d <- case K.keyDispatcher kc handlers of
|
|
|
|
Right d -> return d
|
|
|
|
Left collisions -> do
|
|
|
|
putStrLn "Error: some key events have the same keys bound to them."
|
|
|
|
|
|
|
|
forM_ collisions $ \(b, hs) -> do
|
|
|
|
Text.putStrLn $ "Handlers with the '" <> K.ppBinding b <> "' binding:"
|
|
|
|
forM_ hs $ \h -> do
|
|
|
|
let trigger = case K.kehEventTrigger $ K.khHandler h of
|
|
|
|
K.ByKey k -> "triggered by the key '" <> K.ppBinding k <> "'"
|
|
|
|
K.ByEvent e -> "triggered by the event '" <> fromJust (K.keyEventName allKeyEvents e) <> "'"
|
|
|
|
desc = K.handlerDescription $ K.kehHandler $ K.khHandler h
|
|
|
|
|
|
|
|
Text.putStrLn $ " " <> desc <> " (" <> trigger <> ")"
|
|
|
|
|
|
|
|
exitFailure
|
|
|
|
|
2022-08-03 05:24:35 +03:00
|
|
|
void $ M.defaultMain app $ St { _keyConfig = kc
|
|
|
|
, _lastKey = Nothing
|
|
|
|
, _lastKeyHandled = False
|
|
|
|
, _counter = 0
|
2022-08-09 00:21:19 +03:00
|
|
|
, _customBindingsPath = customFile
|
2022-08-19 04:49:43 +03:00
|
|
|
, _dispatcher = d
|
2022-08-03 05:24:35 +03:00
|
|
|
}
|
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
|
2022-08-05 05:16:44 +03:00
|
|
|
-- looks in plain text and Markdown formats. These can be used to
|
2022-08-05 05:23:34 +03:00
|
|
|
-- generate documentation for users. Note that the output generated
|
|
|
|
-- here takes the active bindings into account! If you don't want
|
|
|
|
-- that, use handlers that were built from a KeyConfig that didn't
|
|
|
|
-- have any custom bindings applied.
|
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
|