/
command_line.py
424 lines (329 loc) · 13.3 KB
/
command_line.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import re
import pygame
import message_center
import text_object
message_center.add_channel("terminal_error")
message_center.add_channel("terminal_info")
## Arg type constants
ARG_OBJECT = 1
ARG_STRING = 2
ARG_NUMBER = 3
ARG_COMMAND = 4
ARG_LIST = 5
default_command_dict = {}
class ArgumentError(Exception):
pass
class StandardCommandError(Exception):
pass
import interface_classes
class CommandLine(interface_classes.Drawable):
def __init__(self, rect, identifier=None, command_dict=default_command_dict, fg_color=(0x66,0xDD,0x66), bg_color=(0x0C,0x26,0x0C), font_name="Droid Sans", font_size=16):
self.fg_color = fg_color
self.bg_color = bg_color
self.command_dict=command_dict
self.typing_buffer = []
self.helper_text = ""
self.output_buffer = []
self.font_name = font_name
self.font_size = font_size
interface_classes.Drawable.__init__(self, rect, identifier)
def reset_for_new_rect(self):
self.surface = pygame.Surface(self.rect.size)
self.text_subsurface = self.surface
self.text_anchor = text_object.SurfaceAnchoredText(self.text_subsurface, font_color=self.fg_color, bg_color=self.bg_color, font_name=self.font_name, font_size=self.font_size)
self.on_text_update()
self.internal_redraw()
def set_command_dict(self, command_dict):
self.clear()
self.command_dict=command_dict
self.on_text_update()
self.internal_redraw()
def get_line_end(self):
try:
return self.typing_buffer.index("\n")
except ValueError:
return -1
def get_current_line(self):
line_end = self.get_line_end()
if line_end == -1:
return self.filter_line(''.join(self.typing_buffer))
else:
return self.filter_line(''.join(self.typing_buffer[0:line_end]))
def current_line_is_complete(self):
line_end = self.get_line_end()
if line_end == -1:
return False
else:
return True
def clear_current_line(self):
line_end = self.get_line_end()
if line_end == -1:
self.typing_buffer = []
else:
self.typing_buffer = self.typing_buffer[line_end+1:]
def replace_current_line(self, line):
line_end = self.get_line_end()
if line_end == -1:
self.typing_buffer = list(line)
else:
self.typing_buffer = list(line) + self.typing_buffer[line_end+1:]
def filter_line(self, line):
space_collapsed_line = re.sub(r"\s+"," ", line)
lower_case_line = space_collapsed_line.lower()
acceptable_characters_line = re.sub(r"[^a-z0-9_. ()\"]","",lower_case_line)
return acceptable_characters_line
def parse_command_strings_from_line(self, line):
# "(?:^|\b)([a-z0-9_.]+)(?:$|\b)"
search_results = re.findall(r'\s*(\w+)(?:\s|$)', line)
if line[-1:] == ' ': search_results.append("")
return search_results
def clear(self):
self.typing_buffer = []
def internal_redraw(self):
self.text_anchor.replace_text((''.join(self.typing_buffer) or "\n") + "\n~~~\n" + self.helper_text)
self.text_anchor.draw()
def draw_to_surface(self, surface):
surface.blit(self.surface, self.rect)
def tab_complete_current_line(self):
current_line = self.get_current_line()
completed_line = self.tab_complete_and_return_line(current_line)
self.replace_current_line(completed_line)
def keydown_event(self, event):
if event.key == pygame.K_BACKSPACE:
if len(self.typing_buffer) > 0: self.typing_buffer.pop()
elif event.key == pygame.K_RETURN:
self.typing_buffer.append("\n")
elif event.key == pygame.K_TAB:
self.tab_complete_current_line()
else:
self.typing_buffer.append(event.unicode)
self.on_text_update()
self.internal_redraw()
def on_text_update(self):
command_dict = self.command_dict
current_line = self.get_current_line()
cmd_strings = self.parse_command_strings_from_line(current_line)
self.helper_text = ""
if self.current_line_is_complete():
self.clear_current_line()
message_center.add_message(" \n>>> " + current_line + "\n")
if len(cmd_strings) == 0 : # User has entered blank command
return
command_string = cmd_strings.pop(0)
try:
if command_string not in command_dict:
raise StandardCommandError("Unrecognized Command: " + command_string)
command_object = command_dict[command_string]
if len(cmd_strings) > 0 and cmd_strings[-1] == "":
cmd_strings.pop()
if len(command_object.arg_handlers) != len(cmd_strings):
raise StandardCommandError("Wrong number of arguments for %s. Try 'help %s' for more information." % (command_string, command_string))
arg_objects = []
for arg_string, arg_handler in zip(cmd_strings, command_object.arg_handlers):
try:
arg_objects.append(arg_handler.get_target_for_arg_string(arg_string))
except ArgumentError, e:
message_center.add_message("Invalid argument: %s " % str(e), "terminal_error")
arg_objects.append(None)
if None in arg_objects:
raise StandardCommandError("Could not parse arguments; see previous errors. Try 'help %s' for command help." % command_string)
### If we made it this far, we have a valid command object which accepts the argument objects we have retreived.
command_object.execute(*arg_objects)
except StandardCommandError as e:
message_center.add_message(str(e), "terminal_error")
self.on_text_update()
else:
# Command line is not complete; this is where we do context help.
if len(cmd_strings) == 0: # Nothing useful has been entered.
self.helper_text = ", ".join(sorted(command_dict.keys()))
elif len(cmd_strings) == 1: # Command completion
command_in_progress = cmd_strings[0]
possible_completions = [command for command in command_dict.keys() if command.startswith(command_in_progress)]
if len(possible_completions) > 0:
self.helper_text = ", ".join(sorted(possible_completions))
else:
self.helper_text = "Unrecognized Command"
else:
command_string = cmd_strings.pop(0)
if command_string not in command_dict:
self.helper_text = "Unrecognized Command"
else:
command_object = command_dict[command_string]
if len(cmd_strings) > len(command_object.arg_handlers):
if cmd_strings[-1] == "":
self.helper_text = "Command complete. Press enter to continue."
else:
self.helper_text = "Too many arguments!"
else:
arg_number = len(cmd_strings)-1
arg_string = cmd_strings[arg_number]
self.helper_text = command_object.arg_handlers[arg_number].context_help_for_arg_string(arg_string)
### END ON_TEXT_UPDATE
def tab_complete_and_return_line(self, line):
command_dict = self.command_dict
cmd_strings = self.parse_command_strings_from_line(line)
if len(cmd_strings) == 0: # Nothing useful has been entered.
return line
elif len(cmd_strings) == 1: # Command completion
command_in_progress = cmd_strings[0]
possible_completions = [command for command in command_dict.keys() if command.startswith(command_in_progress)]
if len(possible_completions) == 1:
complete_command = possible_completions[0]
cmd_strings[0] = complete_command + " "
elif len(possible_completions) > 1:
names = possible_completions[:]
sample_name = names.pop(0)
matching_length = -1
still_matching = True
while still_matching:
matching_length += 1
for name in names:
if name[:matching_length] != sample_name[:matching_length]:
still_matching = False
matching_length -= 1
break
if matching_length > len(cmd_strings[0]):
cmd_strings[0] = sample_name[:matching_length]
else:
command_string = cmd_strings.pop(0)
if command_string not in command_dict:
return line
else:
command_object = command_dict[command_string]
if len(cmd_strings) > len(command_object.arg_handlers):
return line
else:
arg_number = len(cmd_strings)-1
arg_string = cmd_strings[arg_number]
cmd_strings[arg_number] = command_object.arg_handlers[arg_number].tab_complete_arg_string(arg_string)
cmd_strings.insert(0, command_string)
return " ".join(cmd_strings)
class Command:
def __init__(self, command_string, function, argument_handlers=[], command_dict=default_command_dict):
self.string = command_string
self.function = function
self.arg_handlers = list(argument_handlers)
command_dict[command_string] = self
def execute(self, *args):
self.function(*args)
class ArgumentHandler:
def __init__(self, filter_functions=None):
if not getattr(filter_functions, '__iter__', False):
filter_functions = (filter_functions,)
self.filters = filter_functions
def get_target_for_arg_string(self, arg_string):
return arg_string
def get_completions_starting_with(self, arg_string):
return (arg_string,)
def tab_complete_arg_string(self, arg_string):
possible_completions = self.get_completions_starting_with(arg_string)
if len(possible_completions) == 1:
complete_arg = possible_completions[0]
return complete_arg
cmd_strings[0] = complete_command
elif len(possible_completions) > 1:
names = possible_completions[:]
sample_name = names.pop(0)
matching_length = -1
still_matching = True
while still_matching:
matching_length += 1
for name in names:
if name[:matching_length] != sample_name[:matching_length]:
still_matching = False
matching_length -= 1
break
if matching_length > len(arg_string):
return sample_name[:matching_length]
else:
return arg_string
else:
return arg_string
def context_help_for_arg_string(self, arg_string):
return """Enter some text that starts and ends with a double-quote (")."""
class GameObjectArgumentHandler(ArgumentHandler):
def __init__(self, filter_functions=[], search_dict=None, no_target_message="No targets available for this command; Try a different command."):
ArgumentHandler.__init__(self, filter_functions=filter_functions)
self.no_target_message = no_target_message
self.search_dict = search_dict
def get_target_for_arg_string(self, arg_string):
if arg_string in self.search_dict:
target = self.search_dict[arg_string]
for filter_function in self.filters:
if not filter_function(target):
return None
return target
else:
return None
def get_completions_starting_with(self, arg_string):
possible_completions = [string for string in self.search_dict.keys() if string.startswith(arg_string)]
for filter_function in self.filters:
possible_completions = [name for name in possible_completions if filter_function(self.search_dict[name])]
possible_completions.sort()
return possible_completions
def context_help_for_arg_string(self, arg_string):
completions = self.get_completions_starting_with(arg_string)
if len(completions) > 0 :
return ", ".join(completions)
else:
return self.no_target_message
class DictKeyArgumentHandler(ArgumentHandler):
def __init__(self, filter_functions=[], search_dict=None, no_target_message="No targets available for this command; Try a different command."):
ArgumentHandler.__init__(self, filter_functions=filter_functions)
self.no_target_message = no_target_message
self.search_dict = search_dict
def get_target_for_arg_string(self, arg_string):
if arg_string in self.search_dict:
target = self.search_dict[arg_string]
for filter_function in self.filters:
if not filter_function(target):
return None
return arg_string
else:
return None
def get_completions_starting_with(self, arg_string):
possible_completions = [string for string in self.search_dict.keys() if string.startswith(arg_string)]
for filter_function in self.filters:
possible_completions = [name for name in possible_completions if filter_function(self.search_dict[name])]
possible_completions.sort()
return possible_completions
def context_help_for_arg_string(self, arg_string):
completions = self.get_completions_starting_with(arg_string)
if len(completions) > 0 :
return ", ".join(completions)
else:
return self.no_target_message
class StringArgumentHandler(ArgumentHandler):
def __init__(self, filter_functions=[], help_message="Enter a string."):
ArgumentHandler.__init__(self, filter_functions=filter_functions)
self.help_message = help_message
def get_target_for_arg_string(self, arg_string):
return arg_string
def context_help_for_arg_string(self, arg_string):
return self.help_message
class IntegerArgumentHandler(ArgumentHandler):
def __init__(self, filter_functions=[], help_message="Enter an integer."):
ArgumentHandler.__init__(self, filter_functions=filter_functions)
self.help_message = help_message
def get_target_for_arg_string(self, arg_string):
try:
return int(arg_string)
except ValueError:
return None
def context_help_for_arg_string(self, arg_string):
return self.help_message
class MultipleChoiceArgumentHandler(ArgumentHandler):
def __init__(self, filter_functions=[], choices=["yes","no"]):
ArgumentHandler.__init__(self, filter_functions=filter_functions)
self.choices = choices
def get_target_for_arg_string(self, arg_string):
if arg_string in self.choices:
return arg_string
else:
message_center.add_message(channel="error",message="'%s' is not a valid choice here." % arg_string)
return None
def context_help_for_arg_string(self, arg_string):
return ", ".join(self.get_completions_starting_with(arg_string))
def get_completions_starting_with(self, arg_string):
possible_completions = [string for string in self.choices if string.startswith(arg_string)]
return possible_completions