Adds doctests.
authorScott <[email protected]>
Thu, 13 Jan 2022 04:57:11 +0000 (20:57 -0800)
committerScott <[email protected]>
Thu, 13 Jan 2022 04:57:11 +0000 (20:57 -0800)
ansi.py

diff --git a/ansi.py b/ansi.py
index d30ae27e625dbe585d82d3392837b39bf436f888..950a0c4a3c809587bb7c8324a884c62d83b553ab 100755 (executable)
--- a/ansi.py
+++ b/ansi.py
@@ -1738,6 +1738,17 @@ def fg(name: Optional[str] = "",
        *,
        force_16color: bool = False,
        force_216color: bool = False) -> str:
+    """Return the ANSI escape sequence to change the foreground color
+    being printed.  Target colors may be indicated by name or R/G/B.
+    Result will use the 16 or 216 color scheme if force_16color or
+    force_216color are passed (respectively).  Otherwise the code will
+    do what it thinks best.
+
+    >>> import string_utils as su
+    >>> su.to_base64(fg('blue'))
+    b'G1szODs1OzIxbQ==\\n'
+
+    """
     import string_utils
 
     if name is not None and name == 'reset':
@@ -1788,6 +1799,17 @@ def pick_contrasting_color(name: Optional[str] = "",
                            red: Optional[int] = None,
                            green: Optional[int] = None,
                            blue: Optional[int] = None) -> Tuple[int, int, int]:
+    """This method will return a red, green, blue tuple representing a
+    contrasting color given the red, green, blue of a background
+    color or a color name of the background color.
+
+    >>> pick_contrasting_color(None, 20, 20, 20)
+    (255, 255, 255)
+
+    >>> pick_contrasting_color("white")
+    (0, 0, 0)
+
+    """
     import string_utils
 
     if name is not None and string_utils.is_full_string(name):
@@ -1825,6 +1847,14 @@ def bg(name: Optional[str] = "",
        *,
        force_16color: bool = False,
        force_216color: bool = False) -> str:
+    """Returns an ANSI color code for changing the current background
+    color.
+
+    >>> import string_utils as su
+    >>> su.to_base64(bg("red"))    # b'\x1b[48;5;196m'
+    b'G1s0ODs1OzE5Nm0=\\n'
+
+    """
     import string_utils
 
     if name is not None and name == 'reset':
@@ -1881,7 +1911,10 @@ class StdoutInterceptor(io.TextIOBase):
 
 
 class ProgrammableColorizer(StdoutInterceptor):
-    def __init__(self, patterns: Iterable[Tuple[re.Pattern, Callable[[Any, re.Pattern], str]]]):
+    def __init__(
+            self,
+            patterns: Iterable[Tuple[re.Pattern, Callable[[Any, re.Pattern], str]]]
+    ):
         super().__init__()
         self.patterns = [_ for _ in patterns]
 
@@ -1894,6 +1927,9 @@ class ProgrammableColorizer(StdoutInterceptor):
 
 if __name__ == '__main__':
     def main() -> None:
+        import doctest
+        doctest.testmod()
+
         name = " ".join(sys.argv[1:])
         for possibility in COLOR_NAMES_TO_RGB:
             if name in possibility: