From c1aef1106eb537d94041ef502174ba0dc97887c0 Mon Sep 17 00:00:00 2001 From: Jonathan Daugherty Date: Fri, 3 Nov 2023 17:25:12 -0700 Subject: [PATCH] Add Brick.Main.customMainWithDefaultVty (fixes #488) --- brick.cabal | 1 - programs/CustomEventDemo.hs | 7 ++----- programs/TailDemo.hs | 3 +-- src/Brick/Main.hs | 26 ++++++++++++++++++++++---- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/brick.cabal b/brick.cabal index 17c9c0f..29a7767 100644 --- a/brick.cabal +++ b/brick.cabal @@ -453,7 +453,6 @@ executable brick-custom-event-demo build-depends: base, brick, vty, - vty-crossplatform, text, microlens >= 0.3.0.0, microlens-th, diff --git a/programs/CustomEventDemo.hs b/programs/CustomEventDemo.hs index 335d082..4693047 100644 --- a/programs/CustomEventDemo.hs +++ b/programs/CustomEventDemo.hs @@ -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 diff --git a/programs/TailDemo.hs b/programs/TailDemo.hs index 7bd3a28..1cd0b18 100644 --- a/programs/TailDemo.hs +++ b/programs/TailDemo.hs @@ -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 diff --git a/src/Brick/Main.hs b/src/Brick/Main.hs index 7f69e51..98fa33a 100644 --- a/src/Brick/Main.hs +++ b/src/Brick/Main.hs @@ -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