Created
March 19, 2024 19:38
-
-
Save franklindyer/448466365e59d925042b55ddeeeb870b to your computer and use it in GitHub Desktop.
Haskell-brick toggling example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Lib | |
( someFunc | |
) where | |
import Brick | |
import Brick (Widget, simpleMain, (<+>), str, withBorderStyle, joinBorders) | |
import Brick.Widgets.Center (center) | |
import Brick.Widgets.Border (borderWithLabel, vBorder) | |
import Brick.Widgets.Border.Style (unicode) | |
import Control.Monad (void, forever) | |
import Control.Monad.State.Strict | |
import qualified Graphics.Vty as V | |
-- This is an "empty" type. | |
-- We're not using any custom events in our app, so this will be the type we use for events. | |
data Void | |
-- The type of state used for this demonstration is Bool. | |
-- This draws a pane that is either filled with 'x' or '.', depending on whether the state is True or False. | |
drawPane :: Bool -> Widget () | |
drawPane b = if b then fill 'x' else fill '.' | |
-- This is the main drawing function for our demo. | |
-- It draws two panes, side-by-side. | |
-- The state determines which of the two panes is filled by 'x's and which is filled by '.'s. | |
drawUI :: Bool -> [Widget ()] | |
drawUI b = [drawPane b <+> drawPane (not b)] | |
-- The event handler for our app. | |
-- When the escape key is pressed, the application halts. | |
-- When any other key is pressed, the state is toggled. | |
appEvent :: BrickEvent () Void -> EventM () Bool () | |
appEvent e = | |
case e of | |
VtyEvent (V.EvKey V.KEsc []) -> halt -- halt when Esc is pressed | |
VtyEvent _ -> state (\b -> ((), not b)) -- toggle the state when anything else is pressed | |
toggleApp :: App Bool Void () | |
toggleApp = | |
App { | |
appDraw = drawUI, | |
appChooseCursor = showFirstCursor, | |
appHandleEvent = appEvent, | |
appStartEvent = return (), | |
appAttrMap = const $ attrMap V.defAttr [] | |
} | |
main :: IO () | |
main = void $ customMainWithDefaultVty Nothing toggleApp True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment