mirror of
https://github.com/jtdaugherty/brick.git
synced 2024-09-11 15:55:54 +03:00
This experimental change makes it possible to: * Avoid runtime errors due to name typos * Achieve compile-time guarantees about name matching and usage * Force widget functions to be name-agnostic by being polymorphic in their name type * Clean up focus handling by making it possible to pattern-match on cursor location names The change also made many types more heavyweight and in some cases this is unpleasant when we don't want to have to care about names. But in those cases we can just use 'n' or '()' depending on how concrete we need to be. I'm not yet sure how this is going to play out in practice.
66 lines
1.5 KiB
Haskell
66 lines
1.5 KiB
Haskell
{-# LANGUAGE OverloadedStrings #-}
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
module Main where
|
|
|
|
import Control.Lens (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
|
|
)
|
|
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 -> V.Event -> EventM () (Next St)
|
|
appEvent st 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
|
|
|
|
initialState :: St
|
|
initialState =
|
|
St { _stExternalInput = ""
|
|
}
|
|
|
|
theApp :: App St V.Event ()
|
|
theApp =
|
|
App { appDraw = drawUI
|
|
, appChooseCursor = neverShowCursor
|
|
, appHandleEvent = appEvent
|
|
, appStartEvent = return
|
|
, appAttrMap = const def
|
|
, appLiftVtyEvent = id
|
|
}
|
|
|
|
main :: IO ()
|
|
main =
|
|
void $ defaultMain theApp initialState
|