highlighting for comments and changed file read, hGetContents did not work for me
This commit is contained in:
parent
8228e3cdaf
commit
a2fa7e906d
1 changed files with 19 additions and 8 deletions
|
@ -15,12 +15,13 @@ import qualified Brick.Widgets.Edit as EditWidget
|
|||
import qualified Graphics.Vty as Vty
|
||||
|
||||
---- Text ----
|
||||
import Data.Text (pack, singleton)
|
||||
import Data.Text.Markup (Markup, (@@))
|
||||
import Data.Text (unpack, pack, singleton)
|
||||
import Data.Text.Markup (Markup, (@@), toText)
|
||||
|
||||
---- Files ----
|
||||
import System.IO (IOMode (..), hClose, hGetContents,
|
||||
openFile)
|
||||
import System.Directory (doesFileExist)
|
||||
|
||||
---- Various ----
|
||||
import Control.Monad.IO.Class (liftIO)
|
||||
|
@ -50,8 +51,9 @@ makeLenses ''HeditState
|
|||
|
||||
runEditor :: FilePath -> IO ()
|
||||
runEditor f = do
|
||||
handle <- openFile f ReadWriteMode -- creates file if it does not exist
|
||||
content <- hGetContents handle
|
||||
-- gave me: *** Exception: Gui.hs: hGetContents: illegal operation (delayed read on closed handle)
|
||||
-- handle <- openFile f ReadWriteMode -- creates file if it does not exist
|
||||
-- content <- hGetContents handle
|
||||
{-
|
||||
reads the content before closing the handle (non-lazy)
|
||||
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)
|
||||
-> locks the file MultiReader/SingleWriter for free (i think)
|
||||
-}
|
||||
content `seq` hClose handle
|
||||
BrickMain.defaultMain theApp (initialState f content)
|
||||
-- content `seq` hClose handle
|
||||
|
||||
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 ()
|
||||
|
||||
theApp :: App HeditState Vty.Event Names
|
||||
|
@ -82,7 +88,7 @@ initialState f content = HS (EditWidget.editor Hedit drawContent Nothing content
|
|||
(f)
|
||||
|
||||
drawContent :: [String] -> Widget n
|
||||
drawContent = markup . (createMarkup []) . unlinesWA
|
||||
drawContent = markup . (createMarkup []) . unlines
|
||||
|
||||
drawUI :: HeditState -> [Widget Names]
|
||||
drawUI st = [EditWidget.renderEditor True (st^.hedit)]
|
||||
|
@ -129,14 +135,19 @@ mismatched :: (Char, Char) -> String -> Int -> Bool
|
|||
mismatched _ _ 0 = False
|
||||
mismatched _ [] _ = True
|
||||
mismatched b@(ob, cb) (c:cs) d
|
||||
-- TODO ignore comments and strings
|
||||
| c == ob = mismatched b cs $ d+1
|
||||
| c == cb = mismatched b cs $ d-1
|
||||
| otherwise = mismatched b cs d
|
||||
|
||||
createMarkup :: String -> String -> Markup Vty.Attr
|
||||
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
|
||||
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
|
||||
where i = elemIndex '"' ss
|
||||
createMarkup p (c:ss)
|
||||
|
|
Loading…
Reference in a new issue