-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextStyle.py
More file actions
103 lines (86 loc) · 3.61 KB
/
textStyle.py
File metadata and controls
103 lines (86 loc) · 3.61 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# ---------------------------------------------------------------------------- #
# Constants #
# ---------------------------------------------------------------------------- #
START_C = "\033"
END_C = f"{START_C}[00m"
COLORS = {
"default": f"{END_C}", # nathin
"green" : f"{START_C}[92m", # green [OK]
"red" : f"{START_C}[91m", # red [ERROR]
"yellow" : f"{START_C}[93m", # yellow [WARNING]
"cyan" : f"{START_C}[96m", # cyan [INPUT, INFO etc.]
"grey" : f"{START_C}[1;30m", # grey
}
STYLES = {
"default" : COLORS["default"],# nathin
"success" : COLORS["green"] , # green [OK]
"danger" : COLORS["red"] , # red [ERROR]
"warning" : COLORS["yellow"], # yellow [WARNING]
"primary" : COLORS["cyan"] , # cyan [INPUT, INFO etc.]
"secondary": COLORS["grey"] , # grey
}
class C:
"""Named ANSI escape codes.
Centralized so every script in this repo can share one set of color
constants instead of redefining its own ``class c: HEADER = ...`` block.
"""
HEADER = f"{START_C}[95m"
OKBLUE = f"{START_C}[94m"
OKCYAN = f"{START_C}[96m"
OKGREEN = f"{START_C}[92m"
WARNING = f"{START_C}[93m"
FAIL = f"{START_C}[91m"
GREY = f"{START_C}[1;30m"
ENDC = f"{START_C}[0m"
BOLD = f"{START_C}[1m"
UNDERLINE = f"{START_C}[4m"
BLINK = f"{START_C}[5m"
NEGATIVE = f"{START_C}[7m"
CROSSED = f"{START_C}[9m"
# ---------------------------------------------------------------------------- #
# Text style functions #
# ---------------------------------------------------------------------------- #
def style(text, style = 'primary'):
return f"{STYLES[style]}{text}{END_C}"
def color(text, color = 'cyan'):
return f"{COLORS[color]}{text}{END_C}"
def inputs(text):
prompt = input(style(text, "primary"))
return prompt
def warning(txt, forceReturn = "", forceExit = False):
if forceExit == True:
exit(f"❌ [FATAL] {txt}")
if forceReturn != "":
print(f"⚠️ [WARNING] {txt}")
return forceReturn
reply = input(f"⚠️ [WARNING] {txt} [Y/n]")
if reply.upper() == "Y" or reply == "":
return True
return False
def info(txt, type="info"):
types = {
"default": "❔ [INFO]",
"skip": "⏩ [SKIPPING]",
"info": "ℹ️ [INFO]",
}
if type in types:
prepend = types[type]
else:
prepend = types["default"]
print(f"{prepend} {txt}")
# ---------------------------------------------------------------------------- #
# Colored print helpers #
# ---------------------------------------------------------------------------- #
# Shared equivalents of the ``printGreen`` / ``printRed`` / ... helpers that
# used to be duplicated in individual scripts. Each wraps text in an ANSI
# escape and a reset so call sites like
# print(f"{printGreen('hello')} world")
# keep working as-is after the switch.
def printGreen(text): return color(text, "green")
def printRed(text): return color(text, "red")
def printYellow(text): return color(text, "yellow")
def printCyan(text): return color(text, "cyan")
def printGrey(text): return color(text, "grey")
def printBlinking(text): return f"{C.BLINK}{text}{C.ENDC}"
def printNegative(text): return f"{C.NEGATIVE}{text}{C.ENDC}"
def printCrossed(text): return f"{C.CROSSED}{text}{C.ENDC}"