create file if does not exist

This commit is contained in:
Armin Friedl 2016-08-07 05:43:38 +02:00
parent 8a676d6b4f
commit 419cad6cab

View file

@ -1,3 +1,4 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TemplateHaskell #-}
module Gui (runEditor) where module Gui (runEditor) where
@ -17,6 +18,10 @@ import qualified Graphics.Vty as Vty
import Data.Text (pack, singleton) import Data.Text (pack, singleton)
import Data.Text.Markup (Markup, (@@)) import Data.Text.Markup (Markup, (@@))
---- Files ----
import System.IO (IOMode (..), hClose, hGetContents,
openFile)
---- Various ---- ---- Various ----
import Control.Monad.IO.Class (liftIO) import Control.Monad.IO.Class (liftIO)
import Data.List import Data.List
@ -45,8 +50,19 @@ makeLenses ''HeditState
runEditor :: FilePath -> IO () runEditor :: FilePath -> IO ()
runEditor f = do runEditor f = do
content <- readFile f handle <- openFile f ReadWriteMode -- creates file if it does not exist
_ <- BrickMain.defaultMain theApp (initialState f content) content <- hGetContents handle
{-
reads the content before closing the handle (non-lazy)
this is the version with the fewest "dependecies"
other options:
- ByteString.hGetContents: strict, fast
-> ByteString.Lazy for lazy, fast
- Text.IO.hGetContents: strict, needs 2x file size while reading in
-> filesize == 2xRAM = bad
-}
content `seq` hClose handle
BrickMain.defaultMain theApp (initialState f content)
return () return ()
theApp :: App HeditState Vty.Event Names theApp :: App HeditState Vty.Event Names