load file with cmd argument and save with ctrl-s

This commit is contained in:
Johannes Winklehner 2016-08-05 18:48:47 +02:00
parent 80b83098cb
commit b52d5c237f

View file

@ -4,9 +4,14 @@ import Data.Maybe
import Data.Monoid
import Data.List
import Data.Tuple
import Data.Default
import Data.Text (pack, singleton, unpack)
import Data.Text.Markup
import Control.Monad
import Control.Monad.IO.Class
import Control.Concurrent
import Control.DeepSeq
import System.Environment
import qualified Brick.Widgets.Edit as E
import qualified Graphics.Vty as V
@ -18,8 +23,8 @@ type St = E.Editor Name
brackets = [('(',')'),('[',']'),('{','}')]
initialState :: St
initialState = E.editor Text drawContent Nothing ""
initialState :: String -> St
initialState = E.editor Text drawContent Nothing
drawContent :: [String] -> Widget n
drawContent = markup . (createMarkup []) . unlinesWA
@ -57,22 +62,29 @@ drawUI st = [E.renderEditor True st]
appCursor :: St -> [CursorLocation Name] -> Maybe (CursorLocation Name)
appCursor st c = Just (head c)
appEvent :: St -> V.Event -> EventM Name (Next St)
appEvent st ev =
appEvent :: String -> St -> V.Event -> EventM Name (Next St)
appEvent s st ev =
case ev of
V.EvKey V.KEsc [] -> halt st
V.EvKey (V.KChar 'c') [V.MCtrl] -> halt st
V.EvKey (V.KChar 's') [V.MCtrl] -> liftIO (writeFile s (unlines $ E.getEditContents $ st)) >> continue st
_ -> continue =<< E.handleEditorEvent ev st
theApp :: App St V.Event Name
theApp =
theApp :: String -> App St V.Event Name
theApp s =
App { appDraw = drawUI
, appChooseCursor = appCursor
, appHandleEvent = appEvent
, appChooseCursor = showFirstCursor
, appHandleEvent = appEvent s
, appStartEvent = return
, appAttrMap = const (attrMap V.defAttr [ ])
, appLiftVtyEvent = id
}
main = do
st <- defaultMain theApp initialState
putStrLn $ unlines $ E.getEditContents $ st
f <- do
a <- getArgs
return $ head a
c <- readFile f
st <- defaultMain (theApp f) (initialState c)
void $ return st
-- putStrLn $ unlines $ E.getEditContents $ st