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, build-depends: base,
brick, brick,
vty, vty,
vty-crossplatform,
text, text,
microlens >= 0.3.0.0, microlens >= 0.3.0.0,
microlens-th, microlens-th,

View File

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

View File

@ -143,10 +143,9 @@ randomVal as = do
main :: IO () main :: IO ()
main = do main = do
vty <- mkVty V.defaultConfig
chan <- newBChan 10 chan <- newBChan 10
-- Run thread to simulate incoming data -- Run thread to simulate incoming data
void $ forkIO $ generateLines chan 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 , defaultMain
, customMain , customMain
, customMainWithVty , customMainWithVty
, customMainWithDefaultVty
, simpleMain , simpleMain
, resizeOrQuit , resizeOrQuit
, simpleApp , simpleApp
@ -130,10 +131,8 @@ defaultMain :: (Ord n)
-> s -> s
-- ^ The initial application state. -- ^ The initial application state.
-> IO s -> IO s
defaultMain app st = do defaultMain app st =
let builder = mkVty defaultConfig fst <$> customMainWithDefaultVty Nothing app st
initialVty <- builder
customMain initialVty builder Nothing app st
-- | A simple main entry point which takes a widget and renders it. This -- | A simple main entry point which takes a widget and renders it. This
-- event loop terminates when the user presses any key, but terminal -- event loop terminates when the user presses any key, but terminal
@ -235,6 +234,25 @@ customMain initialVty buildVty mUserChan app initialAppState = do
restoreInitialState restoreInitialState
return s 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 -- | Like 'customMain', except the last 'Vty' handle used by the
-- application is returned without being shut down with 'shutdown'. This -- application is returned without being shut down with 'shutdown'. This
-- allows the caller to re-use the 'Vty' handle for something else, such -- allows the caller to re-use the 'Vty' handle for something else, such