Skip to content

Commit f2b71e4

Browse files
committed
Update color script
1 parent 1d079e2 commit f2b71e4

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

scripts/color.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ def g(*args, **kwargs):
5959
return g
6060
return util
6161

62+
@guard
63+
def guarded_print(*args, **kwargs):
64+
print(*args, **kwargs)
65+
6266
@with_modifiers(Colors.RED)
6367
def error(*args, **kwargs):
6468
print(*args, **kwargs)
@@ -100,15 +104,23 @@ def _split_multiple_without_removing(text, delimeters):
100104
text = temp
101105
return text
102106

103-
@guard
104-
def modify_words(text, words, color = '', style = '', replacement_rule = lambda x: x):
107+
def modify_text(text, words, color = '', replacement_rule = lambda x: x):
105108
t = ""
106109
for word in _split_multiple_without_removing(text, words):
107110
if word in words:
108-
t += color + style + replacement_rule(word) + END
111+
t += color + replacement_rule(word) + END
109112
else:
110113
t += word
111114
return t
112115

116+
def modified_print(text, color_mapping, replacement_rule = lambda x: x):
117+
for words, color in color_mapping.items():
118+
text = modify_text(text, words, color, replacement_rule = replacement_rule)
119+
guarded_print(text)
120+
113121
if __name__ == "__main__":
114-
print(modify_words("Merhaba dünya haha", ["dünya"], Colors.RED))
122+
guarded_print(modify_text(modify_text("Hello world!", ["world"], Colors.GREEN + Styles.UNDERLINE), ["Hello"], Colors.BLUE + Styles.ITALIC))
123+
modified_print("Colors here!", {
124+
("lors", "he"): Colors.YELLOW,
125+
("Co", "re!"): Colors.RED,
126+
})

scripts/manage.py

Lines changed: 3 additions & 2 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, Styles, modify_words
4+
from color import error, warning, success, Styles, Colors, modify_text
55
from difflib import SequenceMatcher
66
import sys
77

@@ -221,7 +221,8 @@ def version(app, field = "display"):
221221
f.write("\n".join(versions))
222222

223223
def highlight_arguments(procedure):
224-
return modify_words(procedure[0].__doc__, words = tuple(map(lambda x: f"[{x}]", procedure[1])), style = Styles.BOLD, replacement_rule = lambda x: x[1:-1])
224+
doc = procedure[0].__doc__
225+
return modify_text(doc, words = tuple(map(lambda x: f"[{x}]", procedure[1])), color = Styles.UNDERLINE + Colors.CYAN, replacement_rule = lambda x: x[1:-1])
225226

226227
@app.command(*app.procedures, "help")
227228
def help(app, method = None):

0 commit comments

Comments
 (0)