vty/demos/Demo.hs

91 lines
3.5 KiB
Haskell
Raw Permalink Normal View History

2015-01-02 12:39:11 +03:00
{-# LANGUAGE FlexibleContexts #-}
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.Sequence (Seq, (<|) )
import qualified Data.Sequence as Seq
import Data.Foldable
eventBufferSize = 1000
type App = RWST Vty () (Seq String) IO
main = do
vty <- if True -- change to false for emacs-like input processing
2017-01-22 23:43:21 +03:00
then mkVty defaultConfig
else mkVty (defaultConfig { vmin = Just 2, vtime = Just 300 } )
2014-05-04 03:33:56 +04:00
_ <- 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."
2022-03-23 19:49:10 +03:00
, "Followed by a description of the 16 color palette."
2022-03-20 19:39:52 +03:00
, "Followed by tones of red using a 24-bit palette."
2014-06-05 10:57:32 +04:00
, "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 ¯\\_(ツ)_/¯"
, "¯\\_(ツ)_/¯ ¯\\_(ツ)_/¯ ¯\\_(ツ)_/¯ ¯\\_(ツ)_/¯"
]
2022-03-20 19:39:52 +03:00
splitColorImages :: [Image] -> [[Image]]
splitColorImages [] = []
splitColorImages is = (take 20 is ++ [string defAttr " "]) : (splitColorImages (drop 20 is))
fullcolorbox :: Image
fullcolorbox = vertCat $ map horizCat $ splitColorImages colorImages
where
colorImages = map (\i -> string (currentAttr `withBackColor` linearColor i 0 0) " ") [0..255]
2014-06-05 10:57:32 +04:00
colorbox_240 :: Image
colorbox_240 = vertCat $ map horizCat $ splitColorImages colorImages
where
colorImages = map (\i -> string (currentAttr `withBackColor` Color240 i) " ") [0..239]
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)
2022-03-20 19:39:52 +03:00
`addToBottom` (introText <-> colorbox_240 <|> colorbox_16 <|> fullcolorbox)
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 []