Skip to content

Commit 2683f30

Browse files
Type annotations in output/vt100.py
1 parent 1db7eeb commit 2683f30

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

prompt_toolkit/output/vt100.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def get_code(self, value: Tuple[int, int, int],
166166
Return a (ansi_code, ansi_name) tuple. (E.g. ``(44, 'ansiblue')``.) for
167167
a given (r,g,b) value.
168168
"""
169-
key = (value, tuple(exclude))
169+
key: Hashable = (value, tuple(exclude))
170170
cache = self._cache
171171

172172
if key not in cache:
@@ -315,47 +315,49 @@ def _colors_to_code(self, fg_color: str, bg_color: str) -> Iterable[str]:
315315
# When requesting ANSI colors only, and both fg/bg color were converted
316316
# to ANSI, ensure that the foreground and background color are not the
317317
# same. (Unless they were explicitly defined to be the same color.)
318-
fg_ansi = [()]
318+
fg_ansi = ''
319+
320+
def get(color: str, bg: bool) -> List[int]:
321+
nonlocal fg_ansi
319322

320-
def get(color: str, bg: bool) -> Tuple[int, ...]:
321323
table = BG_ANSI_COLORS if bg else FG_ANSI_COLORS
322324

323325
if not color or self.color_depth == ColorDepth.DEPTH_1_BIT:
324-
return ()
326+
return []
325327

326328
# 16 ANSI colors. (Given by name.)
327329
elif color in table:
328-
return (table[color], )
330+
return [table[color]]
329331

330332
# RGB colors. (Defined as 'ffffff'.)
331333
else:
332334
try:
333335
rgb = self._color_name_to_rgb(color)
334336
except ValueError:
335-
return ()
337+
return []
336338

337339
# When only 16 colors are supported, use that.
338340
if self.color_depth == ColorDepth.DEPTH_4_BIT:
339341
if bg: # Background.
340342
if fg_color != bg_color:
341-
exclude = (fg_ansi[0], )
343+
exclude = [fg_ansi]
342344
else:
343-
exclude = ()
345+
exclude = []
344346
code, name = _16_bg_colors.get_code(rgb, exclude=exclude)
345-
return (code, )
347+
return [code]
346348
else: # Foreground.
347349
code, name = _16_fg_colors.get_code(rgb)
348-
fg_ansi[0] = name
349-
return (code, )
350+
fg_ansi = name
351+
return [code]
350352

351353
# True colors. (Only when this feature is enabled.)
352354
elif self.color_depth == ColorDepth.DEPTH_24_BIT:
353355
r, g, b = rgb
354-
return (48 if bg else 38, 2, r, g, b)
356+
return [(48 if bg else 38), 2, r, g, b]
355357

356358
# 256 RGB colors.
357359
else:
358-
return (48 if bg else 38, 5, _256_colors[rgb])
360+
return [(48 if bg else 38), 5, _256_colors[rgb]]
359361

360362
result: List[int] = []
361363
result.extend(get(fg_color, False))

0 commit comments

Comments
 (0)