Skip to content

Commit 2a15412

Browse files
committed
Somewhat reasonable argspec markup.
1 parent b993c67 commit 2a15412

File tree

1 file changed

+69
-3
lines changed

1 file changed

+69
-3
lines changed

bpython/urwid.py

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,16 +333,79 @@ def _populate_completion(self, main_loop, user_data):
333333
# This is just me flailing around wildly. TODO: actually write.
334334
if self.complete():
335335
if self.argspec:
336-
text = repr(self.argspec)
336+
# This is mostly just stolen from the cli module.
337+
func_name, args, is_bound, in_arg = self.argspec
338+
args, varargs, varkw, defaults = args[:4]
339+
if py3:
340+
kwonly, kwonly_defaults = args[4:]
341+
else:
342+
kwonly, kwonly_defaults = [], {}
343+
markup = [('bold name', func_name),
344+
('name', ': (')]
345+
346+
# the isinstance checks if we're in a positional arg
347+
# (instead of a keyword arg), I think
348+
if is_bound and isinstance(in_arg, int):
349+
in_arg += 1
350+
351+
# bpython.cli checks if this goes off the edge and
352+
# does clever wrapping. I do not (yet).
353+
for k, i in enumerate(args):
354+
if defaults and k + 1 > len(args) - len(defaults):
355+
kw = str(defaults[k - (len(args) - len(defaults))])
356+
else:
357+
kw = None
358+
359+
if not k and str(i) == 'self':
360+
color = 'name'
361+
else:
362+
color = 'token'
363+
364+
if k == in_arg or i == in_arg:
365+
color = 'bold ' + color
366+
367+
markup.append((color, str(i)))
368+
if kw:
369+
markup.extend([('punctuation', '='),
370+
('token', kw)])
371+
if k != len(args) - 1:
372+
markup.append(('punctuation', ', '))
373+
374+
if varargs:
375+
if args:
376+
markup.append(('punctuation', ', '))
377+
markup.append(('token', '*' + varargs))
378+
379+
if kwonly:
380+
if not varargs:
381+
if args:
382+
markup.append(('punctuation', ', '))
383+
markup.append(('punctuation', '*'))
384+
for arg in kwonly:
385+
if arg == in_arg:
386+
color = 'bold token'
387+
else:
388+
color = 'token'
389+
markup.extend([('punctuation', ', '),
390+
(color, arg)])
391+
if arg in kwonly_defaults:
392+
markup.extend([('punctuation', '='),
393+
('token', kwonly_defaults[arg])])
394+
395+
if varkw:
396+
if args or varargs or kwonly:
397+
markup.append(('punctuation', ', '))
398+
markup.append(('token', '**' + varkw))
399+
markup.append(('punctuation', ')'))
337400
else:
338-
text = ''
401+
markup = ''
402+
widget_list[0].set_text(markup)
339403
if self.matches:
340404
texts = [urwid.Text(('main', match))
341405
for match in self.matches]
342406
width = max(text.pack()[0] for text in texts)
343407
gridflow = urwid.GridFlow(texts, width, 1, 0, 'left')
344408
widget_list[1] = gridflow
345-
widget_list[0].set_text(text)
346409
self.frame.body = self.overlay
347410
else:
348411
self.frame.body = self.listbox
@@ -455,6 +518,9 @@ def main(args=None, locals_=None, banner=None):
455518
(name, COLORMAP[color.lower()], 'default',
456519
'bold' if color.isupper() else 'default')
457520
for name, color in config.color_scheme.iteritems()]
521+
palette.extend([
522+
('bold ' + name, color + ',bold', background, monochrome)
523+
for name, color, background, monochrome in palette])
458524

459525
if options.reactor:
460526
from twisted.application import reactors

0 commit comments

Comments
 (0)