annotate rcg.py @ 2:4efb50ce4c9c
rcg.c: expect only one match at a time from regexec()
author |
pang@apollo5.intellisis |
date |
Mon, 05 Apr 2010 21:02:51 -0700 |
parents |
|
children |
6627edb46f36 |
rev |
line source |
paulo@0
|
1 #!/usr/bin/env python
|
paulo@0
|
2
|
paulo@0
|
3 import sys
|
paulo@0
|
4 import re
|
paulo@0
|
5 import getopt
|
paulo@0
|
6
|
paulo@0
|
7 #x = "\x1B[1;31;49m" + x
|
paulo@0
|
8 #x = START + c["white"][0] + ";" + c["blue"][1] + END + x + CLEAR
|
paulo@0
|
9
|
paulo@0
|
10 START = "\x1B["
|
paulo@0
|
11 BOLD = "1;"
|
paulo@0
|
12 END = "m"
|
paulo@0
|
13 CLEAR = START + "0" + END
|
paulo@0
|
14
|
paulo@0
|
15 c = {
|
paulo@0
|
16 # color : (fg, bg)
|
paulo@0
|
17 "black" : ("30", "40"),
|
paulo@0
|
18 "red" : ("31", "41"),
|
paulo@0
|
19 "green" : ("32", "42"),
|
paulo@0
|
20 "brown" : ("33", "43"),
|
paulo@0
|
21 "blue" : ("34", "44"),
|
paulo@0
|
22 "magenta" : ("35", "45"),
|
paulo@0
|
23 "cyan" : ("36", "46"),
|
paulo@0
|
24 "white" : ("37", "47"),
|
paulo@0
|
25 "default" : ("39", "49"),
|
paulo@0
|
26 }
|
paulo@0
|
27
|
paulo@0
|
28 bold = False
|
paulo@0
|
29 fg = None
|
paulo@0
|
30 bg = None
|
paulo@0
|
31
|
paulo@0
|
32 (optvals, args) = getopt.getopt(sys.argv[1:], "f:b:B")
|
paulo@0
|
33
|
paulo@0
|
34 for (opt, val) in optvals:
|
paulo@0
|
35 if opt == '-B':
|
paulo@0
|
36 bold = True
|
paulo@0
|
37 elif opt == '-f':
|
paulo@0
|
38 fg = val
|
paulo@0
|
39 elif opt == '-b':
|
paulo@0
|
40 bg = val
|
paulo@0
|
41
|
paulo@0
|
42 if len(args) > 0:
|
paulo@0
|
43 for line in sys.stdin:
|
paulo@0
|
44 repl = START
|
paulo@0
|
45 if bold:
|
paulo@0
|
46 repl += BOLD
|
paulo@0
|
47 if fg is not None:
|
paulo@0
|
48 repl += c[fg][0]
|
paulo@0
|
49 if bg is not None:
|
paulo@0
|
50 repl += ';' + c[bg][1]
|
paulo@0
|
51 repl += END + r'\1' + CLEAR
|
paulo@0
|
52 x = re.sub(r'(' + args[0] + r')', repl, line)
|
paulo@0
|
53
|
paulo@0
|
54 sys.stdout.write(x)
|
paulo@0
|
55 sys.stdout.flush()
|