Skip to content

Commit e815e48

Browse files
author
Brian Mboya
authored
Add gray color ANSI escape sequence (#9071)
1 parent 8c2ea0f commit e815e48

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

mypy/util.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,6 @@
2626
ENCODING_RE = \
2727
re.compile(br'([ \t\v]*#.*(\r\n?|\n))??[ \t\v]*#.*coding[:=][ \t]*([-\w.]+)') # type: Final
2828

29-
# This works in most default terminals works (because it is ANSI standard). The problem
30-
# this tries to solve is that although it is a basic ANSI "feature", terminfo files
31-
# for most default terminals don't have dim termcap entry, so curses doesn't report it.
32-
# Potentially, we can choose a grey color that would look good on both white and black
33-
# background, but it is not easy, and again most default terminals are 8-color, not 256-color,
34-
# so we can't get the color code from curses.
35-
PLAIN_ANSI_DIM = '\x1b[2m' # type: Final
36-
3729
DEFAULT_SOURCE_OFFSET = 4 # type: Final
3830

3931
# At least this number of columns will be shown on each side of
@@ -476,6 +468,13 @@ def hash_digest(data: bytes) -> str:
476468
return hashlib.sha256(data).hexdigest()
477469

478470

471+
def parse_gray_color(cup: bytes) -> str:
472+
"""Reproduce a gray color in ANSI escape sequence"""
473+
set_color = ''.join([cup[:-1].decode(), 'm'])
474+
gray = curses.tparm(set_color.encode('utf-8'), 1, 89).decode()
475+
return gray
476+
477+
479478
class FancyFormatter:
480479
"""Apply color and bold font to terminal output.
481480
@@ -553,16 +552,15 @@ def initialize_unix_colors(self) -> bool:
553552
bold = curses.tigetstr('bold')
554553
under = curses.tigetstr('smul')
555554
set_color = curses.tigetstr('setaf')
556-
if not (bold and under and set_color):
555+
set_eseq = curses.tigetstr('cup')
556+
557+
if not (bold and under and set_color and set_eseq):
557558
return False
558559

559560
self.NORMAL = curses.tigetstr('sgr0').decode()
560561
self.BOLD = bold.decode()
561562
self.UNDER = under.decode()
562-
dim = curses.tigetstr('dim')
563-
# TODO: more reliable way to get gray color good for both dark and light schemes.
564-
self.DIM = dim.decode() if dim else PLAIN_ANSI_DIM
565-
563+
self.DIM = parse_gray_color(set_eseq)
566564
self.BLUE = curses.tparm(set_color, curses.COLOR_BLUE).decode()
567565
self.GREEN = curses.tparm(set_color, curses.COLOR_GREEN).decode()
568566
self.RED = curses.tparm(set_color, curses.COLOR_RED).decode()

0 commit comments

Comments
 (0)