highlighting for comments and changed file read, hGetContents did not work for me

This commit is contained in:
Johannes Winklehner 2016-08-07 17:30:44 +02:00
parent 8228e3cdaf
commit a2fa7e906d

View file

@ -15,12 +15,13 @@ import qualified Brick.Widgets.Edit as EditWidget
import qualified Graphics.Vty as Vty import qualified Graphics.Vty as Vty
---- Text ---- ---- Text ----
import Data.Text (pack, singleton) import Data.Text (unpack, pack, singleton)
import Data.Text.Markup (Markup, (@@)) import Data.Text.Markup (Markup, (@@), toText)
---- Files ---- ---- Files ----
import System.IO (IOMode (..), hClose, hGetContents, import System.IO (IOMode (..), hClose, hGetContents,
openFile) openFile)
import System.Directory (doesFileExist)
---- Various ---- ---- Various ----
import Control.Monad.IO.Class (liftIO) import Control.Monad.IO.Class (liftIO)
@ -50,8 +51,9 @@ makeLenses ''HeditState
runEditor :: FilePath -> IO () runEditor :: FilePath -> IO ()
runEditor f = do runEditor f = do
handle <- openFile f ReadWriteMode -- creates file if it does not exist -- gave me: *** Exception: Gui.hs: hGetContents: illegal operation (delayed read on closed handle)
content <- hGetContents handle -- handle <- openFile f ReadWriteMode -- creates file if it does not exist
-- content <- hGetContents handle
{- {-
reads the content before closing the handle (non-lazy) reads the content before closing the handle (non-lazy)
this is the version with the fewest "dependecies" this is the version with the fewest "dependecies"
@ -64,8 +66,12 @@ runEditor f = do
-> works with all above options (incl. the lazy variants, i think) -> works with all above options (incl. the lazy variants, i think)
-> locks the file MultiReader/SingleWriter for free (i think) -> locks the file MultiReader/SingleWriter for free (i think)
-} -}
content `seq` hClose handle -- content `seq` hClose handle
BrickMain.defaultMain theApp (initialState f content)
exists <- doesFileExist f
content <- if exists then readFile f else return []
st <- BrickMain.defaultMain theApp (initialState f content)
-- putStr $ unpack $ toText $ createMarkup [] $ unlines $ EditWidget.getEditContents $ st^.hedit
return () return ()
theApp :: App HeditState Vty.Event Names theApp :: App HeditState Vty.Event Names
@ -82,7 +88,7 @@ initialState f content = HS (EditWidget.editor Hedit drawContent Nothing content
(f) (f)
drawContent :: [String] -> Widget n drawContent :: [String] -> Widget n
drawContent = markup . (createMarkup []) . unlinesWA drawContent = markup . (createMarkup []) . unlines
drawUI :: HeditState -> [Widget Names] drawUI :: HeditState -> [Widget Names]
drawUI st = [EditWidget.renderEditor True (st^.hedit)] drawUI st = [EditWidget.renderEditor True (st^.hedit)]
@ -129,14 +135,19 @@ mismatched :: (Char, Char) -> String -> Int -> Bool
mismatched _ _ 0 = False mismatched _ _ 0 = False
mismatched _ [] _ = True mismatched _ [] _ = True
mismatched b@(ob, cb) (c:cs) d mismatched b@(ob, cb) (c:cs) d
-- TODO ignore comments and strings
| c == ob = mismatched b cs $ d+1 | c == ob = mismatched b cs $ d+1
| c == cb = mismatched b cs $ d-1 | c == cb = mismatched b cs $ d-1
| otherwise = mismatched b cs d | otherwise = mismatched b cs d
createMarkup :: String -> String -> Markup Vty.Attr createMarkup :: String -> String -> Markup Vty.Attr
createMarkup _ [] = mempty createMarkup _ [] = mempty
createMarkup p s@('%':ss) = case i of
Just n -> (pack (take (2+n) s) @@ fg Vty.green) <> (createMarkup (reverse (take (2+n) s) ++ p) (drop (1+n) ss))
Nothing -> pack s @@ fg Vty.green
where i = elemIndex '\n' ss
createMarkup p s@('"':ss) = case i of createMarkup p s@('"':ss) = case i of
Just n -> (pack (take (2+n) s) @@ fg Vty.blue) <> (createMarkup (reverse (take (2+n) s) ++ s) (drop (1+n) ss)) Just n -> (pack (take (2+n) s) @@ fg Vty.blue) <> (createMarkup (reverse (take (2+n) s) ++ p) (drop (1+n) ss))
Nothing -> pack s @@ fg Vty.red Nothing -> pack s @@ fg Vty.red
where i = elemIndex '"' ss where i = elemIndex '"' ss
createMarkup p (c:ss) createMarkup p (c:ss)