summaryrefslogtreecommitdiff
path: root/ansi.py
diff options
context:
space:
mode:
Diffstat (limited to 'ansi.py')
-rwxr-xr-xansi.py38
1 files changed, 37 insertions, 1 deletions
diff --git a/ansi.py b/ansi.py
index d30ae27..950a0c4 100755
--- 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: