diff options
| author | Scott Gasch <[email protected]> | 2022-02-08 13:08:52 -0800 |
|---|---|---|
| committer | Scott Gasch <[email protected]> | 2022-02-08 13:08:52 -0800 |
| commit | 5c212d7639f62fcb936f9d7a0bbe704a9f7b213d (patch) | |
| tree | 7141828ab58490339e535d2873fe86d9f33d0c48 /ansi.py | |
| parent | dfc2136113428b99719c49a57d3ce68391dcb307 (diff) | |
Hook in pylint to the pre-commit hook and start to fix some of its
warnings. It seems pickier than flake8 which is probably a good
thing.
Diffstat (limited to 'ansi.py')
| -rwxr-xr-x | ansi.py | 27 |
1 files changed, 17 insertions, 10 deletions
@@ -1,5 +1,9 @@ #!/usr/bin/env python3 +"""A bunch of color names mapped into RGB tuples and some methods for +setting the text color, background, etc... using ANSI escape +sequences.""" + import difflib import io import logging @@ -11,6 +15,8 @@ from typing import Any, Callable, Dict, Iterable, Optional, Tuple from overrides import overrides import logging_utils +import string_utils + logger = logging.getLogger(__name__) @@ -1641,7 +1647,7 @@ def strike_through() -> str: def is_16color(num: int) -> bool: - return num == 255 or num == 128 + return num in (255, 128) def is_216color(num: int) -> bool: @@ -1751,8 +1757,6 @@ def fg( b'G1szODs1OzIxbQ==\\n' """ - import string_utils - if name is not None and name == 'reset': return '\033[39m' @@ -1810,8 +1814,6 @@ def pick_contrasting_color( (0, 0, 0) """ - import string_utils - if name is not None and string_utils.is_full_string(name): rgb = _find_color_by_name(name) else: @@ -1832,7 +1834,7 @@ def guess_name(name: str) -> str: max_ratio = r best_guess = possibility assert best_guess is not None - logger.debug(f"Best guess at color name is {best_guess}") + logger.debug("Best guess at color name is %s", best_guess) return best_guess @@ -1854,8 +1856,6 @@ def bg( b'G1s0ODs1OzE5Nm0=\\n' """ - import string_utils - if name is not None and name == 'reset': return '\033[49m' @@ -1886,7 +1886,11 @@ def bg( class StdoutInterceptor(io.TextIOBase): + """An interceptor for data written to stdout. Use as a context. + + """ def __init__(self): + super().__init__() self.saved_stdout: io.TextIO = None self.buf = '' @@ -1906,12 +1910,16 @@ class StdoutInterceptor(io.TextIOBase): class ProgrammableColorizer(StdoutInterceptor): + """A colorizing interceptor; pass it re.Patterns -> methods that do + something (usually add color to) the match. + + """ def __init__( self, patterns: Iterable[Tuple[re.Pattern, Callable[[Any, re.Pattern], str]]], ): super().__init__() - self.patterns = [_ for _ in patterns] + self.patterns = list(patterns) @overrides def write(self, s: str): @@ -1921,7 +1929,6 @@ class ProgrammableColorizer(StdoutInterceptor): if __name__ == '__main__': - def main() -> None: import doctest |
