|
26 | 26 | ENCODING_RE = \
|
27 | 27 | re.compile(br'([ \t\v]*#.*(\r\n?|\n))??[ \t\v]*#.*coding[:=][ \t]*([-\w.]+)') # type: Final
|
28 | 28 |
|
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 |
| - |
37 | 29 | DEFAULT_SOURCE_OFFSET = 4 # type: Final
|
38 | 30 |
|
39 | 31 | # At least this number of columns will be shown on each side of
|
@@ -476,6 +468,13 @@ def hash_digest(data: bytes) -> str:
|
476 | 468 | return hashlib.sha256(data).hexdigest()
|
477 | 469 |
|
478 | 470 |
|
| 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 | + |
479 | 478 | class FancyFormatter:
|
480 | 479 | """Apply color and bold font to terminal output.
|
481 | 480 |
|
@@ -553,16 +552,15 @@ def initialize_unix_colors(self) -> bool:
|
553 | 552 | bold = curses.tigetstr('bold')
|
554 | 553 | under = curses.tigetstr('smul')
|
555 | 554 | 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): |
557 | 558 | return False
|
558 | 559 |
|
559 | 560 | self.NORMAL = curses.tigetstr('sgr0').decode()
|
560 | 561 | self.BOLD = bold.decode()
|
561 | 562 | 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) |
566 | 564 | self.BLUE = curses.tparm(set_color, curses.COLOR_BLUE).decode()
|
567 | 565 | self.GREEN = curses.tparm(set_color, curses.COLOR_GREEN).decode()
|
568 | 566 | self.RED = curses.tparm(set_color, curses.COLOR_RED).decode()
|
|
0 commit comments