12 class TimeoutOccurred(Exception):
17 sys
.stdout
.write(string
)
21 def posix_inputimeout(prompt
='', timeout
=DEFAULT_TIMEOUT
):
23 sel
= selectors
.DefaultSelector()
24 sel
.register(sys
.stdin
, selectors
.EVENT_READ
)
25 events
= sel
.select(timeout
)
29 return key
.fileobj
.readline().rstrip(LF
)
32 termios
.tcflush(sys
.stdin
, termios
.TCIFLUSH
)
36 def win_inputimeout(prompt
='', timeout
=DEFAULT_TIMEOUT
):
38 begin
= time
.monotonic()
42 while time
.monotonic() < end
:
49 raise KeyboardInterrupt
52 cover
= SP
* len(prompt
+ line
+ SP
)
53 echo(''.join([CR
, cover
, CR
, prompt
, line
]))
69 inputimeout
= posix_inputimeout
74 inputimeout
= win_inputimeout