vty/ModeDemo.hs
Jonathan Daugherty d4789578ed Make mouse and paste modes optional and reconfigurable
- Adds mouseMode and bracketedPasteMode fields to Config
- Config fields default to False
- Adds a Mode type for modal terminal features (mouse events, bracketed
  paste mode)
- The Output interface now has new fields:
  * supportsMode :: Mode -> Bool tells whether the device supports a
    mode
  * setMode :: Mode -> Bool -> IO () turns a mode on or off
  * getModeStatus :: Mode -> IO Bool tells you whether a mode is on or
    off
2016-06-26 12:14:36 -07:00

53 lines
1.8 KiB
Haskell

module Main where
import Data.Monoid
import Graphics.Vty
mkUI :: (Bool, Bool, Bool, Bool) -> Maybe Event -> Image
mkUI (m, ms, p, ps) e =
vertCat [ string defAttr $ "Mouse mode supported: " <> show m
, string defAttr $ "Mouse mode status: " <> show ms
, string defAttr " "
, string defAttr $ "Paste mode supported: " <> show p
, string defAttr $ "Paste mode status: " <> show ps
, string defAttr " "
, string defAttr $ "Last event: " <> show e
, string defAttr " "
, string defAttr "Press 'm' to toggle mouse mode, 'p' to toggle paste mode, and 'q' to quit."
]
main :: IO ()
main = do
cfg <- standardIOConfig
vty <- mkVty cfg
let renderUI lastE = do
let output = outputIface vty
info <- (,,,) <$> (pure $ supportsMode output Mouse)
<*> getModeStatus output Mouse
<*> (pure $ supportsMode output BracketedPaste)
<*> getModeStatus output BracketedPaste
return $ picForImage $ mkUI info lastE
let go lastE = do
pic <- renderUI lastE
update vty pic
e <- nextEvent vty
case e of
EvKey (KChar 'q') [] -> return ()
EvKey (KChar 'm') [] -> do
let output = outputIface vty
enabled <- getModeStatus output Mouse
setMode output Mouse (not enabled)
go (Just e)
EvKey (KChar 'p') [] -> do
let output = outputIface vty
enabled <- getModeStatus output BracketedPaste
setMode output BracketedPaste (not enabled)
go (Just e)
_ -> go (Just e)
go Nothing
shutdown vty