on da635bb help sign in
vty/test/Bench.hs
62 lines 2.1 kB history raw
1module Main where
3import Graphics.Vty hiding ( pad )
5import Control.Concurrent( threadDelay )
6import Control.Monad( liftM2 )
8import Data.List
9import Data.Word
11import System.Environment( getArgs )
12import System.IO
13import System.Random
16main = do
17 let fixed_gen = mkStdGen 0
18 setStdGen fixed_gen
19 args <- getArgs
20 case args of
21 [] -> mkVty >>= liftM2 (>>) (run $ Just 0) shutdown
22 ["--delay"] -> mkVty >>= liftM2 (>>) (run $ Just 1000000) shutdown
23 ["--slow"] -> mkVty >>= liftM2 (>>) (run Nothing ) shutdown
24 _ -> fail "usage: ./Bench [--slow|--delay]"
26run (Just delay) vt = mapM_ (\p -> update vt p >> threadDelay delay) . benchgen =<< display_bounds (terminal vt)
27run Nothing vt = mapM_ (update vt) (benchgen $ DisplayRegion 200 100)
29-- Currently, we just do scrolling.
30takem :: (a -> Word) -> Word -> [a] -> ([a],[a])
31takem len n [] = ([],[])
32takem len n (x:xs) | lx > n = ([], x:xs)
33 | True = let (tk,dp) = takem len (n - lx) xs in (x:tk,dp)
34 where lx = len x
36fold :: (a -> Word) -> [Word] -> [a] -> [[a]]
37fold len [] xs = []
38fold len (ll:lls) xs = let (tk,dp) = takem len ll xs in tk : fold len lls dp
40lengths :: Word -> StdGen -> [Word]
41lengths ml g =
42 let (x,g2) = randomR (0,fromEnum ml) g
43 (y,g3) = randomR (0,x) g2
44 in (toEnum y) : lengths ml g3
46nums :: StdGen -> [(Attr, String)]
47nums g = let (x,g2) = (random g :: (Int, StdGen))
48 (c,g3) = random g2
49 in ( if c then def_attr `with_fore_color` red else def_attr
50 , shows x " "
51 ) : nums g3
53pad :: Word -> Image -> Image
54pad ml img = img <|> char_fill def_attr ' ' (ml - image_width img) 1
56clines :: StdGen -> Word -> [Image]
57clines g maxll = map (pad maxll . horiz_cat . map (uncurry string)) $ fold (toEnum . length . snd) (lengths maxll g1) (nums g2)
58 where (g1,g2) = split g
60benchgen :: DisplayRegion -> [Picture]
61benchgen (DisplayRegion w h) = take 500 $ map ((\i -> pic_for_image i) . vert_cat . take (fromEnum h)) $ tails $ clines (mkStdGen 42) w