Skip to content

Commit d00c68d

Browse files
simplified event handling
- also enhanced console scrolling behaviour
1 parent 941b807 commit d00c68d

File tree

3 files changed

+46
-40
lines changed

3 files changed

+46
-40
lines changed

cocos/interpreter/interpreter.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,22 +104,24 @@ def on_completion(self, command):
104104
output.append('\n')
105105
for symbol in possibilities:
106106
output.append(symbol + '\n')
107-
self.stdout.dispatch_event('on_completed', completed or slice, ''.join(output))
107+
self.dispatch_event('on_completion_done', completed or slice, ''.join(output))
108108

109109
def on_history_prev(self, command):
110110
item = self.history.previous()
111-
self.stdout.dispatch_event('on_set_command', item)
111+
self.dispatch_event('on_history_result', item)
112112

113113
def on_history_next(self, command):
114114
item = self.history.next()
115-
self.stdout.dispatch_event('on_set_command', item)
116-
117-
def on_update_completer(self, ns_locals):
118-
self.completer.namespace = ns_locals
119-
120-
Interpreter.register_event_type('on_exit')
121-
Interpreter.register_event_type('on_command')
122-
Interpreter.register_event_type('on_completion')
123-
Interpreter.register_event_type('on_history_prev')
124-
Interpreter.register_event_type('on_history_next')
125-
Interpreter.register_event_type('on_update_completer')
115+
self.dispatch_event('on_history_result', item)
116+
117+
def update_namespace(self, ns_locals=None, ns_globals=None):
118+
namespace = self.completer.namespace
119+
if ns_globals is not None:
120+
namespace.update(ns_globals)
121+
if ns_locals is not None:
122+
namespace.update(ns_locals)
123+
self.completer.namespace = namespace
124+
125+
Interpreter.register_event_type('on_command_done')
126+
Interpreter.register_event_type('on_completion_done')
127+
Interpreter.register_event_type('on_history_result')

cocos/layer/interpreter.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import pyglet
55

6-
from pyglet.event import EventDispatcher
76
from pyglet.text import caret, document, layout
87
from pyglet.window import key
98

@@ -12,8 +11,10 @@
1211
from cocos.layer.util_layers import ColorLayer
1312
from cocos.interpreter import Interpreter
1413

14+
LINE_HEIGHT = 20
1515

16-
class InterpreterLayer(Layer, EventDispatcher):
16+
17+
class InterpreterLayer(Layer):
1718
name = "py"
1819

1920
prompt = ">>> " #: first line prompt
@@ -104,6 +105,8 @@ def write(self, text):
104105
'color': self.cfg['code.color'],
105106
})
106107
self.layout.end_update()
108+
lines = len(text.split())
109+
self._scroll(lines)
107110

108111
#################
109112
# Layer API
@@ -129,6 +132,9 @@ def write_prompt(self, first_line=True):
129132
self.prompt_end = len(self.document.text)
130133
self.caret.position = len(self.document.text)
131134

135+
def _scroll(self, amount):
136+
self.layout.view_y -= amount * LINE_HEIGHT
137+
132138
#################
133139
# Layer Events
134140
#################
@@ -162,7 +168,7 @@ def on_exit(self):
162168
#################
163169
def on_text(self, text):
164170
if text == '\r':
165-
self.dispatch_event('on_command', self.get_command())
171+
self.on_command(self.get_command())
166172
else:
167173
self.caret.on_text(text)
168174

@@ -178,9 +184,9 @@ def on_text_motion(self, motion):
178184
elif motion == key.MOTION_DELETE:
179185
self.caret.on_text_motion(motion)
180186
elif motion == key.MOTION_UP:
181-
self.interpreter.dispatch_event('on_history_prev', self.get_command())
187+
self.interpreter.on_history_prev(self.get_command())
182188
elif motion == key.MOTION_DOWN:
183-
self.interpreter.dispatch_event('on_history_next', self.get_command())
189+
self.interpreter.on_history_next(self.get_command())
184190
elif motion == key.MOTION_BEGINNING_OF_LINE:
185191
self.caret.position = self.prompt_start
186192
elif motion == key.MOTION_END_OF_LINE:
@@ -193,7 +199,7 @@ def on_text_motion_select(self, motion):
193199
# Mouse Events
194200
#################
195201
def on_mouse_scroll(self, x, y, dx, dy):
196-
self.layout.view_y += dy * 16
202+
self._scroll(dy)
197203

