|
1 | 1 | # -*- coding: utf-8 -*- |
| 2 | +import logging |
2 | 3 |
|
3 | 4 | from fmtstr.fmtfuncs import * |
4 | 5 | from fmtstr.fsarray import fsarray |
5 | 6 | from fmtstr.bpythonparse import func_for_letter |
| 7 | +from fmtstr.fmtstr import fmtstr, linesplit |
| 8 | + |
| 9 | +from bpython._py3compat import py3 |
| 10 | +if not py3: |
| 11 | + import inspect |
6 | 12 |
|
7 | | -import logging |
8 | 13 |
|
9 | 14 | #TODO take the boring parts of repl.paint out into here? |
10 | 15 |
|
@@ -52,19 +57,94 @@ def matches_lines(rows, columns, matches, current, config): |
52 | 57 | logging.debug('matches_lines: %r' % matches_lines) |
53 | 58 | return matches_lines |
54 | 59 |
|
55 | | -def formatted_argspec(argspec): |
56 | | - return argspec[0] + ': (' + ", ".join(argspec[1][0]) + ')' |
| 60 | +def formatted_argspec(argspec, columns, config): |
| 61 | + is_bound_method = argspec[2] |
| 62 | + func = argspec[0] |
| 63 | + args = argspec[1][0] |
| 64 | + kwargs = argspec[1][3] |
| 65 | + _args = argspec[1][1] #*args |
| 66 | + _kwargs = argspec[1][2] #**kwargs |
| 67 | + is_bound_method = argspec[2] |
| 68 | + in_arg = argspec[3] |
| 69 | + if py3: |
| 70 | + kwonly = argspec[1][4] |
| 71 | + kwonly_defaults = argspec[1][5] or dict() |
| 72 | + |
| 73 | + arg_color = func_for_letter(config.color_scheme['name']) |
| 74 | + func_color = func_for_letter(config.color_scheme['name'].swapcase()) |
| 75 | + punctuation_color = func_for_letter(config.color_scheme['punctuation']) |
| 76 | + token_color = func_for_letter(config.color_scheme['token']) |
| 77 | + bolds = {token_color: lambda x: bold(token_color(x)), |
| 78 | + arg_color: lambda x: bold(arg_color(x))} |
| 79 | + |
| 80 | + s = func_color(func) + arg_color(': (') |
| 81 | + |
| 82 | + if is_bound_method and isinstance(in_arg, int): #TODO what values could this have? |
| 83 | + in_arg += 1 |
| 84 | + |
| 85 | + for i, arg in enumerate(args): |
| 86 | + kw = None |
| 87 | + if kwargs and i >= len(args) - len(kwargs): |
| 88 | + kw = repr(kwargs[i - (len(args) - len(kwargs))]) |
| 89 | + color = token_color if in_arg in (i, arg) else arg_color |
| 90 | + if i == in_arg or arg == in_arg: |
| 91 | + color = bolds[color] |
| 92 | + |
| 93 | + if not py3: |
| 94 | + s += color(inspect.strseq(arg, str)) |
| 95 | + else: |
| 96 | + s += color(arg) |
| 97 | + |
| 98 | + if kw is not None: |
| 99 | + s += punctuation_color('=') |
| 100 | + s += token_color(kw) |
| 101 | + |
| 102 | + if i != len(args) - 1: |
| 103 | + s += punctuation_color(', ') |
| 104 | + |
| 105 | + if _args: |
| 106 | + if args: |
| 107 | + s += punctuation_color(', ') |
| 108 | + s += token_color('*%s' % (_args,)) |
| 109 | + |
| 110 | + #TODO what in the world is this about? Just transcribing from bpython/cli for now |
| 111 | + if py3 and kwonly: |
| 112 | + if not _args: |
| 113 | + if args: |
| 114 | + s += punctuation_color(', ') |
| 115 | + s += punctuation_color('*') |
| 116 | + marker = object() |
| 117 | + for arg in kwonly: |
| 118 | + s += punctuation_color(', ') |
| 119 | + color = token_color |
| 120 | + if in_arg: |
| 121 | + color = bolds[color] |
| 122 | + s += color(arg) |
| 123 | + default = kwonly_defaults.get(arg, marker) |
| 124 | + if default is not marker: |
| 125 | + s += punctuation_color('=') |
| 126 | + s += token_color(repr(default)) |
| 127 | + |
| 128 | + if _kwargs: |
| 129 | + if args or _args or (py3 and kwonly): |
| 130 | + s += token_color('**%s' % (_kwargs,)) |
| 131 | + s += punctuation_color(')') |
| 132 | + |
| 133 | + return linesplit(s, columns) |
| 134 | + |
| 135 | +def formatted_docstring(docstring, columns, config): |
| 136 | + color = func_for_letter(config.color_scheme['comment']) |
| 137 | + return sum(([color(x) for x in (display_linize(line, width) if line else fmtstr(''))] |
| 138 | + for line in docstring.split('\n')), []) |
57 | 139 |
|
58 | 140 | def paint_infobox(rows, columns, matches, argspec, match, docstring, config): |
59 | 141 | """Returns painted completions, argspec, match, docstring etc.""" |
60 | 142 | if not (rows and columns): |
61 | 143 | return fsarray(0, 0) |
62 | 144 | width = columns - 4 |
63 | | - color = func_for_letter(config.color_scheme['main']) |
64 | | - lines = ((display_linize(blue(formatted_argspec(argspec)), width) if argspec else []) + |
65 | | - sum(([color(x) for x in (display_linize(line, width) if line else fmtstr(''))] |
66 | | - for line in docstring.split('\n')) if docstring else [], []) + |
67 | | - (matches_lines(rows, columns, matches, match, config) if matches else []) |
| 145 | + lines = ((formatted_argspec(argspec, width, config) if argspec else []) + |
| 146 | + (matches_lines(rows, width, matches, match, config) if matches else []) + |
| 147 | + (formatted_docstring(docstring, width, config) if docstring else []) |
68 | 148 | ) |
69 | 149 |
|
70 | 150 | output_lines = [] |
|
0 commit comments