Last active
November 6, 2017 18:42
-
-
Save andrevdm/6bf3c7e1ca16877946fde719ef43973a to your computer and use it in GitHub Desktop.
Brick: simple editor demo
This file contains hidden or 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
| {-# LANGUAGE NoImplicitPrelude #-} | |
| {-# LANGUAGE OverloadedStrings #-} | |
| module Main where | |
| import Protolude | |
| import Prelude (unlines) | |
| import Brick ((<+>), (<=>)) | |
| import qualified Brick as B | |
| import qualified Brick.Focus as BF | |
| import qualified Brick.Widgets.Edit as BE | |
| import qualified Brick.Widgets.Border as BB | |
| import qualified Brick.Widgets.Border.Style as BBS | |
| import qualified Graphics.Vty as V | |
| main :: IO () | |
| main = do | |
| let g = St 0 (BF.focusRing [Edit1]) "" (BE.editor Edit1 Nothing "init") | |
| void $ B.customMain (V.mkVty V.defaultConfig) Nothing app g | |
| data Name = ViewPort1 | |
| | ViewPort2 | |
| | Edit1 | |
| deriving (Eq, Ord, Show) | |
| data Tick = Tick | |
| data St = St { gmScore :: Int | |
| , gmRing :: BF.FocusRing Name | |
| , gmText :: Text | |
| , gmEditor1 :: BE.Editor [Char] Name | |
| } | |
| app :: B.App St Tick Name | |
| app = B.App { B.appDraw = drawUI | |
| , B.appChooseCursor = B.showFirstCursor | |
| , B.appHandleEvent = handleEvent | |
| , B.appStartEvent = pure | |
| , B.appAttrMap = const theMap | |
| } | |
| handleEvent :: St -> B.BrickEvent Name Tick -> B.EventM Name (B.Next St) | |
| handleEvent (St score r t e) (B.AppEvent Tick) = B.continue $ St (score + 1) r t e | |
| handleEvent s (B.VtyEvent (V.EvKey V.KEsc [])) = B.halt s | |
| handleEvent s (B.VtyEvent e) = do | |
| r <- BE.handleEditorEvent e (gmEditor1 s) | |
| B.continue $ s { gmEditor1 = r } | |
| handleEvent s _ = B.continue s | |
| drawUI :: St -> [B.Widget Name] | |
| drawUI g = | |
| [ ( B.hLimit 50 $ | |
| B.withBorderStyle BBS.unicodeRounded $ | |
| BB.borderWithLabel (B.str "menu") $ | |
| B.padAll 1 $ | |
| B.viewport ViewPort1 B.Vertical $ | |
| B.str "1" | |
| ) | |
| <+> | |
| ( -- B.padLeft (B.Pad 2) $ | |
| B.withBorderStyle BBS.unicodeRounded $ | |
| BB.borderWithLabel (B.str "data") $ | |
| B.padAll 1 $ | |
| B.viewport ViewPort2 B.Vertical ( ( B.str "2" ) | |
| <=> | |
| ( B.vLimit 5 $ | |
| B.withBorderStyle BBS.unicode $ | |
| BB.border $ | |
| BF.withFocusRing (gmRing g) (BE.renderEditor (B.str . unlines)) (gmEditor1 g) | |
| ) | |
| ) | |
| ) | |
| ] | |
| theMap :: B.AttrMap | |
| theMap = B.attrMap V.defAttr | |
| [ (BE.editAttr, V.white `B.on` V.blue) | |
| , (BE.editFocusedAttr, V.black `B.on` V.yellow) | |
| ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment