-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcli.py
More file actions
391 lines (341 loc) · 12.4 KB
/
cli.py
File metadata and controls
391 lines (341 loc) · 12.4 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
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
#-----------------------------------------------------------------------------
"""
Command Line Interface
Implements a CLI with:
* hierarchical menus
* command completion
* command history
* context sensitive help
* command editing
Notes:
Menu Format: (name, descr, help, leaf, submenu)
Function Help Format: (parm, descr)
"""
#-----------------------------------------------------------------------------
import conio
#-----------------------------------------------------------------------------
class command:
def __init__(self, app):
"""initialise to an empty command string"""
self.app = app
self.clear()
def clear(self):
"""clear the command string"""
self.cmd = []
self.cursor = 0
self.old_cmd = []
self.old_cursor = 0
def repeat(self):
"""repeating the current command on a new line"""
# set the old command to null
self.old_cmd = []
self.old_cursor = 0
self.end()
def set(self, cmd):
"""set the command string to a new value"""
self.cmd = list(cmd)
self.cursor = len(cmd)
def get(self):
"""return the current command string"""
return ''.join(self.cmd)
def erase(self):
"""erase a character from the tail of the command string"""
del self.cmd[-1:]
# ensure the cursor is valid
self.end()
def backspace(self):
"""erase the character to the left of the cursor position"""
if self.cursor > 0:
del self.cmd[self.cursor - 1]
self.cursor -= 1
def delete(self):
"""erase the character at the cursor position"""
if self.cursor < len(self.cmd):
del self.cmd[self.cursor]
def add(self, x):
"""add character(s) to the command string"""
for c in x:
self.cmd.insert(self.cursor, c)
self.cursor += 1
def left(self):
"""move the cursor left"""
if self.cursor > 0:
self.cursor -= 1
def right(self):
"""move the cursor right"""
if self.cursor < len(self.cmd):
self.cursor += 1
def end(self):
"""move the cursor to the end"""
self.cursor = len(self.cmd)
def home(self):
"""move the cursor to the home"""
self.cursor = 0
def render(self):
"""render the command line"""
if (self.old_cmd == self.cmd) and (self.old_cursor == self.cursor):
return
# This is the dumbest thing that works
# Fix it for serial port operation
# erase the old command
n1 = self.old_cursor
n2 = len(self.old_cmd)
erase = ''.join(['\b' * n1, ' ' * n2, '\b' * n2])
self.app.io.put(erase)
# write the new command
self.app.io.put(self.get())
# position the cursor
bs = '\b' * (len(self.cmd) - self.cursor)
self.app.io.put(bs)
self.old_cmd = list(self.cmd)
self.old_cursor = self.cursor
#-----------------------------------------------------------------------------
class cli:
def __init__(self, app):
self.app = app
self.history = []
self.hidx = 0
self.cl = command(app)
self.running = True
self.prompt = '\n> '
self.poll = None
def set_root(self, root):
"""set the menu root"""
self.root = root
def set_prompt(self, prompt):
"""set the command prompt"""
self.prompt = prompt
def set_poll(self, poll):
"""set the external polling function"""
self.poll = poll
def func_help(self, help):
"""print help for a leaf function"""
self.app.io.put('\n\n')
for (parm, descr) in help:
if parm != '':
self.app.io.put(' %-19s: %s\n' % (parm, descr))
else:
self.app.io.put(' %-19s %s\n' % ('', descr))
def reset_history(self):
self.hidx = len(self.history)
def put_history(self, cmd):
"""put a command into the history list"""
n = len(self.history)
if (n == 0) or ((n >= 1) and (self.history[-1] != cmd)):
self.history.append(cmd)
def get_history_rev(self):
"""get history in the reverse (up) direction"""
n = len(self.history)
if n != 0:
if self.hidx > 0:
# go backwards
self.hidx -= 1
else:
# top of list
self.app.io.put(chr(conio.CHAR_BELL))
return self.history[self.hidx]
else:
# no history - return current command line
return self.cl.get()
def get_history_fwd(self):
"""get history in the forward (down) direction"""
n = len(self.history)
if self.hidx == n:
# not in the history list - return current command line
return self.cl.get()
elif self.hidx == n - 1:
# end of history recent - go back to an empty command
self.hidx = n
return ''
else:
# go forwards
self.hidx += 1
return self.history[self.hidx]
def error_str(self, msg, cmds, idx):
"""return a parse error string"""
marker = []
for i in range(len(cmds)):
l = len(cmds[i])
if i == idx:
marker.append('^' * l)
else:
marker.append(' ' * l)
return '\n'.join([msg, ' '.join(cmds), ' '.join(marker)])
def get_cmd(self):
"""
accumulate input characters to the command line
return True when processing is needed
return False for on going input
"""
c = self.app.io.get()
if c == conio.CHAR_NULL:
return False
elif (c == conio.CHAR_TAB) or (c == conio.CHAR_QM):
self.cl.end()
self.cl.add(chr(c))
return True
elif c == conio.CHAR_CR:
return True
elif c == conio.CHAR_DOWN:
self.cl.set(self.get_history_fwd())
return False
elif c == conio.CHAR_UP:
self.cl.set(self.get_history_rev())
return False
elif c == conio.CHAR_LEFT:
self.cl.left()
return False
elif c == conio.CHAR_RIGHT:
self.cl.right()
return False
elif c == conio.CHAR_END:
self.cl.end()
return False
elif c == conio.CHAR_HOME:
self.cl.home()
return False
elif c == conio.CHAR_ESC:
self.cl.clear()
return True
elif c == conio.CHAR_BS:
self.cl.backspace()
return False
elif c == conio.CHAR_DEL:
self.cl.delete()
return False
else:
self.cl.add(chr(c))
return False
def parse_cmd(self):
"""
parse and process the current command line
return True if we need a new prompt line
return False if we should reuse the existing one
"""
# scan the command line into a list of tokens
cmd_list = [cmd for cmd in self.cl.get().split(' ') if cmd != '']
# if there are no commands, print a new empty prompt
if len(cmd_list) == 0:
self.cl.clear()
return True
# trace each command through the menu tree
menu = self.root
for idx in range(len(cmd_list)):
cmd = cmd_list[idx]
# A trailing '?' means the user wants help for this command
if cmd[-1] == '?':
# strip off the '?'
cmd = cmd[:-1]
# print the matching items and help strings for this menu
self.app.io.put('\n\n')
for (name, descr, help, leaf, submenu) in menu:
if name.startswith(cmd):
self.app.io.put(' %-19s: %s\n' % (name, descr))
# strip off the '?' and recycle the command
self.cl.erase()
self.cl.repeat()
return True
# A trailing tab means the user wants command completion
if cmd[-1] == '\t':
# get rid of the tab
cmd = cmd[:-1]
self.cl.erase()
matches = []
for item in menu:
if item[0].startswith(cmd):
matches.append(item)
if len(matches) == 0:
# no completions
self.app.io.put(chr(conio.CHAR_BELL))
return False
elif len(matches) == 1:
# one completion: add it to the command
self.cl.add(matches[0][0][len(cmd):] + ' ')
self.cl.end()
return False
else:
# multiple completions: display them
self.app.io.put('\n\n')
for (name, descr, help, leaf, submenu) in matches:
self.app.io.put('%s ' % name)
self.app.io.put('\n')
# recycle the command
self.cl.repeat()
return True
# try to match the cmd with a unique menu item
matches = []
for item in menu:
if item[0] == cmd:
# accept an exact match
matches = [item]
break;
if item[0].startswith(cmd):
matches.append(item)
if len(matches) == 0:
# no matches - unknown command
self.app.io.put('\n\n%s\n' % self.error_str('unknown command', cmd_list, idx))
self.cl.repeat()
return True
if len(matches) == 1:
# one match - submenu/leaf
(name, descr, help, leaf, submenu) = matches[0]
if submenu != None:
# switch to the submenu - continue parsing
menu = submenu
continue
else:
# process leaf function - get the arguments
args = cmd_list[idx:]
del args[0]
if len(args) != 0:
if args[-1][-1] == '?':
# display help for the leaf function
self.func_help(help)
# strip off the '?', repeat the command
self.cl.erase()
self.cl.repeat()
return True
elif args[-1][-1] == '\t':
# tab happy user: strip off the tab
self.cl.erase()
self.cl.end()
return False
# call the leaf function
self.put_history(self.cl.get())
leaf(self.app, args)
self.cl.clear()
return True
else:
# multiple matches - ambiguous command
self.app.io.put('\n\n%s\n' % self.error_str('ambiguous command', cmd_list, idx))
self.cl.clear()
return True
# reached the end of the command list with no errors and no leaf function.
self.app.io.put('\n\nadditional input needed\n')
self.cl.repeat()
return True
def run(self):
"""get and process cli commands in a loop"""
self.app.io.put(self.prompt)
while self.running:
if self.get_cmd():
if self.parse_cmd():
if self.running == True:
# create a new prompt line
self.reset_history()
self.app.io.put('%s' % self.prompt)
else:
# clean exit
self.app.io.put('\n\n')
continue
# run the external polling routine
if self.poll != None:
if self.poll() == True:
# create a new prompt line
self.reset_history()
self.app.io.put('%s' % self.prompt)
self.cl.render()
def exit(self):
"""exit the cli"""
self.running = False
#-----------------------------------------------------------------------------