hsedit/editor.hs

53 lines
1.5 KiB
Haskell
Raw Normal View History

import Brick
2016-07-27 15:34:48 +00:00
import Brick.Markup
import Data.Maybe
import Data.Monoid
import Data.List
import Data.Text (pack, singleton)
import Data.Text.Markup ((@@))
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
2016-07-27 15:34:48 +00:00
initialState = E.editor Text drawContent Nothing ""
drawContent :: [String] -> Widget n
drawContent = markup . createMarkup . unlines
createMarkup :: String -> Markup V.Attr
createMarkup [] = mempty
createMarkup s@('"':ss) = case i of
Just n -> (pack (take (2+n) s) @@ fg V.blue) <> (createMarkup (drop (1+n) ss))
Nothing -> pack s @@ fg V.red
where i = elemIndex '"' ss
createMarkup (c:ss) = (singleton c @@ fg V.white) <> (createMarkup ss)
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