vty/test/BenchVerticalScroll.hs

62 lines
1.7 KiB
Haskell
Raw Normal View History

2013-01-28 09:41:59 +04:00
module BenchVerticalScroll where
import Graphics.Vty hiding ( pad )
import Verify
import Control.Concurrent( threadDelay )
import Control.Monad( liftM2 )
2014-01-27 01:25:13 +04:00
import Data.Default (def)
import Data.List
import Data.Word
import System.Environment( getArgs )
import System.IO
import System.Random
bench0 = do
let fixedGen = mkStdGen 0
setStdGen fixedGen
2014-01-27 01:25:13 +04:00
return $ Bench (return ()) (\() -> mkVty def >>= liftM2 (>>) run shutdown)
run vt = mapM_ (\p -> update vt p) . benchgen =<< displayBounds (outputIface vt)
-- Currently, we just do scrolling.
2013-06-01 16:18:00 +04:00
takem :: (a -> Int) -> Int -> [a] -> ([a],[a])
takem len n [] = ([],[])
takem len n (x:xs) | lx > n = ([], x:xs)
| True = let (tk,dp) = takem len (n - lx) xs in (x:tk,dp)
where lx = len x
2013-06-01 16:18:00 +04:00
fold :: (a -> Int) -> [Int] -> [a] -> [[a]]
fold len [] xs = []
fold len (ll:lls) xs = let (tk,dp) = takem len ll xs in tk : fold len lls dp
2013-06-01 16:18:00 +04:00
lengths :: Int -> StdGen -> [Int]
lengths ml g =
let (x,g2) = randomR (0,ml) g
(y,g3) = randomR (0,x) g2
in y : lengths ml g3
nums :: StdGen -> [(Attr, String)]
nums g = let (x,g2) = (random g :: (Int, StdGen))
(c,g3) = random g2
in ( if c then defAttr `withForeColor` red else defAttr
, shows x " "
) : nums g3
2013-06-01 16:18:00 +04:00
pad :: Int -> Image -> Image
pad ml img = img <|> charFill defAttr ' ' (ml - imageWidth img) 1
2013-06-01 16:18:00 +04:00
clines :: StdGen -> Int -> [Image]
clines g maxll = map (pad maxll . horizCat . map (uncurry string))
2013-06-01 16:18:00 +04:00
$ fold (length . snd) (lengths maxll g1) (nums g2)
where (g1,g2) = split g
benchgen :: DisplayRegion -> [Picture]
2013-12-20 10:24:56 +04:00
benchgen (w,h)
= take 2000 $ map ((\i -> picForImage i) . vertCat . take (fromEnum h))
2013-01-28 09:41:59 +04:00
$ tails
$ clines (mkStdGen 80) w