on master help sign in
abstract event block generation from source table. initial terminfo caps based input block test
commit22774a905a0158bc4a97f9738f7371f7b33ed573 authorCorey O'Connor <corey@ooyala.com> dateSun 20 Oct 2013, 14:18 (13 years ago) parent4726c74
4 files changed+42−18
src/Graphics/Vty.hs+1−1 src/Graphics/Vty/Input.hs+7−3 test/VerifyUsingMockInput.hs+32−14 vty.cabal+2
src/Graphics/Vty.hs+1−1
@@ -73,7 +73,7 @@ data Vty = Vty
7373-- | Set up the state object for using vty. At most one state object should be
7474-- created at a time.
7575mkVty :: IO Vty
76−mkVty = mkVtyEscDelay 0
76+mkVty = mkVtyEscDelay defaultEscDelay
7777
7878-- | Set up the state object for using vty. At most one state object should be
7979-- created at a time. The delay, in microseconds, specifies the period of time to wait for a key
src/Graphics/Vty/Input.hs+7−3
@@ -4,11 +4,13 @@ module Graphics.Vty.Input ( Key(..)
44 , Button(..)
55 , Event(..)
66 , initTermInput
7+ , defaultEscDelay
78 )
89 where
910
1011import Graphics.Vty.Input.Data
1112import Graphics.Vty.Input.Internal
13+import Graphics.Vty.Input.Terminfo
1214
1315import Data.Char
1416import Data.Word
@@ -29,6 +31,9 @@ import System.Posix.IO ( stdInput
2931
3032import Foreign ( alloca, poke, peek, Ptr )
3133
34+defaultEscDelay :: Int
35+defaultEscDelay = 0
36+
3237-- | Set up the terminal for input. Returns a function which reads key
3338-- events, and a function for shutting down the terminal access.
3439initTermInput :: Int -> Terminal -> IO (IO Event, IO ())
@@ -76,9 +81,8 @@ initTermInput escDelay terminal = do
7681 finishAtomicInput
7782 loop
7883
79− extract_cap = first (getCapability terminal . tiGetStr)
80− caps_classify_table = map_to_legacy_table [(x,y) | (Just x,y) <- map extract_cap keys_from_caps_table]
81− term_event_classify_table = concat $ caps_classify_table : ansi_classify_table
84+ caps_legacy_table = map_to_legacy_table $ caps_classify_table terminal keys_from_caps_table
85+ term_event_classify_table = concat $ caps_legacy_table : ansi_classify_table
8286 term_event_classifier = classify term_event_classify_table
8387
8488 eventThreadId <- forkIO $ inputToEventThread term_event_classifier inputChannel eventChannel
test/VerifyUsingMockInput.hs+32−14
@@ -8,18 +8,22 @@ module Main where
88
99import Verify.Graphics.Vty.Terminal
1010
11+import Data.List (intersperse, permutations)
12+
1113import Graphics.Vty hiding (resize)
1214import Graphics.Vty.Input
1315import Graphics.Vty.Input.Data
1416import Graphics.Vty.Input.Internal
17+import Graphics.Vty.Input.Terminfo
1518
1619import Control.Applicative
1720import Control.Concurrent
1821import Control.Exception
1922import Control.Monad
2023
21−import System.Posix.Env
24+import System.Console.Terminfo
2225import System.IO
26+import System.Posix.Env
2327
2428import Test.Framework.Providers.SmallCheck
2529import Test.Framework
@@ -68,12 +72,6 @@ write_input_spec_to_chan chan (Delay t : input_spec')
6872
6973{-
7074
71−verify_keys_from_caps_table :: String -> Property
72−verify_keys_from_caps_table = do
73− term_name <- elements terminals_of_interest
74− cap <- elements keys_from_caps_table
75−
76−
7775tests :: IO [Test]
7876tests = do
7977 mappend
@@ -85,20 +83,40 @@ tests = do
8583 ]
8684-}
8785
88−data SimpleEvent = SimpleEvent (String, (Key, [Modifier]))
89− deriving (Show)
86+-- how to make this data dependent?
9087
91−instance Monad m => Serial m SimpleEvent where
92− series = generate (\n -> map SimpleEvent $ take n simple_chars)
88+newtype EventBlock event = EventBlock ([(String,event)] -> [(String, event)])
89+
90+instance Show (EventBlock event) where
91+ show (EventBlock g) = "EventBlock(* -> *)"
92+
93+instance Monad m => Serial m (EventBlock event) where
94+ series = do
95+ n :: Int <- localDepth (max 16) series -- what elements to select from the table
96+ return $ EventBlock $ \table -> concat (take n (permutations table))
9397
9498verify_simple_input_block_to_event :: Property IO
95−verify_simple_input_block_to_event = forAll $ \input_spec -> do
96− let input = Bytes $ concat [s | SimpleEvent (s,_) <- input_spec]
97− events = [e | SimpleEvent (_,(k,ms)) <- input_spec, let e = EvKey k ms]
99+verify_simple_input_block_to_event = forAll $ \(EventBlock block_gen) -> do
100+ let simple_input_seq = block_gen simple_chars
101+ input = Bytes $ concat [s | (s,_) <- simple_input_seq]
102+ events = [e | (_,(k,ms)) <- simple_input_seq, let e = EvKey k ms]
98103 monadic $ assert_events_from_input_block simple_chars [input] events
99104
105+verify_keys_from_caps_table_block_to_event :: Property IO
106+verify_keys_from_caps_table_block_to_event = forAll $ \(EventBlock block_gen) ->
107+ over (generate (\n -> take n terminals_of_interest)) $ \term_name -> monadic $ do
108+ terminal <- setupTerm term_name
109+ let caps_seq :: [(String, Event)] = block_gen keys_from_caps_table
110+ let bytes_seq = caps_classify_table terminal caps_seq
111+ input = [Bytes s | (s,_) <- bytes_seq]
112+ escaped_sequence = intersperse (Delay defaultEscDelay) input
113+ return True :: IO Bool
114+ -- cap <- elements keys_from_caps_table
115+
100116main = defaultMain
101117 [ testProperty "basic block generated from a single ansi chars to event translation"
102118 verify_simple_input_block_to_event
119+ , testProperty "key sequences read from caps table map to expected events"
120+ verify_keys_from_caps_table_block_to_event
103121 ]
104122
vty.cabal+2
@@ -62,6 +62,7 @@ library
6262 Graphics.Vty.Input
6363 Graphics.Vty.Input.Data
6464 Graphics.Vty.Input.Internal
65+ Graphics.Vty.Input.Terminfo
6566 Graphics.Vty.Picture
6667 Graphics.Vty.Terminal
6768 Graphics.Text.Width
@@ -527,6 +528,7 @@ test-suite verify-using-mock-input
527528 containers,
528529 deepseq >= 1.1 && < 1.4,
529530 mtl >= 1.1.1.0 && < 2.2,
531+ terminfo >= 0.3 && < 0.4,
530532 text >= 0.11.3,
531533 unix,
532534 utf8-string >= 0.3 && < 0.4,