blob: c712e7470f97cc6e105b920a80770b36d71705c8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
|