From 5f508d7a271d8994179c8bb5579bab8387030f1c Mon Sep 17 00:00:00 2001 From: Armin Friedl Date: Sun, 7 Aug 2016 16:07:57 +0200 Subject: [PATCH 1/2] start fontifier --- src/App/Fontifier.hs | 40 ++++++++++++++++++++++++++++++++++++++++ src/App/Gui.hs | 25 +++++++++++++------------ 2 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 src/App/Fontifier.hs diff --git a/src/App/Fontifier.hs b/src/App/Fontifier.hs new file mode 100644 index 0000000..aa64563 --- /dev/null +++ b/src/App/Fontifier.hs @@ -0,0 +1,40 @@ +{-# LANGUAGE TemplateHaskell #-} -- needed for lenses + +module Fontifier (fontMap, fontify) where + +{---- Imports ----} + +---- Brick, for markup specifications ---- +import Brick.AttrMap (AttrMap, attrMap, attrName) +import Brick.Util (bg, fg) +import qualified Graphics.Vty as Vty + +---- Various ---- +import Lens.Micro +import Lens.Micro.TH (makeLenses) + + +{---- Type Definitions ----} +-- defines an attribute for a text slice +data Fontification = FT { _ftStart :: Integer -- start index in string + , _ftEnd :: Integer -- end index in string + , _ftAttr :: String -- attribute name + } +makeLenses ''Fontification + +{---- Functions ----} + +fontMap :: AttrMap +fontMap = attrMap Vty.defAttr + [ + (attrName "assignment" , fg Vty.blue), + (attrName "return" , fg Vty.green), + (attrName "guard" , fg Vty.yellow), + (attrName "err_braces" , fg Vty.red), -- unbalanced braces + (attrName "err_string" , fg Vty.red) -- unterminated strings + ] + +-- calls the fontification functions +-- and returns a list of attributes for text slices +fontify :: String -> [Fontification] +fontify s = [] diff --git a/src/App/Gui.hs b/src/App/Gui.hs index 9093603..4e7bc43 100644 --- a/src/App/Gui.hs +++ b/src/App/Gui.hs @@ -1,5 +1,4 @@ -{-# LANGUAGE OverloadedStrings #-} -{-# LANGUAGE TemplateHaskell #-} +{-# LANGUAGE TemplateHaskell #-} -- needed for lenses module Gui (runEditor) where @@ -22,6 +21,9 @@ import Data.Text.Markup (Markup, (@@)) import System.IO (IOMode (..), hClose, hGetContents, openFile) +---- Markup ---- +import qualified Fontifier as Fontifier + ---- Various ---- import Control.Monad.IO.Class (liftIO) import Data.List @@ -45,7 +47,6 @@ makeLenses ''HeditState - {---- Functions ----} runEditor :: FilePath -> IO () @@ -73,7 +74,7 @@ theApp = App { appDraw = drawUI , appChooseCursor = showFirstCursor , appHandleEvent = appEvent , appStartEvent = return - , appAttrMap = const (attrMap Vty.defAttr [ ]) + , appAttrMap = const $ Fontifier.fontMap , appLiftVtyEvent = id } @@ -109,14 +110,14 @@ appEvent st ev --- TODO Refactor parts into SyntaxChecker.hs somehow --- Rough idea: --- 1) make some monad that carries kind of markup-info --- state from one checker function to the next --- 2) call checker functions in editor --- 3) get (combined) markup-info from checkers --- 4) apply markup-info in editor function - +-- TODO Refactor +-- 1) Forget the monad thing that used to stand here, no need to +-- introduce state +-- 2) Rough idea: +-- -> call Fontifier.fontify (public interface to whatever +-- the fontification functions do) +-- -> get list of attributes for text slices +-- -> apply attributes to text in editor gui brackets :: [(Char,Char)] brackets = [('(',')'),('[',']'),('{','}')] From 95bf8f385380a34750f3d5aecf182a745a35d840 Mon Sep 17 00:00:00 2001 From: Armin Friedl Date: Sun, 7 Aug 2016 16:26:54 +0200 Subject: [PATCH 2/2] exit with msg if no file given --- src/Main.hs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Main.hs b/src/Main.hs index 924e88b..881fe5d 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -1,8 +1,16 @@ module Main where +import Control.Monad (when) import Gui (runEditor) import System.Environment (getArgs) main = do args <- getArgs + when (null args) $ error (msgWithUsage "Please specify a file") runEditor $ head args + +msgWithUsage :: String -> String +msgWithUsage = \x -> x ++ "\n" ++ usage + +usage :: String +usage = "Usage: hedit "