Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork252
Expand file tree
/
Copy pathinteraction.py
More file actions
174 lines (153 loc) · 5.86 KB
/
interaction.py
File metadata and controls
174 lines (153 loc) · 5.86 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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
importgreenlet
importtime
fromcurtsiesimportevents
from ..translationsimport_
from ..replimportInteraction
from ..curtsiesfrontend.eventsimportRefreshRequestEvent
from ..curtsiesfrontend.manual_readlineimportedit_keys
classStatusBar(Interaction):
"""StatusBar and Interaction for Repl
Passing of control back and forth between calls that use interact api
(notify, confirm, file_prompt) like bpython.Repl.write2file and events on
the main thread happens via those calls and
self.wait_for_request_or_notify.
Calling one of these three is required for the main thread to regain
control!
This is probably a terrible idea, and better would be rewriting this
functionality in a evented or callback style, but trying to integrate
bpython.Repl code.
"""
def__init__(
self,
config,
permanent_text="",
request_refresh=lambda:None,
schedule_refresh=lambdawhen:None,
):
self._current_line=""
self.cursor_offset_in_line=0
self.in_prompt=False
self.in_confirm=False
self.waiting_for_refresh=False
self.prompt=""
self._message=""
self.message_start_time=time.time()
self.message_time=3.0
self.permanent_stack= []
ifpermanent_text:
self.permanent_stack.append(permanent_text)
self.main_context=greenlet.getcurrent()
self.request_context=None
self.request_refresh=request_refresh
self.schedule_refresh=schedule_refresh
super().__init__(config)
defpush_permanent_message(self,msg):
self._message=""
self.permanent_stack.append(msg)
defpop_permanent_message(self,msg):
ifmsginself.permanent_stack:
self.permanent_stack.remove(msg)
else:
raiseValueError("Message %r was not in permanent_stack"%msg)
@property
defhas_focus(self):
returnself.in_promptorself.in_confirmorself.waiting_for_refresh
defmessage(self,msg,schedule_refresh=True):
"""Sets a temporary message"""
self.message_start_time=time.time()
self._message=msg
ifschedule_refresh:
self.schedule_refresh(time.time()+self.message_time)
def_check_for_expired_message(self):
if (
self._message
andtime.time()>self.message_start_time+self.message_time
):
self._message=""
defprocess_event(self,e)->None:
"""Returns True if shutting down"""
assertself.in_promptorself.in_confirmorself.waiting_for_refresh
ifisinstance(e,RefreshRequestEvent):
self.waiting_for_refresh=False
self.request_context.switch()
elifisinstance(e,events.PasteEvent):
foreeine.events:
# strip control seq
self.add_normal_character(eeiflen(ee)==1elseee[-1])
elife=="<ESC>"orisinstance(e,events.SigIntEvent):
self.request_context.switch(False)
self.escape()
elifeinedit_keys:
self.cursor_offset_in_line,self._current_line=edit_keys[e](
self.cursor_offset_in_line,self._current_line
)
elife=="<Ctrl-c>":# TODO can this be removed?
raiseKeyboardInterrupt()
elife=="<Ctrl-d>":# TODO this isn't a very intuitive behavior
raiseSystemExit()
elifself.in_promptandein ("\n","\r","<Ctrl-j>","Ctrl-m>"):
line=self._current_line
self.escape()
self.request_context.switch(line)
elifself.in_confirm:
ife.lower()==_("y"):
self.request_context.switch(True)
else:
self.request_context.switch(False)
self.escape()
else:# add normal character
self.add_normal_character(e)
defadd_normal_character(self,e):
ife=="<SPACE>":
e=" "
iflen(e)>1:
return
self._current_line= (
self._current_line[:self.cursor_offset_in_line]
+e
+self._current_line[self.cursor_offset_in_line :]
)
self.cursor_offset_in_line+=1
defescape(self):
"""unfocus from statusbar, clear prompt state, wait for notify call"""
self.in_prompt=False
self.in_confirm=False
self.prompt=""
self._current_line=""
@property
defcurrent_line(self):
self._check_for_expired_message()
ifself.in_prompt:
returnself.prompt+self._current_line
ifself.in_confirm:
returnself.prompt
ifself._message:
returnself._message
ifself.permanent_stack:
returnself.permanent_stack[-1]
return""
@property
defshould_show_message(self):
returnbool(self.current_line)
# interaction interface - should be called from other greenlets
defnotify(self,msg,n=3.0,wait_for_keypress=False):
self.request_context=greenlet.getcurrent()
self.message_time=n
self.message(msg,schedule_refresh=wait_for_keypress)
self.waiting_for_refresh=True
self.request_refresh()
self.main_context.switch(msg)
# below really ought to be called from greenlets other than main because
# they block
defconfirm(self,q):
"""Expected to return True or False, given question prompt q"""
self.request_context=greenlet.getcurrent()
self.prompt=q
self.in_confirm=True
returnself.main_context.switch(q)
deffile_prompt(self,s):
"""Expected to return a file name, given"""
self.request_context=greenlet.getcurrent()
self.prompt=s
self.in_prompt=True
returnself.main_context.switch(s)