File: ThemeDemo.hs

package info (click to toggle)
haskell-brick 2.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,328 kB
  • sloc: haskell: 8,492; makefile: 5
file content (86 lines) | stat: -rw-r--r-- 2,188 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
module Main where

import Control.Monad (void)
import Control.Monad.State (put)
import Graphics.Vty
  ( white, blue, green, yellow, black, magenta
  , Event(EvKey)
  , Key(KChar, KEsc)
  )

import Brick.Main
import Brick.Themes
  ( Theme
  , newTheme
  , themeToAttrMap
  )
import Brick.Types
  ( Widget
  , BrickEvent(VtyEvent)
  , EventM
  )
import Brick.Widgets.Center
  ( hCenter
  , center
  )
import Brick.Widgets.Core
  ( (<+>)
  , vBox
  , str
  , hLimit
  , withDefAttr
  )
import Brick.Util (on, fg)
import Brick.AttrMap (AttrName, attrName)

ui :: Widget n
ui =
    center $
    hLimit 40 $
    vBox $ hCenter <$>
         [ str "Press " <+> (withDefAttr keybindingAttr $ str "1") <+> str " to switch to theme 1."
         , str "Press " <+> (withDefAttr keybindingAttr $ str "2") <+> str " to switch to theme 2."
         ]

keybindingAttr :: AttrName
keybindingAttr = attrName "keybinding"

theme1 :: Theme
theme1 =
    newTheme (white `on` blue)
             [ (keybindingAttr, fg magenta)
             ]

theme2 :: Theme
theme2 =
    newTheme (green `on` black)
             [ (keybindingAttr, fg yellow)
             ]

appEvent :: BrickEvent () e -> EventM () Int ()
appEvent (VtyEvent (EvKey (KChar '1') [])) = put 1
appEvent (VtyEvent (EvKey (KChar '2') [])) = put 2
appEvent (VtyEvent (EvKey (KChar 'q') [])) = halt
appEvent (VtyEvent (EvKey KEsc [])) = halt
appEvent _ = return ()

app :: App Int e ()
app =
    App { appDraw = const [ui]
        , appHandleEvent = appEvent
        , appStartEvent = return ()
        , appAttrMap = \s ->
            -- Note that in practice this is not ideal: we don't want
            -- to build an attribute map from a theme every time this is
            -- invoked, because it gets invoked once per redraw. Instead
            -- we'd build the attribute map at startup and store it in
            -- the application state. Here I just use themeToAttrMap to
            -- show the mechanics of the API.
            themeToAttrMap $ if s == 1
                             then theme1
                             else theme2
        , appChooseCursor = neverShowCursor
        }

main :: IO ()
main = void $ defaultMain app 1