mirror of
https://github.com/jtdaugherty/brick.git
synced 2024-12-04 17:36:43 +03:00
fc8cfe3b4a
This change makes it possible for brick to extent the event space using its own event notions in addition those provided by Vty and the application itself. This means we no longer need the user to provide the type and appLiftVtyEvent went away. This makes pattern-matching in event handlers a little noisier with the benefit that we can now add events like mouse clicks or drags to the event type.
68 lines
1.5 KiB
Haskell
68 lines
1.5 KiB
Haskell
{-# LANGUAGE OverloadedStrings #-}
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
module Main where
|
|
|
|
import Lens.Micro ((.~), (^.), (&))
|
|
import Lens.Micro.TH (makeLenses)
|
|
import Control.Monad (void)
|
|
import Data.Monoid
|
|
import Data.Default
|
|
import qualified Graphics.Vty as V
|
|
|
|
import Brick.Main
|
|
( App(..), neverShowCursor, defaultMain
|
|
, suspendAndResume, halt, continue
|
|
)
|
|
import Brick.Types
|
|
( Widget
|
|
, EventM
|
|
, Next
|
|
, BrickEvent(..)
|
|
)
|
|
import Brick.Widgets.Core
|
|
( vBox
|
|
, str
|
|
)
|
|
|
|
data St =
|
|
St { _stExternalInput :: String
|
|
}
|
|
|
|
makeLenses ''St
|
|
|
|
drawUI :: St -> [Widget ()]
|
|
drawUI st = [ui]
|
|
where
|
|
ui = vBox [ str $ "External input: \"" <> st^.stExternalInput <> "\""
|
|
, str "(Press Esc to quit or Space to ask for input)"
|
|
]
|
|
|
|
appEvent :: St -> BrickEvent () e -> EventM () (Next St)
|
|
appEvent st (VtyEvent e) =
|
|
case e of
|
|
V.EvKey V.KEsc [] -> halt st
|
|
V.EvKey (V.KChar ' ') [] -> suspendAndResume $ do
|
|
putStrLn "Suspended. Please enter something and press enter to resume:"
|
|
s <- getLine
|
|
return $ st & stExternalInput .~ s
|
|
_ -> continue st
|
|
appEvent st _ = continue st
|
|
|
|
initialState :: St
|
|
initialState =
|
|
St { _stExternalInput = ""
|
|
}
|
|
|
|
theApp :: App St e ()
|
|
theApp =
|
|
App { appDraw = drawUI
|
|
, appChooseCursor = neverShowCursor
|
|
, appHandleEvent = appEvent
|
|
, appStartEvent = return
|
|
, appAttrMap = const def
|
|
}
|
|
|
|
main :: IO ()
|
|
main =
|
|
void $ defaultMain theApp initialState
|