brick/programs/SuspendAndResumeDemo.hs

72 lines
1.5 KiB
Haskell
Raw Permalink Normal View History

2015-06-29 09:45:04 +03:00
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE CPP #-}
2015-06-29 09:45:04 +03:00
module Main where
import Lens.Micro ((^.))
import Lens.Micro.TH (makeLenses)
2015-06-29 09:45:04 +03:00
import Control.Monad (void)
#if !(MIN_VERSION_base(4,11,0))
2015-06-29 09:45:04 +03:00
import Data.Monoid
#endif
import qualified Graphics.Vty as V
2015-06-29 09:45:04 +03:00
import Brick.Main
( App(..), neverShowCursor, defaultMain
, suspendAndResume, halt
)
2017-01-25 00:48:45 +03:00
import Brick.AttrMap
( attrMap
)
import Brick.Types
( Widget
, EventM
, BrickEvent(..)
)
import Brick.Widgets.Core
( vBox
, str
)
2015-06-29 09:45:04 +03:00
data St =
St { _stExternalInput :: String
}
makeLenses ''St
drawUI :: St -> [Widget ()]
2015-06-29 09:45:04 +03:00
drawUI st = [ui]
where
ui = vBox [ str $ "External input: \"" <> st^.stExternalInput <> "\""
, str "(Press Esc to quit or Space to ask for input)"
2015-06-29 09:45:04 +03:00
]
appEvent :: BrickEvent () e -> EventM () St ()
appEvent (VtyEvent e) =
2015-06-29 09:45:04 +03:00
case e of
V.EvKey V.KEsc [] -> halt
V.EvKey (V.KChar ' ') [] -> suspendAndResume $ do
2015-06-29 09:45:04 +03:00
putStrLn "Suspended. Please enter something and press enter to resume:"
s <- getLine
return $ St { _stExternalInput = s }
_ -> return ()
appEvent _ = return ()
2015-06-29 09:45:04 +03:00
initialState :: St
initialState =
St { _stExternalInput = ""
}
theApp :: App St e ()
2015-06-29 09:45:04 +03:00
theApp =
App { appDraw = drawUI
, appChooseCursor = neverShowCursor
, appHandleEvent = appEvent
, appStartEvent = return ()
2017-01-25 00:48:45 +03:00
, appAttrMap = const $ attrMap V.defAttr []
2015-06-29 09:45:04 +03:00
}
main :: IO ()
main =
2015-06-29 09:45:04 +03:00
void $ defaultMain theApp initialState