From 9fed792c40b215639cdcd121cdf58d75f122ac2a Mon Sep 17 00:00:00 2001 From: Johannes Winklehner Date: Thu, 21 Jul 2016 13:23:49 +0200 Subject: [PATCH] simple text editor using the bricks library --- editor.hs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 editor.hs diff --git a/editor.hs b/editor.hs new file mode 100644 index 0000000..9535b72 --- /dev/null +++ b/editor.hs @@ -0,0 +1,35 @@ +import Brick +import qualified Brick.Widgets.Edit as E +import qualified Graphics.Vty as V + +data Name = Text deriving (Ord, Show, Eq) +type St = E.Editor Name + +initialState :: St +initialState = E.editor Text (str . unlines) Nothing "" + +drawUI :: St -> [Widget Name] +drawUI st = [E.renderEditor True st] + +appCursor :: St -> [CursorLocation Name] -> Maybe (CursorLocation Name) +appCursor st c = Just (head c) + +appEvent :: St -> V.Event -> EventM Name (Next St) +appEvent st ev = + case ev of + V.EvKey V.KEsc [] -> halt st + _ -> continue =<< E.handleEditorEvent ev st + +theApp :: App St V.Event Name +theApp = + App { appDraw = drawUI + , appChooseCursor = appCursor + , appHandleEvent = appEvent + , appStartEvent = return + , appAttrMap = const (attrMap V.defAttr [ ]) + , appLiftVtyEvent = id + } + +main = do + st <- defaultMain theApp initialState + putStrLn $ unlines $ E.getEditContents $ st