More cleanup.
[python_utils.git] / ansi.py
diff --git a/ansi.py b/ansi.py
index 1633fddbcb31714d3ae7342daca37c1c4034b4c6..a49760037e31c6c01e84a225e7147d588f8f5934 100755 (executable)
--- a/ansi.py
+++ b/ansi.py
@@ -1,16 +1,22 @@
 #!/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 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__)
 
@@ -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'
 
@@ -1885,8 +1885,11 @@ def bg(
     return bg_24bit(red, green, blue)
 
 
-class StdoutInterceptor(io.TextIOBase):
+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
         self.buf = ''
 
@@ -1899,19 +1902,24 @@ 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):
+    """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):