-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoloredText.py
182 lines (143 loc) · 5.57 KB
/
coloredText.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
"""
Change the color of printed text
"""
from collections import deque
from enum import Enum
class ColorCode(Enum):
CLEAR = "\033[0m",
RED = "\033[31m",
GREEN = "\033[32m",
YELLOW = "\033[33m",
BLUE = "\033[34m",
MAGENTA = "\033[35m",
CYAN = "\033[36m",
WHITE = "\033[37m",
LIGHT_RED = "\033[31;1m",
LIGHT_GREEN = "\033[32;1m",
LIGHT_YELLOW = "\033[33;1m",
LIGHT_BLUE = "\033[34;1m",
LIGHT_MAGENTA = "\033[35;1m",
LIGHT_CYAN = "\033[36;1m",
LIGHT_WHITE = "\033[37;1m",
def red(text):
return f"\u001b[31m{text}\u001b[0m"
def green(text):
return f"\u001b[32m{text}\u001b[0m"
def yellow(text):
return f"\u001b[33m{text}\u001b[0m"
def blue(text):
return f"\u001b[34m{text}\u001b[0m"
def magenta(text):
return f"\u001b[35m{text}\u001b[0m"
def cyan(text):
return f"\u001b[36m{text}\u001b[0m"
def white(text):
return f"\u001b[37m{text}\u001b[0m"
def lightRed(text):
return f"\u001b[31;1m{text}\u001b[0m"
def lightGreen(text):
return f"\u001b[32;1m{text}\u001b[0m"
def lightYellow(text):
return f"\u001b[33;1m{text}\u001b[0m"
def lightBlue(text):
return f"\u001b[34;1m{text}\u001b[0m"
def lightMagenta(text):
return f"\u001b[35;1m{text}\u001b[0m"
def lightCyan(text):
return f"\u001b[36;1m{text}\u001b[0m"
def lightWhite(text):
return f"\u001b[37;1m{text}\u001b[0m"
"""Messing around with in-line color codes:
def color(text):
splitText = text.split("[")
for i in range(len(splitText)):
splitText[i] = splitText[i].split("]")
print(splitText)
"""
opening_color_codes = [
"[red]",
"[green]",
"[yellow]",
"[blue]"
]
closing_color_codes = [
"[/red]",
"[/green]",
"[/yellow]",
"[/blue]"
]
#Input text with inline custom color codes and output ANSI color codes
def color(text):
actual_text = text
for i in range(len(opening_color_codes)):
current_color_code = opening_color_codes[i]
if actual_text.find(current_color_code) != -1:
#print("opening color code found")
divided_string = actual_text.split(current_color_code)
#print(divided_string)
#print(i)
second_half_of_string = divided_string[1]
#print(second_half_of_string)
#print(second_half_of_string.find(closing_color_codes[i]))
second_half_of_string = second_half_of_string.split(closing_color_codes[i])
#print(second_half_of_string)
divided_string.pop()
divided_string.append(second_half_of_string[0])
divided_string.append(second_half_of_string[1])
#print(divided_string)
divided_string[1] = eval(getColorFromColorTag(current_color_code))(divided_string[1])
#print(divided_string)
final_string = divided_string[0] + divided_string[1] + divided_string [2]
actual_text = final_string
return actual_text
# use stack to keep track of colors
# read string linearly
# when encountering open tag, push new color and replace with color code
# when encountering close tag, pop color and apply top-of-stack color code (white if empty)
# TODO: update color codes with styles and other customizations
def gabeColor(text: str, default_color: ColorCode = ColorCode.WHITE):
text_list: list[str] = [] # recompile text after replacing color codes
color_stack = deque()
text_begin = 0
tag_begin = 0
tag_end = 0
while (tag_begin := text.find('|', tag_end)) != -1:
tag_end = text.find('|', tag_begin + 1)
tag = text[tag_begin:tag_end+1]
#print(f"text_begin: {text_begin}, tag_begin: {tag_begin}, tag_end: {tag_end}")
#print(tag)
color_enum = default_color
if not isClosingColorTag(tag):
color = getColorFromColorTag(tag)
color_enum = ColorCode[color.upper()]
color_stack.append(color_enum)
else:
color_stack.pop()
color_enum = color_stack[-1] if len(color_stack) > 0 else default_color
text_list.append(text[text_begin:tag_begin])
text_list.append(color_enum.value[0])
tag_end += 1
text_begin = tag_end
text_list.append(text[text_begin:]) # append rest of text after final tag
compiled_text = "".join(text_list)
print(compiled_text)
def getColorFromColorTag(tag: str) -> str: # example: |yellow| or |/yellow|
first_char = 1 if tag[1] != '/' else 2
color = tag[first_char:-1]
return color
def isClosingColorTag(tag: str) -> bool:
# efficient because left side is faster, but right side guarentees tag can find closing indicator
# right side of bool expression isn't evaluated unless left side fails
# this may not be necessary though
return tag[1] == '/' or tag.find('/') != -1
#debug/testing
test_text1 = "This is test text to check the [yellow]color of printed[/yellow] text. This is a second [red]color[/red] code"
test_text2 = "[yellow]This is test text to check the [green]color[/green] of printed text. This is a second [red]color[/red] code[/yellow]"
new_text1 = "This is test text to check the |yellow|color of printed|/yellow| text. This is a second |red|color|/red| code"
new_text2 = "|yellow|This is test text to check the |green|color|/green| of printed text. This is a second |red|color|/red| code|/yellow|"
gabe_text3 = "|green|Hello, world! |magenta||blue||red|Welcum to|/red| or epicc gam mad|/blue| byyyy el jacque|/magenta| and el gabo|/green|"
gabeColor(new_text2)
gabeColor(new_text1)
gabeColor(gabe_text3)
#print(color(test_text1))
#print(color(test_text2))