Skip to content

Commit 2b05c47

Browse files
committed
Extract colorize methods into reusable functions
1 parent 89dde8f commit 2b05c47

2 files changed

Lines changed: 26 additions & 21 deletions

File tree

Lib/_pyrepl/fancycompleter.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,27 @@ def safe_getattr(obj, name):
2121
return getattr(obj, name, None)
2222

2323

24+
def colorize_matches(names, values, theme):
25+
return [
26+
_color_for_obj(name, obj, theme)
27+
for name, obj in zip(names, values)
28+
]
29+
30+
def _color_for_obj(name, value, theme):
31+
t = type(value)
32+
color = _color_by_type(t, theme)
33+
return f"{color}{name}{ANSIColors.RESET}"
34+
35+
36+
def _color_by_type(t, theme):
37+
typename = t.__name__
38+
# this is needed e.g. to turn method-wrapper into method_wrapper,
39+
# because if we want _colorize.FancyCompleter to be "dataclassable"
40+
# our keys need to be valid identifiers.
41+
typename = typename.replace('-', '_').replace('.', '_')
42+
return getattr(theme.fancycompleter, typename, ANSIColors.RESET)
43+
44+
2445
class Completer(rlcompleter.Completer):
2546
"""
2647
When doing something like a.b.<tab>, keep the full a.b.attr completion
@@ -169,23 +190,7 @@ def _attr_matches(self, text):
169190
return expr, attr, names, values
170191

171192
def colorize_matches(self, names, values):
172-
return [
173-
self._color_for_obj(name, obj)
174-
for name, obj in zip(names, values)
175-
]
176-
177-
def _color_for_obj(self, name, value):
178-
t = type(value)
179-
color = self._color_by_type(t)
180-
return f"{color}{name}{ANSIColors.RESET}"
181-
182-
def _color_by_type(self, t):
183-
typename = t.__name__
184-
# this is needed e.g. to turn method-wrapper into method_wrapper,
185-
# because if we want _colorize.FancyCompleter to be "dataclassable"
186-
# our keys need to be valid identifiers.
187-
typename = typename.replace('-', '_').replace('.', '_')
188-
return getattr(self.theme.fancycompleter, typename, ANSIColors.RESET)
193+
return colorize_matches(names, values, self.theme)
189194

190195

191196
def commonprefix(names):

Lib/test/test_pyrepl/test_fancycompleter.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from _colorize import ANSIColors, get_theme
77
from _pyrepl.completing_reader import stripcolor
8-
from _pyrepl.fancycompleter import Completer, commonprefix
8+
from _pyrepl.fancycompleter import Completer, commonprefix, _color_for_obj
99
from test.support.import_helper import ready_to_import
1010

1111
class MockPatch:
@@ -167,9 +167,9 @@ def test_complete_global_colored(self):
167167
self.assertEqual(compl.global_matches('foobaz'), ['foobazzz'])
168168
self.assertEqual(compl.global_matches('nothing'), [])
169169

170-
def test_colorized_match_is_stripped(self):
171-
compl = Completer({'a': 42}, use_colors=True)
172-
match = compl._color_for_obj('spam', 1)
170+
def test_large_color_sort_prefix_is_stripped(self):
171+
theme = get_theme()
172+
match = _color_for_obj('spam', 1, theme)
173173
self.assertEqual(stripcolor(match), 'spam')
174174

175175
def test_complete_with_indexer(self):

0 commit comments

Comments
 (0)