Add Brick.Main.customMainWithDefaultVty (fixes #488)

This commit is contained in:
Jonathan Daugherty 2023-11-03 17:25:12 -07:00
parent 3852513d21
commit c1aef1106e
4 changed files with 25 additions and 12 deletions

View File

@ -453,7 +453,6 @@ executable brick-custom-event-demo
build-depends: base,
brick,
vty,
vty-crossplatform,
text,
microlens >= 0.3.0.0,
microlens-th,

View File

@ -12,13 +12,12 @@ import Control.Concurrent (threadDelay, forkIO)
import Data.Monoid
#endif
import qualified Graphics.Vty as V
import Graphics.Vty.CrossPlatform (mkVty)
import Brick.BChan
import Brick.Main
( App(..)
, showFirstCursor
, customMain
, customMainWithDefaultVty
, halt
)
import Brick.AttrMap
@ -83,6 +82,4 @@ main = do
writeBChan chan Counter
threadDelay 1000000
let buildVty = mkVty V.defaultConfig
initialVty <- buildVty
void $ customMain initialVty buildVty (Just chan) theApp initialState
void $ customMainWithDefaultVty (Just chan) theApp initialState

View File

@ -143,10 +143,9 @@ randomVal as = do
main :: IO ()
main = do
vty <- mkVty V.defaultConfig
chan <- newBChan 10
-- Run thread to simulate incoming data
void $ forkIO $ generateLines chan
void $ customMain vty (mkVty V.defaultConfig) (Just chan) app initialState
void $ customMainWithDefaultVty (Just chan) app initialState

View File

@ -5,6 +5,7 @@ module Brick.Main
, defaultMain
, customMain
, customMainWithVty
, customMainWithDefaultVty
, simpleMain
, resizeOrQuit
, simpleApp
@ -130,10 +131,8 @@ defaultMain :: (Ord n)
-> s
-- ^ The initial application state.
-> IO s
defaultMain app st = do
let builder = mkVty defaultConfig
initialVty <- builder
customMain initialVty builder Nothing app st
defaultMain app st =
fst <$> customMainWithDefaultVty Nothing app st
-- | A simple main entry point which takes a widget and renders it. This
-- event loop terminates when the user presses any key, but terminal
@ -235,6 +234,25 @@ customMain initialVty buildVty mUserChan app initialAppState = do
restoreInitialState
return s
-- | Like 'customMainWithVty', except that Vty is initialized with the
-- default configuration.
customMainWithDefaultVty :: (Ord n)
=> Maybe (BChan e)
-- ^ An event channel for sending custom
-- events to the event loop (you write to this
-- channel, the event loop reads from it).
-- Provide 'Nothing' if you don't plan on
-- sending custom events.
-> App s e n
-- ^ The application.
-> s
-- ^ The initial application state.
-> IO (s, Vty)
customMainWithDefaultVty mUserChan app initialAppState = do
let builder = mkVty defaultConfig
vty <- builder
customMainWithVty vty builder mUserChan app initialAppState
-- | Like 'customMain', except the last 'Vty' handle used by the
-- application is returned without being shut down with 'shutdown'. This
-- allows the caller to re-use the 'Vty' handle for something else, such