From 419cad6cab12c6fefb0a07ed70e250d061d60635 Mon Sep 17 00:00:00 2001 From: Armin Friedl Date: Sun, 7 Aug 2016 05:43:38 +0200 Subject: [PATCH] create file if does not exist --- src/App/Gui.hs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/App/Gui.hs b/src/App/Gui.hs index bd5e598..56468df 100644 --- a/src/App/Gui.hs +++ b/src/App/Gui.hs @@ -1,4 +1,5 @@ -{-# LANGUAGE TemplateHaskell #-} +{-# 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