From 4472025157b62011f2cdee40f3309249ecb1ee70 Mon Sep 17 00:00:00 2001 From: Johannes Winklehner Date: Wed, 27 Jul 2016 17:34:48 +0200 Subject: [PATCH] added highlighting for string literals --- editor.hs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/editor.hs b/editor.hs index 9535b72..5ed96d6 100644 --- a/editor.hs +++ b/editor.hs @@ -1,4 +1,10 @@ import Brick +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 @@ -6,7 +12,18 @@ data Name = Text deriving (Ord, Show, Eq) type St = E.Editor Name initialState :: St -initialState = E.editor Text (str . unlines) Nothing "" +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]