vty/Demo.hs

81 lines
3.0 KiB
Haskell
Raw Normal View History

2014-05-04 03:33:56 +04:00
module Main where
import Graphics.Vty
2014-06-05 10:57:32 +04:00
import Control.Applicative hiding ((<|>))
2014-05-04 03:33:56 +04:00
import Control.Arrow
import Control.Monad.RWS
import Data.Default (def)
import Data.Sequence (Seq, (<|) )
import qualified Data.Sequence as Seq
import Data.Foldable
eventBufferSize = 1000
type App = RWST Vty () (Seq String) IO
main = do
vty <- mkVty def
_ <- execRWST (vtyInteract False) vty Seq.empty
shutdown vty
vtyInteract :: Bool -> App ()
vtyInteract shouldExit = do
updateDisplay
unless shouldExit $ handleNextEvent >>= vtyInteract
2014-06-05 10:57:32 +04:00
introText = vertCat $ map (string defAttr)
[ "this line is hidden by the top layer"
, "The vty demo program will echo the events generated by the pressed keys."
, "Below there is a 240 color box."
, "Followed by a description of the 16 color pallete."
, "If the 240 color box is not visible then the terminal"
, "claims 240 colors are not supported."
, "Try setting TERM to xterm-256color"
, "This text is on a lower layer than the event list."
, "Which means it'll be hidden soon."
, "Bye!"
, "Great Faith in the ¯\\_(ツ)_/¯"
, "¯\\_(ツ)_/¯ ¯\\_(ツ)_/¯ ¯\\_(ツ)_/¯ ¯\\_(ツ)_/¯"
]
colorbox_240 :: Image
colorbox_240 = vertCat $ map horizCat $ splitColorImages colorImages
where
colorImages = map (\i -> string (currentAttr `withBackColor` Color240 i) " ") [0..239]
splitColorImages [] = []
splitColorImages is = (take 20 is ++ [string defAttr " "]) : (splitColorImages (drop 20 is))
colorbox_16 :: Image
colorbox_16 = border <|> column0 <|> border <|> column1 <|> border <|> column2 <|> border
where
column0 = vertCat $ map lineWithColor normal
column1 = vertCat $ map lineWithColor bright
border = vertCat $ replicate (length normal) $ string defAttr " | "
column2 = vertCat $ map (string defAttr . snd) normal
lineWithColor (c, cName) = string (defAttr `withForeColor` c) cName
normal = zip [ black, red, green, yellow, blue, magenta, cyan, white ]
[ "black", "red", "green", "yellow", "blue", "magenta", "cyan", "white" ]
bright = zip [ brightBlack, brightRed, brightGreen, brightYellow, brightBlue
, brightMagenta, brightCyan, brightWhite ]
[ "bright black", "bright red", "bright green", "bright yellow"
, "bright blue", "bright magenta", "bright cyan", "bright white" ]
2014-05-04 03:33:56 +04:00
updateDisplay :: App ()
updateDisplay = do
2014-06-05 10:57:32 +04:00
let info = string (defAttr `withForeColor` black `withBackColor` green)
"Press ESC to exit. Events for keys below."
2014-05-04 03:33:56 +04:00
eventLog <- foldMap (string defAttr) <$> get
2014-06-05 10:57:32 +04:00
let pic = picForImage (info <-> eventLog)
`addToBottom` (introText <-> colorbox_240 <|> colorbox_16)
2014-05-04 03:33:56 +04:00
vty <- ask
liftIO $ update vty pic
handleNextEvent = ask >>= liftIO . nextEvent >>= handleEvent
where
handleEvent e = do
modify $ (<|) (show e) >>> Seq.take eventBufferSize
return $ e == EvKey KEsc []