annotate rcg.py @ 11:6627edb46f36
add "color line" option to reference python implementation
author |
paulo@thepaulopc |
date |
Sat, 06 Nov 2010 00:17:34 -0700 |
parents |
df03fa355b60 |
children |
e618a3ff2027 |
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@11
|
31 colorLine = False
|
paulo@0
|
32
|
paulo@11
|
33 (optvals, args) = getopt.getopt(sys.argv[1:], "f:b:Bl")
|
paulo@0
|
34
|
paulo@0
|
35 for (opt, val) in optvals:
|
paulo@0
|
36 if opt == '-B':
|
paulo@0
|
37 bold = True
|
paulo@0
|
38 elif opt == '-f':
|
paulo@0
|
39 fg = val
|
paulo@0
|
40 elif opt == '-b':
|
paulo@0
|
41 bg = val
|
paulo@11
|
42 elif opt == '-l':
|
paulo@11
|
43 colorLine = True
|
paulo@0
|
44
|
paulo@0
|
45 if len(args) > 0:
|
paulo@0
|
46 for line in sys.stdin:
|
paulo@0
|
47 repl = START
|
paulo@0
|
48 if bold:
|
paulo@0
|
49 repl += BOLD
|
paulo@0
|
50 if fg is not None:
|
paulo@0
|
51 repl += c[fg][0]
|
paulo@0
|
52 if bg is not None:
|
paulo@0
|
53 repl += ';' + c[bg][1]
|
paulo@0
|
54 repl += END + r'\1' + CLEAR
|
paulo@11
|
55
|
paulo@11
|
56 x = line
|
paulo@11
|
57
|
paulo@11
|
58 if colorLine == False:
|
paulo@11
|
59 x = re.sub(r'(' + args[0] + r')', repl, line)
|
paulo@11
|
60 else:
|
paulo@11
|
61 if re.search(args[0], line) != None:
|
paulo@11
|
62 last_char = line[-1]
|
paulo@11
|
63 x = repl.replace(r'\1', line[:-1])
|
paulo@11
|
64 x += last_char
|
paulo@0
|
65
|
paulo@0
|
66 sys.stdout.write(x)
|
paulo@0
|
67 sys.stdout.flush()
|