Various changes.
[python_utils.git] / ansi.py
diff --git a/ansi.py b/ansi.py
index 4c580c0a0262ad133a71f6def6ca63a7d003dd8f..769b29c46b21c705f8c8ad09906f9dd7239cfaa2 100755 (executable)
--- a/ansi.py
+++ b/ansi.py
@@ -1,9 +1,12 @@
 #!/usr/bin/env python3
 
+from abc import abstractmethod
 import difflib
+import io
 import logging
+import re
 import sys
-from typing import Dict, Optional, Tuple
+from typing import Any, Callable, Dict, Iterable, Optional, Tuple
 
 logger = logging.getLogger(__name__)
 
@@ -1846,6 +1849,37 @@ def bg(name: Optional[str] = "",
     return bg_24bit(red, green, blue)
 
 
+class StdoutInterceptor(io.TextIOBase):
+    def __init__(self):
+        self.saved_stdout: Optional[io.TextIOBase] = None
+        self.buf = ''
+
+    @abstractmethod
+    def write(self, s):
+        pass
+
+    def __enter__(self) -> None:
+        self.saved_stdout = sys.stdout
+        sys.stdout = self
+        return None
+
+    def __exit__(self, *args) -> bool:
+        sys.stdout = self.saved_stdout
+        print(self.buf)
+        return None
+
+
+class ProgrammableColorizer(StdoutInterceptor):
+    def __init__(self, patterns: Iterable[Tuple[re.Pattern, Callable[[Any, re.Pattern], str]]]):
+        super().__init__()
+        self.patterns = [_ for _ in patterns]
+
+    def write(self, s: str):
+        for pattern in self.patterns:
+            s = pattern[0].sub(pattern[1], s)
+        self.buf += s
+
+
 if __name__ == '__main__':
     def main() -> None:
         name = " ".join(sys.argv[1:])