load file with cmd argument and save with ctrl-s
This commit is contained in:
parent
80b83098cb
commit
b52d5c237f
1 changed files with 22 additions and 10 deletions
32
editor.hs
32
editor.hs
|
@ -4,9 +4,14 @@ import Data.Maybe
|
||||||
import Data.Monoid
|
import Data.Monoid
|
||||||
import Data.List
|
import Data.List
|
||||||
import Data.Tuple
|
import Data.Tuple
|
||||||
|
import Data.Default
|
||||||
import Data.Text (pack, singleton, unpack)
|
import Data.Text (pack, singleton, unpack)
|
||||||
import Data.Text.Markup
|
import Data.Text.Markup
|
||||||
import Control.Monad
|
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 Brick.Widgets.Edit as E
|
||||||
import qualified Graphics.Vty as V
|
import qualified Graphics.Vty as V
|
||||||
|
|
||||||
|
@ -18,8 +23,8 @@ type St = E.Editor Name
|
||||||
|
|
||||||
brackets = [('(',')'),('[',']'),('{','}')]
|
brackets = [('(',')'),('[',']'),('{','}')]
|
||||||
|
|
||||||
initialState :: St
|
initialState :: String -> St
|
||||||
initialState = E.editor Text drawContent Nothing ""
|
initialState = E.editor Text drawContent Nothing
|
||||||
|
|
||||||
drawContent :: [String] -> Widget n
|
drawContent :: [String] -> Widget n
|
||||||
drawContent = markup . (createMarkup []) . unlinesWA
|
drawContent = markup . (createMarkup []) . unlinesWA
|
||||||
|
@ -57,22 +62,29 @@ drawUI st = [E.renderEditor True st]
|
||||||
appCursor :: St -> [CursorLocation Name] -> Maybe (CursorLocation Name)
|
appCursor :: St -> [CursorLocation Name] -> Maybe (CursorLocation Name)
|
||||||
appCursor st c = Just (head c)
|
appCursor st c = Just (head c)
|
||||||
|
|
||||||
appEvent :: St -> V.Event -> EventM Name (Next St)
|
appEvent :: String -> St -> V.Event -> EventM Name (Next St)
|
||||||
appEvent st ev =
|
appEvent s st ev =
|
||||||
case ev of
|
case ev of
|
||||||
V.EvKey V.KEsc [] -> halt st
|
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
|
_ -> continue =<< E.handleEditorEvent ev st
|
||||||
|
|
||||||
theApp :: App St V.Event Name
|
theApp :: String -> App St V.Event Name
|
||||||
theApp =
|
theApp s =
|
||||||
App { appDraw = drawUI
|
App { appDraw = drawUI
|
||||||
, appChooseCursor = appCursor
|
, appChooseCursor = showFirstCursor
|
||||||
, appHandleEvent = appEvent
|
, appHandleEvent = appEvent s
|
||||||
, appStartEvent = return
|
, appStartEvent = return
|
||||||
, appAttrMap = const (attrMap V.defAttr [ ])
|
, appAttrMap = const (attrMap V.defAttr [ ])
|
||||||
, appLiftVtyEvent = id
|
, appLiftVtyEvent = id
|
||||||
}
|
}
|
||||||
|
|
||||||
main = do
|
main = do
|
||||||
st <- defaultMain theApp initialState
|
f <- do
|
||||||
putStrLn $ unlines $ E.getEditContents $ st
|
a <- getArgs
|
||||||
|
return $ head a
|
||||||
|
c <- readFile f
|
||||||
|
st <- defaultMain (theApp f) (initialState c)
|
||||||
|
void $ return st
|
||||||
|
-- putStrLn $ unlines $ E.getEditContents $ st
|
||||||
|
|
Loading…
Reference in a new issue