Mercurial > hg > index.fcgi > rcg > rcg-1
view rcg.py @ 5:26e28ec5fe87
rcg.c: add color string; has bug with realloc()
author | paulo@twcdns.fastsearch.net |
---|---|
date | Tue, 06 Apr 2010 01:25:30 -0700 |
parents | |
children | 6627edb46f36 |
line source
1 #!/usr/bin/env python
3 import sys
4 import re
5 import getopt
7 #x = "\x1B[1;31;49m" + x
8 #x = START + c["white"][0] + ";" + c["blue"][1] + END + x + CLEAR
10 START = "\x1B["
11 BOLD = "1;"
12 END = "m"
13 CLEAR = START + "0" + END
15 c = {
16 # color : (fg, bg)
17 "black" : ("30", "40"),
18 "red" : ("31", "41"),
19 "green" : ("32", "42"),
20 "brown" : ("33", "43"),
21 "blue" : ("34", "44"),
22 "magenta" : ("35", "45"),
23 "cyan" : ("36", "46"),
24 "white" : ("37", "47"),
25 "default" : ("39", "49"),
26 }
28 bold = False
29 fg = None
30 bg = None
32 (optvals, args) = getopt.getopt(sys.argv[1:], "f:b:B")
34 for (opt, val) in optvals:
35 if opt == '-B':
36 bold = True
37 elif opt == '-f':
38 fg = val
39 elif opt == '-b':
40 bg = val
42 if len(args) > 0:
43 for line in sys.stdin:
44 repl = START
45 if bold:
46 repl += BOLD
47 if fg is not None:
48 repl += c[fg][0]
49 if bg is not None:
50 repl += ';' + c[bg][1]
51 repl += END + r'\1' + CLEAR
52 x = re.sub(r'(' + args[0] + r')', repl, line)
54 sys.stdout.write(x)
55 sys.stdout.flush()