-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput_timeout.py
88 lines (65 loc) · 1.79 KB
/
input_timeout.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# https://github.com/johejo/inputimeout/blob/master/inputimeout/inputimeout.py
import sys
DEFAULT_TIMEOUT = 30.0
INTERVAL = 0.05
SP = " "
CR = "\r"
LF = "\n"
CRLF = CR + LF
class InputTimeoutOccurred(Exception):
pass
def echo(string):
sys.stdout.write(string)
sys.stdout.flush()
def posix_input_timeout(prompt="", timeout=DEFAULT_TIMEOUT):
echo(prompt)
flush_input()
sel = selectors.DefaultSelector()
sel.register(sys.stdin, selectors.EVENT_READ)
events = sel.select(timeout)
if events:
key, _ = events[0]
return key.fileobj.readline().rstrip(LF)
else:
echo(LF)
termios.tcflush(sys.stdin, termios.TCIFLUSH)
raise InputTimeoutOccurred
def win_input_timeout(prompt="", timeout=DEFAULT_TIMEOUT):
echo(prompt)
begin = time.monotonic()
end = begin + timeout
line = ""
while time.monotonic() < end:
if msvcrt.kbhit():
c = msvcrt.getwche()
if c in (CR, LF):
echo(CRLF)
return line
if c == "\003":
raise KeyboardInterrupt
if c == "\b":
line = line[:-1]
cover = SP * len(prompt + line + SP)
echo("".join([CR, cover, CR, prompt, line]))
else:
line += c
time.sleep(INTERVAL)
echo(CRLF)
raise InputTimeoutOccurred
def flush_input():
try:
import msvcrt
while msvcrt.kbhit():
msvcrt.getch()
except ImportError:
import sys, termios # for linux/unix
termios.tcflush(sys.stdin, termios.TCIOFLUSH)
try:
import msvcrt
except ImportError:
import selectors
import termios
input_timeout = posix_input_timeout
else:
import time
input_timeout = win_input_timeout