-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolored.py
More file actions
83 lines (71 loc) · 2.44 KB
/
Copy pathcolored.py
File metadata and controls
83 lines (71 loc) · 2.44 KB
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
from typing import Any
from colorama import Fore
class MetaColor(type):
"""
This Meta Class controls wether the console output is colored or not.
This is controlled by the `MetaColor.USE_COLOR` flag.
If color output is activated the class attributes of the class will
return their value otherwise an empty string.
if the class attr is all upper case or starts with a "_" it will
always return its value neither the state of the USE_COLOR flag
"""
USE_COLOR = True
def __init__(cls, name, bases, attrs):
super().__init__(name, bases, attrs)
def __getattribute__(cls, name:str):
if name.isupper() or name.startswith("_") or cls.USE_COLOR:
return type.__getattribute__(cls, name)
return ""
class Color(metaclass=MetaColor):
title = Fore.GREEN
normal_text = Fore.CYAN
info_text = Fore.LIGHTMAGENTA_EX
prompt = Fore.MAGENTA
user_input = Fore.YELLOW
error_message = Fore.RED
reset = Fore.RESET
color_keys = {
"%t": Color.title,
"%n": Color.normal_text,
"%i": Color.info_text,
"%ui": Color.user_input,
"%e": Color.error_message
}
reset_color = lambda: print(Color.reset, end="\r") # simplified way to clear colors without crating a new line
def parse_colors(text:str) -> str:
"""
go trough the text and add color codes
colors can be changed in colored.Color:
- %t for title
- %n for normal_text
- %i for info_text
- %ui for user_input
- %e for error_message
"""
for color_key, color_code in color_keys.items():
text = text.replace(color_key, color_code)
return text
def colored_print(text:str) -> None:
"""
procedure to parse color codes, print them and reset colors afterwards
color codes are listed in `parse_colors`
"""
text = parse_colors(text)
print(text)
reset_color()
error_message = colored_print # for easier readability in code
def colored_input(prompt:str) -> str:
"""
procedure to parse color codes, asks for input with the given prompt and resets the colors after input
color codes are listed in `parse_colors`
"""
prompt = parse_colors(prompt)
value = input(Color.prompt+prompt+Color.user_input)
reset_color()
return value
def run_colored(func):
""" run the function an reset colors afterwards, even if a exception is thrown """
try:
func()
finally:
reset_color()