198204
def on_mouse_press(self, x, y, button, modifiers):
199205
self.caret.on_mouse_press(x, y, button, modifiers)
@@ -225,11 +231,11 @@ def on_key_press(self, symbol, modifiers):
225231
self.document.delete_text(self.prompt_end, len(self.document.text))
226232
self.caret.position = self.prompt_end
227233
elif symbol == key.TAB:
228-
self.dispatch_event('on_completion', self.get_command())
234+
self.on_completion(self.get_command())
229235
elif symbol == key.PAGEUP:
230-
self.layout.view_y += 16
236+
self._scroll(-1)
231237
elif symbol == key.PAGEDOWN:
232-
self.layout.view_y -= 16
238+
self._scroll(1)
233239
else:
234240
return pyglet.event.EVENT_UNHANDLED
235241
return pyglet.event.EVENT_HANDLED
@@ -238,8 +244,11 @@ def on_key_press(self, symbol, modifiers):
238244
# Command Events
239245
#################
240246
def on_command(self, command):
247+
# clear autocompletion results
248+
self.document.delete_text(self.caret.position, len(self.document.text))
241249
self.write('\n')
242-
self.interpreter.dispatch_event('on_command', command)
250+
# execute command
251+
self.interpreter.on_command(command)
243252

244253
def on_command_done(self):
245254
self.write_prompt()
@@ -250,9 +259,9 @@ def on_completion(self, command):
250259
# clear the command line
251260
self.document.delete_text(self.prompt_end, len(self.document.text))
252261
# request completion
253-
self.interpreter.dispatch_event('on_completion', command)
262+
self.interpreter.on_completion(command)
254263

255-
def on_completed(self, completed, output):
264+
def on_completion_done(self, completed, output):
256265
# write out the result of completion
257266
self.write(completed)
258267
self.caret.position = len(self.document.text)
@@ -263,16 +272,10 @@ def on_completed(self, completed, output):
263272
# so write out all possibilities
264273
self.write(output)
265274

275+
def on_history_result(self, command):
276+
self.set_command(command)
277+
266278
def on_get_command(self):
267279
command = self.get_command()
268280
self.write(command + '\n')
269281

270-
def on_set_command(self, command):
271-
self.set_command(command)
272-
273-
InterpreterLayer.register_event_type('on_command')
274-
InterpreterLayer.register_event_type('on_command_done')
275-
InterpreterLayer.register_event_type('on_completion')
276-
InterpreterLayer.register_event_type('on_completed')
277-
InterpreterLayer.register_event_type('on_get_command')
278-
InterpreterLayer.register_event_type('on_set_command')

cocos/utils.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
__all__ = ['SequenceScene', 'FileIn', 'FileOut']
4646

4747
from StringIO import StringIO
48+
from pyglet.event import EventDispatcher
4849

4950
from cocos.layer import *
5051
from cocos.scene import Scene
@@ -71,23 +72,20 @@ def on_enter(self):
7172
self.p += 1
7273

7374

74-
class FileIO(StringIO):
75+
class FileIO(StringIO, EventDispatcher):
7576
"""
7677
A fake file object. If asked for a file number, returns one set on
7778
instance creation
7879
"""
7980

8081
def __init__(self, buffer, fn=-1):
81-
StringIO.__init__(self)
82+
super(FileIO, self).__init__()
8283
self.buffer = buffer
8384
self.fn = fn
8485

8586
def fileno(self):
8687
return self.fn
8788

88-
def dispatch_event(self, event_type, *args):
89-
self.buffer.dispatch_event(event_type, *args)
90-
9189

9290
class FileIn(FileIO):
9391
"""
@@ -98,9 +96,12 @@ def read(self, n=-1):
9896
return self.readline()
9997

10098
def readline(self, length=None):
101-
self.buffer.dispatch_event('on_get_command')
99+
import pdb; pdb.set_trace()
100+
self.dispatch_event('on_get_command')
102101
return ''
103102

103+
FileIn.register_event_type('on_get_command')
104+
104105

105106
class FileOut(FileIO):
106107
"""

0 commit comments

Comments
 (0)