-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecolorize
executable file
·63 lines (57 loc) · 1.37 KB
/
decolorize
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
#!/usr/local/bin/python
import sys
NORMAL = '\x1b[0m'
BOLD = '\x1b[1m'
UNDERLINE = '\x1b[4m'
INVERSE = '\x1b[7m'
FG_BLACK = '\x1b[30m'
FG_BLUE = '\x1b[34m'
FG_CYAN = '\x1b[36m'
FG_GREEN = '\x1b[32m'
FG_MAGENTA = '\x1b[35m'
FG_RED = '\x1b[31m'
FG_WHITE = '\x1b[37m'
FG_YELLOW = '\x1b[33m'
BG_BLACK = '\x1b[40m'
BG_BLUE = '\x1b[44m'
BG_CYAN = '\x1b[46m'
BG_GREEN = '\x1b[42m'
BG_MAGENTA = '\x1b[45m'
BG_RED = '\x1b[41m'
BG_WHITE = '\x1b[47m'
BG_YELLOW = '\x1b[43m'
colors = [BG_BLUE, BG_CYAN, BG_GREEN,
BG_MAGENTA, BG_RED, BG_WHITE,
BG_YELLOW, BOLD, FG_BLACK,
FG_BLUE, FG_CYAN, FG_GREEN,
FG_MAGENTA, FG_RED, FG_WHITE,
FG_YELLOW, INVERSE, NORMAL,
UNDERLINE]
def decolorize(text):
"""Strip color codes from text.
:Parameters:
text : `str`
:Returns:
Text without color formatting
:Rtype:
`str`
"""
for c in colors:
text = text.replace(c, "")
return text
def main():
# Treat args as filenames if given.
#
args = sys.argv[1:]
if args:
for arg in sys.argv[1:]:
with open(arg) as f:
for line in f.readlines():
sys.stdout.write(decolorize(line))
# Otherwise, decolorize stdin
#
else:
for line in sys.stdin:
sys.stdout.write(decolorize(line))
if __name__ == "__main__":
main()