metaverse-dev/scripts/colorize

59 lines
1.4 KiB
Plaintext
Raw Normal View History

#!/usr/bin/python
import sys
argv = sys.argv[1:]
color = {
'black': '\033[30m',
'red': '\033[31m',
'green': '\033[32m',
'yellow': '\033[33m',
'blue': '\033[34m',
'pink': '\033[35m',
'cyan': '\033[36m',
'white': '\033[37m',
'lblack': '\033[1;30m',
'lred': '\033[1;31m',
'lgreen': '\033[1;32m',
'lyellow': '\033[1;33m',
'lblue': '\033[1;34m',
'lpink': '\033[1;35m',
'lcyan': '\033[1;36m',
'lwhite': '\033[1;37m',
}
replace = []
for arg in argv:
aarg = arg.split("=")
first = aarg[0]
second = aarg[1]
if color.get(second) == None:
print(f"There is no color '{second}'.")
exit(0)
second = color[second]
replace.append((first, second))
for line in sys.stdin:
if line.strip() == '':
break
line = line.strip()
for s in replace:
start = 0
while start < len(line):
for i in range(start, len(line) - len(s[0])):
ok = True
for j in range(len(s[0])):
if line[i+j] != s[0][j]:
ok = False
break
if ok:
line = line[:i+j+1] + "\033[0m" + line[i+j+1:]
line = line[:i] + s[1] + line[i:]
start = i + j + len(s[1]) + len("\033[0m")
break
if not ok:
break
print(line)