brick/programs/EditDemo.hs

54 lines
1.3 KiB
Haskell
Raw Normal View History

2015-06-28 22:45:26 +03:00
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Data.Monoid
2015-07-10 23:12:12 +03:00
import qualified Graphics.Vty as V
2015-06-28 22:45:26 +03:00
2015-07-10 23:12:12 +03:00
import qualified Brick.Main as M
import qualified Brick.Types as T
2015-06-28 22:45:26 +03:00
import Brick.Widgets.Core
2015-07-10 23:12:12 +03:00
( Widget
, (<+>)
, hLimit
, str
)
import qualified Brick.Widgets.Center as C
import qualified Brick.Widgets.Edit as E
import qualified Brick.AttrMap as A
import Brick.Util (on)
drawUI :: E.Editor -> [Widget]
2015-06-28 23:24:36 +03:00
drawUI e = [ui]
2015-06-28 22:45:26 +03:00
where
2015-07-10 23:12:12 +03:00
ui = C.center $ "Input: " <+> (hLimit 30 $ E.renderEditor e)
2015-06-28 22:45:26 +03:00
2015-07-10 23:12:12 +03:00
appEvent :: E.Editor -> V.Event -> M.EventM (M.Next E.Editor)
appEvent e ev =
2015-06-28 23:24:36 +03:00
case ev of
2015-07-10 23:12:12 +03:00
V.EvKey V.KEsc [] -> M.halt e
V.EvKey V.KEnter [] -> M.halt e
_ -> M.continue $ T.handleEvent ev e
2015-06-28 22:45:26 +03:00
2015-07-10 23:12:12 +03:00
initialState :: E.Editor
initialState = E.editor (T.Name "edit") str ""
2015-06-28 22:45:26 +03:00
2015-07-10 23:12:12 +03:00
theMap :: A.AttrMap
theMap = A.attrMap V.defAttr
[ (E.editAttr, V.white `on` V.blue)
2015-06-28 22:45:26 +03:00
]
2015-07-10 23:12:12 +03:00
theApp :: M.App E.Editor V.Event
2015-06-28 22:45:26 +03:00
theApp =
2015-07-10 23:12:12 +03:00
M.App { M.appDraw = drawUI
, M.appChooseCursor = M.showFirstCursor
, M.appHandleEvent = appEvent
, M.appStartEvent = return
, M.appAttrMap = const theMap
, M.appLiftVtyEvent = id
2015-07-10 23:12:12 +03:00
}
2015-06-28 22:45:26 +03:00
main :: IO ()
main = do
2015-07-10 23:12:12 +03:00
e <- M.defaultMain theApp initialState
putStrLn $ "You entered: " <> (E.getEditContents e)