X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=ansi.py;h=a49760037e31c6c01e84a225e7147d588f8f5934;hb=1ee64f3522abbbc2df154c8f44be87929a16e17e;hp=03f8fd27473c4e295cdafbb9e7122b4cec296453;hpb=5c212d7639f62fcb936f9d7a0bbe704a9f7b213d;p=python_utils.git diff --git a/ansi.py b/ansi.py index 03f8fd2..a497600 100755 --- a/ansi.py +++ b/ansi.py @@ -4,20 +4,20 @@ setting the text color, background, etc... using ANSI escape sequences.""" +import contextlib import difflib import io import logging import re import sys from abc import abstractmethod -from typing import Any, Callable, Dict, Iterable, Optional, Tuple +from typing import Any, Callable, Dict, Iterable, Literal, Optional, Tuple from overrides import overrides import logging_utils import string_utils - logger = logging.getLogger(__name__) # https://en.wikipedia.org/wiki/ANSI_escape_code @@ -1885,10 +1885,9 @@ def bg( return bg_24bit(red, green, blue) -class StdoutInterceptor(io.TextIOBase): - """An interceptor for data written to stdout. Use as a context. +class StdoutInterceptor(io.TextIOBase, contextlib.AbstractContextManager): + """An interceptor for data written to stdout. Use as a context.""" - """ def __init__(self): super().__init__() self.saved_stdout: io.TextIO = None @@ -1903,10 +1902,10 @@ class StdoutInterceptor(io.TextIOBase): sys.stdout = self return self - def __exit__(self, *args) -> Optional[bool]: + def __exit__(self, *args) -> Literal[False]: sys.stdout = self.saved_stdout print(self.buf) - return None + return False class ProgrammableColorizer(StdoutInterceptor): @@ -1914,6 +1913,7 @@ class ProgrammableColorizer(StdoutInterceptor): something (usually add color to) the match. """ + def __init__( self, patterns: Iterable[Tuple[re.Pattern, Callable[[Any, re.Pattern], str]]], @@ -1929,6 +1929,7 @@ class ProgrammableColorizer(StdoutInterceptor): if __name__ == '__main__': + def main() -> None: import doctest