Skip to content

Commit 486aaed

Browse files
committed
Update color.py
1 parent 553a23f commit 486aaed

File tree

2 files changed

+58
-23
lines changed

2 files changed

+58
-23
lines changed

scripts/color.py

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# see https://stackoverflow.com/questions/287871/how-do-i-print-colored-text-to-the-terminal
2+
# and https://stackoverflow.com/questions/2048509/how-to-echo-with-different-colors-in-the-windows-command-line
23
import os
34
from functools import wraps
45

@@ -7,41 +8,75 @@
78
os.system("")
89

910
class Colors:
10-
HEADER = '\033[95m'
11-
OKBLUE = '\033[94m'
12-
OKCYAN = '\033[96m'
13-
OKGREEN = '\033[92m'
14-
WARNING = '\033[93m'
15-
FAIL = '\033[91m'
16-
END = '\033[0m'
11+
WHITE = '\033[97m'
12+
CYAN = '\033[96m'
13+
MAGENTA = '\033[95m'
14+
BLUE = '\033[94m'
15+
YELLOW = '\033[93m'
16+
GREEN = '\033[92m'
17+
RED = '\033[91m'
18+
BLACK = '\033[90m'
19+
20+
class Styles:
1721
BOLD = '\033[1m'
22+
DIM = '\033[2m'
23+
ITALIC = '\033[3m'
1824
UNDERLINE = '\033[4m'
1925

26+
END = '\033[0m'
27+
28+
class Modify:
29+
def __init__(self, color = '', style = ''):
30+
self.color = color
31+
self.style = style
32+
33+
def __enter__(self):
34+
print(self.color + self.style, end = "")
35+
return self
36+
37+
def __exit__(self, exc_type, exc_val, exc_tb):
38+
print(END, end = "")
39+
return False
40+
2041
def guard(f):
2142
@wraps(f)
2243
def g(*args, **kwargs):
2344
try:
2445
f(*args, **kwargs)
25-
# we want to catch SystemExit and KeyboardInterrupt too
26-
except BaseException as e:
27-
print(Colors.END, end = "")
28-
raise e
46+
finally:
47+
print(END, end = "")
2948
return g
3049

31-
@guard
50+
def with_modifiers(*modifiers):
51+
def util(f):
52+
@wraps(f)
53+
@guard
54+
def g(*args, **kwargs):
55+
print(*modifiers, sep = "", end = "")
56+
f(*args, **kwargs)
57+
return g
58+
return util
59+
60+
@with_modifiers(Colors.RED)
3261
def error(*args, **kwargs):
33-
print(Colors.FAIL, end = "")
3462
print(*args, **kwargs)
35-
print(Colors.END, end = "")
3663

37-
@guard
64+
@with_modifiers(Colors.GREEN)
3865
def success(*args, **kwargs):
39-
print(Colors.OKGREEN, end = "")
4066
print(*args, **kwargs)
41-
print(Colors.END, end = "")
4267

43-
@guard
68+
@with_modifiers(Colors.YELLOW)
4469
def warning(*args, **kwargs):
45-
print(Colors.WARNING, end = "")
4670
print(*args, **kwargs)
47-
print(Colors.END, end = "")
71+
72+
@with_modifiers(Styles.BOLD)
73+
def bold(*args, **kwargs):
74+
print(*args, **kwargs)
75+
76+
@with_modifiers(Colors.MAGENTA)
77+
def header(*args, **kwargs):
78+
print(*args, **kwargs)
79+
80+
if __name__ == "__main__":
81+
with Modify(Colors.CYAN, Styles.ITALIC):
82+
print("asd")

scripts/manage.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
from os.path import join
33
from functools import wraps
4-
from color import error, warning, success, Colors
4+
from color import error, warning, success
55
from difflib import SequenceMatcher
66
import sys
77

@@ -120,10 +120,10 @@ def build(app, job = None):
120120

121121
import move_documents
122122
if job == "dev":
123-
open()
123+
view()
124124

125125
@app.command()
126-
def open(app):
126+
def view(app):
127127
"Opens the docs/index.html in browser."
128128
import webbrowser
129129
webbrowser.open(index_file, new=0, autoraise=True)

0 commit comments

Comments
 (0)