-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconsole.py
More file actions
63 lines (40 loc) · 1.36 KB
/
console.py
File metadata and controls
63 lines (40 loc) · 1.36 KB
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
# Calling input() in separate thread to avoid freezing.
# Not used on web platform.
import threading
class Input:
def __init__(self, callback):
self.callback = callback
self.result = None # None until the text got
_inputs = [] # Input objects
_inputInProgress = False
_inputGot = False
def _consoleProc(prompt): # console thread
global _inputs, _inputInProgress, _inputGot
_inputInProgress = True
_inputs[-1].result = input(prompt) # blocking
_inputInProgress = False
_inputGot = True
def requestInput( prompt, callback ): # call in main thread
if _inputInProgress:
return False # Sorry! Previous input is not finished.
global _inputs
_inputs.append(Input(callback))
threading.Thread(
name = 'Console input',
target = _consoleProc,
args = (prompt,)
).start()
return True
def checkInputs(): # call in main thread
global _inputGot, _inputs
if not _inputGot: return
def check(i):
if i.result is None:
return True # keep waiting
else:
i.callback(i.result)
return False # not needed anymore
_inputs = list(filter(check, _inputs))
_inputGot = False
def isWaiting(): # call in main thread
return len(_inputs) > 0