Skip to content

Commit 447623d

Browse files
argspec pixel perfect with bpython, fmtstr version bump
--HG-- branch : scroll-frontend
1 parent 13cc842 commit 447623d

File tree

2 files changed

+89
-9
lines changed

2 files changed

+89
-9
lines changed

bpython/scrollfrontend/replpainter.py

Lines changed: 88 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
# -*- coding: utf-8 -*-
2+
import logging
23

34
from fmtstr.fmtfuncs import *
45
from fmtstr.fsarray import fsarray
56
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
612

7-
import logging
813

914
#TODO take the boring parts of repl.paint out into here?
1015

@@ -52,19 +57,94 @@ def matches_lines(rows, columns, matches, current, config):
5257
logging.debug('matches_lines: %r' % matches_lines)
5358
return matches_lines
5459

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')), [])
57139

58140
def paint_infobox(rows, columns, matches, argspec, match, docstring, config):
59141
"""Returns painted completions, argspec, match, docstring etc."""
60142
if not (rows and columns):
61143
return fsarray(0, 0)
62144
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 [])
68148
)
69149

70150
output_lines = []

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def initialize_options(self):
153153
'pygments'
154154
],
155155
extras_require = {
156-
'scroll': ['fmtstr>=0.0.18'],
156+
'scroll': ['fmtstr>=0.0.20'],
157157
'urwid' : ['urwid']
158158
},
159159
tests_require = ['mock'],

0 commit comments

Comments
 (0)