This repository has been archived by the owner on Nov 23, 2023. It is now read-only.
/
ansiprint.py
executable file
·104 lines (87 loc) · 2.23 KB
/
ansiprint.py
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
104
#!/usr/bin/env python
'''Print message using ANSI terminal codes'''
__author__ = "Miki Tebeka <miki@mikitebeka.com>"
from sys import stdout, stderr
# Format
bright = 1
dim = 2
underline = 4
blink = 5
reverse = 7
hidden = 8
# Forground
black = 30
red = 31
green = 32
yellow = 33
blue = 34
magenta = 35
cyan = 36
white = 37
# Background
on_black = 40
on_red = 41
on_green = 42
on_yellow = 43
on_blue = 44
on_magenta = 45
on_cyan = 46
on_white = 47
def ansiformat(msg, *args):
'''Format msg according to args.
See http://www.termsys.demon.co.uk/vtansi.htm for more details/
'''
return "\033[%sm%s\033[0m" % (";".join(["%s" % f for f in args]), msg)
def ansiprint(msg, *args, **kw):
'''Print formatted message.
Should work on ANSI compatible terminal.
'''
if kw.get("stderr", 0):
outfo = stderr
else:
outfo = stdout
outfo.write(ansiformat(msg, *args))
outfo.flush()
if __name__ == "__main__":
from sys import argv, exit
from os.path import basename
h = {
"bright" : bright,
"dim" : dim,
"underline" : underline,
"blink" : blink,
"reverse" : reverse,
"hidden" : hidden,
"black" : black,
"red" : red,
"green" : green,
"yellow" : yellow,
"blue" : blue,
"magenta" : magenta,
"cyan" : cyan,
"white" : white,
"on_black" : on_black,
"on_red" : on_red,
"on_green" : on_green,
"on_yellow" : on_yellow,
"on_blue" : on_blue,
"on_magenta" : on_magenta,
"on_cyan" : on_cyan,
"on_white" : on_white
}
eg = "e.g. ansiprint hello red on_green underline -> %s" % \
ansiformat("hello", red, on_green, underline)
if len(argv) < 2:
print >> stderr, "usage: %s message [format ...]" % basename(argv[0])
print >> stderr, eg
exit(1)
for i in argv[2:]:
if i not in h:
ansiprint("%s: Unknown format\n" % i, red, bright, stderr=True)
print >> stderr, "Formats can be:",
msg = ", ".join([ansiformat(f, h[f]) for f in h.keys()])
print msg
print >> stderr, eg
exit(1)
ansiprint(argv[1], *[h[i] for i in argv[2:]])
print