-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKeyFSM.hs
More file actions
31 lines (27 loc) · 834 Bytes
/
KeyFSM.hs
File metadata and controls
31 lines (27 loc) · 834 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
module KeyFSM where
import System.IO ( BufferMode(NoBuffering)
, hSetBuffering, hSetEcho
, hGetBuffering, hGetEcho
, stdin, stdout, getChar
)
silently :: IO a -> IO a
silently f = do
inB <- hGetBuffering stdin
outB <- hGetBuffering stdout
inE <- hGetEcho stdin
outE <- hGetEcho stdout
hSetBuffering stdin NoBuffering
hSetBuffering stdout NoBuffering
hSetEcho stdout True -- HACK: required for clean exit
hSetEcho stdin False
hSetEcho stdout False
r <- f
hSetBuffering stdin inB
hSetBuffering stdout outB
hSetEcho stdin inE
hSetEcho stdout outE
return r
trap :: (Char -> Bool) -> String -> IO Char
trap p s = putStrLn s >> f
where f = do c' <- getChar
if p c' then return c' else f