aboutsummaryrefslogtreecommitdiff
path: root/rainbow_parenthesis/term.py
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--rainbow_parenthesis/term.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/rainbow_parenthesis/term.py b/rainbow_parenthesis/term.py
new file mode 100644
index 0000000..c712e74
--- /dev/null
+++ b/rainbow_parenthesis/term.py
@@ -0,0 +1,47 @@
+"""
+Output engine for rainbow parenthesis.
+
+Attaches terminal escape codes for use on UNIX-like systems.
+"""
+
+from enum import auto, Enum
+# from dataclasses import dataclass, field
+
+from . import Colored
+
+
+CSI = '\033['
+
+
+class ANSIColor(Enum):
+ """Known CSI colors."""
+
+ # BLACK = 30
+ RED = 31
+ GREEN = auto()
+ YELLOW = auto()
+ BLUE = auto()
+ MAGENTA = auto()
+ CYAN = auto()
+
+ @classmethod
+ def get(cls, depth: int) -> int:
+ """Get color code for the given depth."""
+ return list(cls)[depth % len(list(cls))].value
+
+
+def SGR(*xs):
+ """
+ Build a CSI escape sequence.
+
+ https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_(Control_Sequence_Introducer)_sequences
+ """
+ return CSI + ';'.join(str(x) for x in xs) + 'm'
+
+
+def colorize(c: Colored) -> str:
+ """Return contents of colored, surrounded by color escapes."""
+ out = SGR(1, ANSIColor.get(c.depth))
+ out += c.item
+ out += SGR()
+ return out