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 #-}
module Gui (runEditor) where
@ -17,6 +18,10 @@ import qualified Graphics.Vty as Vty
import Data.Text (pack, singleton)
import Data.Text.Markup (Markup, (@@))
---- Files ----
import System.IO (IOMode (..), hClose, hGetContents,
openFile)
---- Various ----
import Control.Monad.IO.Class (liftIO)
import Data.List
@ -45,8 +50,19 @@ makeLenses ''HeditState
runEditor :: FilePath -> IO ()
runEditor f = do
content <- readFile f
_ <- BrickMain.defaultMain theApp (initialState f content)
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"
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 ()
theApp :: App HeditState Vty.Event Names